Thread: Is this code memory leak free? ---> POSIX Threads

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    38

    Is this code memory leak free? ---> POSIX Threads

    Here is a small sample of what I am doing without much of the logic.. basically the threading part. Could someone tell me if this is memory leak free?

    If it is not, what could I do to make it memory leak free.

    Thanks,

    Code:
    pthread_mutex_t hMutex = PTHREAD_MUTEX_INITIALIZER;
    
    
    void SendFile()
    {
    	if ( pthread_mutex_lock( &hMutex ) != 0 )
    		cout << "Error locking mutex" << endl;
    
    	/* Some Code... */
    
    	if ( pthread_mutex_unlock( &hMutex ) != 0 )
    		cout << "Error unlocking mutex" << endl;
    
    	pthread_exit( NULL );
    }
    
    void Run()
    {
    	/*Some Code....*/
    	pthread_t uploadThread = {0};
    	if( pthread_create( &uploadThread, NULL, (void*(*)(void*))SendFile, NULL ) != 0 )
    		cout << "Error creating thread" << endl;
    }
    int main()
    {
    	Run();
    	pthread_mutex_destroy( &hMutex );
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > (void*(*)(void*))SendFile
    If you get the declaration of the function right, then this cast is not needed.

    > pthread_mutex_destroy( &hMutex );
    Maybe no leak, but deleting the mutex before you get a chance to use it would be bad.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    38
    What is the right way to declare it?

    I think it was something like SendFile( void** pVal ), then pass SendFile by reference.

    With regards to the mutex... is is created/initalized with

    Code:
    pthread_mutex_t hMutex = PTHREAD_MUTEX_INITIALIZER;
    I would think that would take care of it.. but if not, I acually do use joins before i destroy the threads, just forgot to put them in here.

    Thanks.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > What is the right way to declare it?
    The thread that creates it should be the thread that deletes it.

    > I acually do use joins before i destroy the threads, just forgot to put them in here.
    Oh well, if you want an ACCURATE response then, you know what to do.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    What is the right way to declare it?
    I think by "it" he meant the function, Salem.

    The right way is, as the pointer signature and the manpage for pthread_create say:
    Code:
    void *SendFile(void *arg)
    For the meaning of the return value, see pthread_exit and pthread_join.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Assuming you're using Linux, Valgrind is a great tool for detecting memory leaks: valgrind.org

    I find it doesn't help you fix memory leaks as much as detect them, though.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    38
    Yes I am in linux. I'll try it out, thanks for the suggestion. I've been looking for something like this and ran into a few programs but they seemed to conly catch basic memory leaks... dumb things like if you new something do you delete it. But i do need something to catch more advanced leaks then this. Thanks again

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Umm ... all memory leaks involve not deleting memory that was allocated. You can also have memory errors, where you delete something that wasn't allocated or delete something twice or use the wrong deletion form, but those aren't leaks (and they're detected by a normal allocator, except for the last one). Then there are resource leaks, of which memory leaks are one case. Other resource leaks involved allocating mutexes, network connections and other stuff that is in limited supply and not freeing them - but valgrind doesn't detect most of these.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    38
    ah, thank for the heads up

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by avalanche333 View Post
    Code:
    void Run()
    {
    	/*Some Code....*/
    	pthread_t uploadThread = {0};
    	if( pthread_create( &uploadThread, NULL, (void*(*)(void*))SendFile, NULL ) != 0 )
    		cout << "Error creating thread" << endl;
    }
    int main()
    {
    	Run();
    	pthread_mutex_destroy( &hMutex );
    	return 0;
    }
    This isn't even right. The code that calls pthread_create() has to either do a pthread_join() to wait for the spawned thread to finish, or call pthread_detach() on it. Since the code you posted contains no memory allocations anywhere, there is obviously no memory leak here, except for the created thread itself (since like I said, you never pthread_join() it!)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  2. Replies: 2
    Last Post: 09-28-2006, 01:06 PM
  3. Memory leak - need help finding
    By ChadJohnson in forum C++ Programming
    Replies: 8
    Last Post: 04-06-2005, 07:26 PM
  4. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM