Let's say I have started new thread with pthread_create()
and then detached it using pthread_detach()
. Now, from within the thread context, I allocated some memory using malloc()
.
When the thread exits, will the malloc'ed
memory be freed automatically?
So now answer should be obvious, any memory allocated has no linkage to threads so it is not free()'d on thread exit. Of course you can provide some special handling mechanics but it is not done automatically.
Good side of this is you can pass memory allocation between threads in other words allocate such resource in one thread and then free from another (is it good for you or not).
Hope this would be useful explanation, think about allocated memory pointer as about any process-level descriptor.
No - malloc'ed memory is only ever freed by an explicit 'free'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With