2

I've stumbled across a code example here. The lines that caught my attention (all other lines skipped):

{  
...  
void *res;  
...  
s = pthread_join(tinfo[tnum].thread_id, &res);  
...  
free(res);      /* Free memory allocated by thread */  
}  

Can anyone deeper into pthreads than myself comment on the free(res), please? I have to say I have never seen this before, and that googling for 1-1.5 hours didn't give me any other similar examples.

unwind
  • 391,730
  • 64
  • 469
  • 606
tum_
  • 632
  • 1
  • 7
  • 16

1 Answers1

1

In pthread_join(thread_id, &res) , if &res is not NULL - is free(res) needed?

It depends whether the return value of thread was dynamically allocated (with malloc() & co).

If you look at the function thread_start() on same page, you'll see that it has a return statement:

return uargv;

and uagrv was allocated with:

       uargv = strdup(tinfo->argv_string);

Hence, the free() call is used in main() after the pthread_join() call. Because res is the filled with uargv (returned by the thread). You can conceptually assume there's a code like this inside pthread_join() function:

 if (res)
   *res = uargv;

Here's it's allocated using strdup() (which internally allocates memory). So you free() it. If the thread simply has return NULL; (and free()'s the uargv itself) then you don't need free().

The general answer is if you allocate something with malloc() family functions then you need to free().

P.P
  • 117,907
  • 20
  • 175
  • 238
  • deleted, fighting with the engine ;) – tum_ Mar 03 '16 at 12:14
  • Thank you for the prompt reply. I missed the strdup() (or, rather, auto-converted it to alloca() while reading, though alloca() is not allowed to be used for this purpose). You actually gave me a new direction in thinking... I somehow assumed that once a thread is (correctly) terminated its resources are freed automatically by the system (with a few explicitly mentioned in man-pages exclusions). – tum_ Mar 03 '16 at 12:17
  • Thank you for the prompt reply. I missed the strdup(). You actually gave me a new direction in thinking. I somehow assumed that once a thread is (correctly) terminated its resources are freed automatically by the system (with a few explicitly mentioned in man-pages exceptions).: "If a thread is joinable, then another thread can call pthread_join(3) to wait for the thread to terminate and fetch its exit status. Only when a terminated joinable thread has been joined are the last of its resources released back to the system." – tum_ Mar 03 '16 at 12:27
  • @A.Toumantsev `I somehow assumed that once a thread is (correctly) terminated its resources are freed automatically by the system` - No. But it's true for a *process* termination (this is true on modern operating systems -- but there can be systems such as some embedded platforms where as explicit free() is required to reclaim the memory even on process exit). – P.P Mar 03 '16 at 15:43
  • 1
    @A.Toumantsev: Memory allocated with `malloc()` (or the equivalent, like `strdup()`) is not a resource belonging to the thread - it belongs to the entire process. Resources belonging to the thread are things like the thread's stack. – caf Mar 04 '16 at 03:45
  • Yep, thanks guys, things are getting much clearer now. [don't know how to 'Vote' for the answer or whatever else people are supposed to do here]. – tum_ Mar 04 '16 at 15:09
  • @A.Toumantsev You need 15 rep to upvote any post. So,unfortunately you can't do it yet! But thanks for thinking of upvoting :) – P.P Mar 04 '16 at 16:32