Different Threads - Different Heaps
-
Hi,
I recently ran into some troubles with allocating memory for Arrays in a multithreading dll. I am passing a pointer to a pointer as parameter to a new Thread. In this Thread the pointer is set to point to a new allocated Array.Unfortunately the function that allocates the memory has been written by a company and I do not have the .c file. So I can't say what exactly happens and I can't change it. However I have managed to get memory leaks using that function, so I assume it will be malloc().
What I am trying to do is accessing and freeing the allocated memory in the original thread after the Thread that has allocated the memory has finsished and been closed.
However this gives me an Assertion Failure by _PtrIsValidHeappointer.
I am fairly new to Multithreading and don't quite understand what is happpening there. However I guess I wouldn't run into this problem if the memory was allocated on the first threads?
Trouble really is, that I need the function I can't change.
Is there any way I can work around that problem?Glad, if someone can help me
Greetings
-
different language?
-
I would asumme the function doesn't use malloc() to get the memory, but some other function like GlobalAlloc() (or it modifies the pointer it gets from malloc() to store some internal information). So you should ask the documentation of this library how you can free the memory it gives you.
-
Hi,
seems, as ifI was to quick assuming that it was malloc().
You were right of course, it was some strange function the company wrote themselves as well.
However:
The companys function (let's call it dispose(void *Pointer)) will also only work in the Thread that assingned the memory.
Putting it in the calling Thread, the function will do nothing for about 5 seconds (and I can observe a memory leak) and then suddenly the program will crash way before the memory is actually full (not even a third being used at that time).
So I think there is some sort of random behaviour messing about with wrong parts of the memory. I observe the same behaviour if I actually use malloc and free instead of the companys functions.
So the initial problem I posted sdadly still remains.
I'd be glad if anyone had ideas on that. I heard that every Thread has its own Stack. Could this Error be due to some sort of an own Heap? Or is the Array not accessible anymore after the Thread finished, as it was kinda declared in a different block?Thanks a lot for the help
Henn