[FFmpeg-devel] Close Thread Handle in avcodec_thread_free

Reimar Döffinger Reimar.Doeffinger
Fri Aug 7 16:41:02 CEST 2009

On Fri, Aug 07, 2009 at 12:59:07PM +0100, Shehzad Salim wrote:
> But I have no idea to how to create a diff file or patch file on windows OS.

Have you ever tried TortoiseSVN? Not only can you easily get the latest
SVN of FFmpeg (which patches have to be against), you also can also
easily create diffs of the changes you made.
Also several IDEs (like eclipse) have at least optional plugins for SVN
support that can do that kind of thing, too.
Of course you can just use the commandline subversion client in exactly
the same way you do on Linux or so.

> Only one line is added in avcodec_thread_free function which is
> CloseHandle(c[i].thread);
> void avcodec_thread_free(AVCodecContext *s){
>     ThreadContext *c= s->thread_opaque;
>     int i;
>     for(i=0; i<s->thread_count; i++){
>         c[i].func= NULL;
>         ReleaseSemaphore(c[i].work_sem, 1, 0);
>         WaitForSingleObject(c[i].thread, INFINITE);
>         if(c[i].work_sem) CloseHandle(c[i].work_sem);
>         if(c[i].done_sem) CloseHandle(c[i].done_sem);
>         CloseHandle(c[i].thread);
>     }
>     av_freep(&s->thread_opaque);
> }

Ok, I read it up. This is indeed correct since we use _beginthreadex.
It is also incomplete though, the thread function must call
_endthreadex, the return 0 is wrong and may leak memory, too (well,
actually the documentation contradicts itself here, it says that
_endthreadex will be called automatically anyway, but also says that
calling it will help reclaiming resources, the exact quote:
"You can call  _endthread or  _endthreadex explicitly to terminate a
thread; however, _endthread or _endthreadex is called automatically when
the thread returns from the routine passed as a parameter. Terminating a
thread with a call to endthread or _endthreadex helps to ensure proper
recovery of resources allocated for the thread.").

More information about the ffmpeg-devel mailing list