This is the mail archive of the pthreads-win32@sources.redhat.com mailing list for the pthreas-win32 project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Handle leak when using pthread mutex with win32 api threads


On Fri, 2005-03-04 at 12:03 -0500, Gottlob Frege wrote:
> On Fri, 4 Mar 2005 17:52:58 +0200, Dmitry Sernii <bogolt@gmail.com> wrote:
> > Hello All!
> > I have handle leak, when using win32 api threads with pthread mutexes.
> > I'm using pthread-win32 2005-01-03 pthread snapshot.
> > 
> > Here is the sample application which reproduce the problem:
> > 
> > #include <pthread.h>
> > #include <windows.h>
> > #include <conio.h>
> > 
> > const int max_threads = 500;
> > 
> > DWORD WINAPI threadProc(void* param)
> > {
> >        static pthread_mutex_t mutex(PTHREAD_RECURSIVE_MUTEX_INITIALIZER);
> 
> that line above is not thread safe.  you might initialize the same
> mutex multiple times.  Not good.
> 

The handle leak comes about because, in pthreads-win32, pthread_mutex_t
is only a pointer, and the actual mutex struct is malloced by the
library, in this case, the first time you call pthread_mutex_lock().
That is, the library just calls pthread_mutex_init() for you. Also, you
must always call pthread_mutex_destroy() to clean up, just the same as
if you had called pthread_mutex_init() yourself.

So, in your sample code, any destructor that gets called to clean up
'mutex' will only clean up the pointer, leaving the mutex struct and any
handles that were created for it behind.

POSIX is defined in C terms and you can't use C++ shortcuts like this
generally. They may work for some pthreads implementations, but that
will be accidental. You need to find a C++ wrapper library or write one
if you want to take advantage of C++ magic.

Regards.
Ross

> >        pthread_mutex_lock(&mutex);
> >        pthread_mutex_unlock(&mutex);
> >        return 0;
> > }
> > 
> > void main()
> > {
> >        DWORD id;
> >        HANDLE th[max_threads];
> >        int i;
> >        for (i=0;i<max_threads;i++)
> >                th[i]=CreateThread(0,0,threadProc,0,0,&id);
> > 
> >        for (i=0;i<max_threads;i++)
> >        {
> >                WaitForSingleObject(th[i],INFINITE);
> >                CloseHandle(th[i]);
> >        }
> >        getch();
> > }
> > 
> > after that if you take a look at Task Manager you can see lots of
> > handlers used by the program. If you replace win32 API threads with
> > pthreads everything works fine. Also this program works ok if you
> > change pthread mutexes with win32 mutexes.
> > The same problem happends when using QT threads (version 3.3.4
> > )instead of win32 api threads.
> > 
> > problem reproduces with MSVC 6.0 compiler.
> > 
> > Best Regards,
> > Dmitrii Sernii
> >
> 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]