Thread: PThread Handles Not Closing?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    265

    PThread Handles Not Closing?

    Is there a CloseHandle For PThreads?

    Im using somewhat portable code, and while watching my code execute via process explorer, im noticing that the software is generating huge numbers of thread handles but never destroying them. Im using the posix thread lib, and the threads appear to be terminating, however the thread handles never close. I have tried pthread_exit in the thread itself, and outside i have attempted pthread_cancel, pthread_kill, pthread_join, and delete thread. While the thread is no longer processing anything, it never closes its handle and in turn causes a memory leak. The threads themselves are in an overly simple wrapper class, constructor creating the thread datamember, and calling pthread_create. The kill code is in the objects destructor. When the thread is done, it deletes the wrapper thus killing itself (which is possibly where the problem is?)

    I have also experimented with an external cleanup thread running, issueing kills to the worker threads once they are finished, and have not had any successful results.

    Any help would be greatly appreciated. Is there a tool i can use to determine exactly where in my code the leak happens? Thanks for your time.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    I'm not familar with POSIX however in Win32 threads handles should be closed with CloseHandle when they are not needed, this isn't done automatically when the thread has exited. You can close a thread handle while a thread is still running and this won't make the thread exit.
    Last edited by Quantum1024; 04-17-2005 at 06:03 PM.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    What implementation of pthreads are you using?

    gg

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    Sorry for the late reply, have been busy.
    Im not sure about the implimentation, i simply use G++ with the -lpthread option. Im usually working on FreeBSD, however I havent seen any differences in application behavior between operating systems.
    Thanks for your time.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Do you create your threads detached or not detached?

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    Detached i believe, i just use pthread_create( &thread, 0, &exeWrapPthreads, this); So im not exactly sure.

    I never join the threads, or set any attributes. Are there some necessary things i forgot to do here?

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> I never join the threads
    http://www.rt.com/man/pthread_join.3.html
    Read the description carefully.

    http://www.llnl.gov/computing/tutorials/pthreads/

    gg

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    Thanks alot codeplug, you were absolutely right. Sticking pthread_join in fixed the bulk of the problem instantly. (There is still a 12 byte memory leak hiding somewhere, but its almost too small to worry about)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pthread question
    By quantt in forum Linux Programming
    Replies: 7
    Last Post: 04-07-2009, 01:21 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. The Pthread Hell
    By matott in forum Linux Programming
    Replies: 1
    Last Post: 04-10-2005, 05:59 AM
  4. Handles to visible programs
    By Snip in forum Windows Programming
    Replies: 3
    Last Post: 03-26-2005, 08:52 PM
  5. Closing thread handles from within
    By Sang-drax in forum Windows Programming
    Replies: 6
    Last Post: 09-26-2003, 12:18 PM