I have a thread function which allocates memory using malloc(). I kill the thread using pthread_kill without freeing the dynamically allocated memory.Will it be freed automatically once i call pthread_kill or there will be a leak?
Memory you are allocating in one thread doesn't "belong" to that thread. It is allocated from the same global heap all the other threads are using your program. So you have to free all the memory you have allocated otherwise you end up with a leak.
As vicatcu says, there will be a leak.
I wouldn't ever recommending using pthread_kill unless you absolutely have to. Instead, you should create a signaling mechanism to let the thread know when it should be finished, and then join on the thread. And the thread function should poll that value occasionally, and if it gets a terminate signal, it should clean up its own resources and exit.
The other option, of course, is to try not to allocate memory in threads. But I guess you don't always get that luxury. :-)
there will be a leak. how would pthreads kill function possibly know the names of variables that have been assigned via malloc? There is no garbage collection in C, if you call malloc somewhere, you better make sure that you call free somewhere else.
[Edit]
Maybe you should just set a global flag variable associated with your thread, and have your thread poll that variable occasionally to know if and when it should terminate itself.
Yes, assuming you don't install a signal handler for the signal you use and the default action for the signal is process termination.
That's because pthread_kill does not kill a thread, it sends a signal to the thread. If the action of that signal is to terminate the process, then the whole process ceases to exist, and with it, any dynamic memory allocated by any thread in the process.
There is no way to kill a thread.
Related
Before using a pthread_mutex_t pthread_mutex_init() should be called, and after it's not longer required it should be destroyed using pthread_mutex_destroy().
My question is, what happens if my process terminates before it can call pthread_mutex_destroy(), for example a SIGKILL, SIGINT or SIGABORT? Is it possible that some resource will leak?
Same question goes to pthread_cond_init/destroy as well.
Not on any platform you're likely to use. Objects like mutexes and condition variables are just chunks of memory in the process' address space. When a process terminates, its address space ceases to exist. So it's not possible for any resources to leak.
Process-shared resources are more complex. While the resources won't leak, they may still exist and may even still be in use by other processes.
I have a C program creating a detached thread as a child.
Inside of the function I pass to pthread_create I use pthread_detach to detach the thread. At the end I call pthread_exit((void *) 0)
I would like to know if it is normal behaviour that the memory consumption increases after the thread is created.
I did a valgrind check and there are no leaks just 4 suppressed errors.
I would like to know if it is normal behaviour that the memory consumption increases after the thread is created.
Yes, as
each thread gets its own stack assigned. The size is OS setting dependend and could be around 1M.
some system resource will be used to manage each thread itself.
Both will be released if the thread ends for a detached thread or if the thread was joined for a joinable thread.
On linux, pthread (linux threads),
what does happen to the running threads when returning from main (before the threads are finished)?
When returning from main, the memory is dis-allocated so the threads should access unallocated memory. Does this cause the threads to exit?
I'm sure the threads are killed, but how does this actually happen?
I'm sure the threads are killed, but how does this actually happen?
Returning from main is the same as calling exit(). This means handlers established by atexit(), and any system cleanup handlers are run. Finally the kernel is asked to terminate the entire process(i.e. all threads).
(Note that this might cause issues if you have other threads running at that point, e.g. another thread accessing a global C++ objects right after the runtime calls their destructors.)
Well, threads operate under the process of main application (or other process but I assume you do not create another process, just threads). They share memory with it, and are the same process, so is system kills the process it automatically kills all threads. There is nothing more to it. A thread cannot exists without a process, so there is no option of accessing some disallocated memory, it just stops executing, and the memory is cleaned up on a process clean-up level.
And how it happens is obviously system dependent. E.g. Windows 95 did not free memory after a process ended, so if application had a memory leak, killing it didn't help. This had changed since then. Every system can handle it differently.
I experience some memory allocation problems and try to detect possible reasons for these problems.
There are many possible reasons, and lots of hours must be spent to check each of them.
One of the possible reasons is that there is a memory buffer, that is allocated within a thread, and this buffer is used after the thread terminates.
So, if there is a chance that thread termination causes memory deallocation, then many hours of debugging may be avoided.
Thank you very much in advance.
I don't think it does, although it of course might depend on your particular details.
Generally, memory allocation from the operating system's point of view is a per-process activity, while threads exist inside the process. So if one thread allocates memory and then dies, the operating system doesn't clean that up since the process is still alive. Memory is shared inside the process, so the OS can't know that the memory no longer is used and can be cleaned up.
No, threads that 'die' do not deallocate any memory.
When a thread ends, the thread itself vanishes from memory, like a function does once it's done executing. It will take all the 'stack' objects with it, but all the memory you allocated yourself (i.e. malloc) will still be there.
As such, before you end your thread, you should make sure that all dynamic memory that was used by the thread and is not needed any more is freed properly.
Anything on the thread's stack (a local variable, for example) becomes invalid when the thread ends. However, if the data is in the heap, then the memory is still valid as long as the process is running. Of course, you'll need to save the pointer to that heap allocation somewhere outside that thread.
Memory allocated by a thread behaves like memory allocated by a method call:
variables on the stack will be dealocated when the method returns (thread terminates)
variables on the heap will continue to be allocated unless explicitly deallocated.
In addition to all answers, I'd like to make a note that pthread has a TLS keys which are registered with pthread_key_create which accepts key ID and destructor functions. On pthread_exit a static pthread_key_clean_all() is called that iterates through the keys and invokes assigned destructors that may perform memory deallocation (by application design).
So, to understand that - search in your code all pthread_key_create invocations, check if a destructor assigned and the put breakpoints to all of them to check what and in which order is destroyed.
hello everyone I have some question about threads if for example I have some thread 1 which allocates some piece of the memory, and anohter thread (let's assume 2) is killing thread 1 using pthread_cancel() or just using return what is going on with the peace of memory which it allocated? will be leak if thread 1 didn't free this piece of memory? thanks in advance for any answer
edited
just to make it clearer, as I know pthread_cancel() kills thread, but what is going on with its memory when I kill it? In case of return all thread will be dead if 1 is the main thread
Yes, it will leak memory in that case. C does not have any garbage collection -- if you allocate memory and fail to free it, it will be leaked, plain and simple.
If you want to avoid leaking memory, don't call pthread_cancel. Make your threads exit gracefully by setting a flag asking them to exit, and then when they detect that that flag is set, they can free their memory and kill themselves by returning from their thread procedures or by calling pthread_exit.
Alternatively, you can set a thread cleanup handler by calling pthread_cleanup_push, which will get called when your thread exits or gets canceled by a call to pthread_cancel. You can use a handler function which will free any allocated memory you have outstanding.
First of all whether it gets cancelled immediately or not depends on cancellation state.
please check "pthread_setcancelstate" and "pthread_setcanceltype".
Also it is important to have a handler after a thread is cancelled.In the handler all the resources must be freed, like locks and memory, it is similar with the return.So, Yes, there is a chance of leak if implementation is not right.
I suggest to look at the implementation of a method or function before using it.
Hope this helps.