Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory allocation and deallocation across threads

I'm still trying to debug a very sneaky memory corruption problem. I came across a section of my code that allocates memory on one thread and deletes it on another.

I have a vague feeling that this is wrong, but I'm not sure why. The threads share the process memory and access to these structures is protected by a mutex, so I think everything would work. However, is it there any danger I'm not seeing?

like image 277
ggambett Avatar asked Dec 23 '08 21:12

ggambett


People also ask

What is memory allocation and deallocation?

A process has to be loaded into the RAM for its execution and remains in the RAM until its completion. Finished processes are deallocated or removed from the memory and new processes are allocated again. This is how the OS works with allocation and deallocation.

What is the correct pair of memory allocation and deallocation?

The correct answer is option 4. Concept: Heap memory allocation: Heap allocation is the most flexible allocation scheme. Allocation and deallocation of memory can be done at any time and at any place depending upon the user's requirement.

Do threads allocate memory?

Thread-local storage is a memory location, which is allocated and freed by a single thread. Therefore, there is no need to synchronize allocation and deallocation of thread-local storage. Specifically, we enhance the memory management functions with two new functions, tls malloc and tls free.

How dynamic memory allocation and deallocation is performed?

When a class object is composed of other class objects, the destructor of the container class object is invoked first. After this the destructors of the component objects are invoked in the opposite order in which they were constructed.


2 Answers

As indicated in another answer by @monjardin, there is nothing inherently wrong with what you are trying to do.

As an additional thought, you didn't mention the platform, etc. you are running into this problem on but if multi-threading is new to you and/or this application you are working on, you want to be sure that the standard support libraries you are using are the thread safe versions of the libraries. In many environments/platforms they have both the single threaded and multi-threaded versions of the support libraries available to the developer. If you are using threads but linking against the single thread version of the libraries, lots of bad things could be happening. For example, in a single threaded support library for malloc() and free() it would not have mutex protection for the heap (as an optimization). The multi-thread version of the library would add mutex protection to the heap manager to support more than one thread manipulating the heap at a time. (This is just one example).

like image 194
Tall Jeff Avatar answered Sep 17 '22 09:09

Tall Jeff


Nope, This is fine, and quite common especially with windows programming using CreateThread, where you might allocate the arguments on the heap, and pass the argument as the a void * argument to CreateThread. And the easiest thing to do is to let the called thread delete it's arguments when it's done with them. However if you are having memory corruption issues, and you are concerned about having one thread delete memory that another created, perhaps you should consider if there is a double delete happening, say if the transfer of what context is now responsible for cleaning up the allocated memory is not clear, perhaps both calling and called section are doing it?

like image 44
Dan Avatar answered Sep 21 '22 09:09

Dan