Sharing memory between dll and application with HeapAlloc ?
-
Hi, I would like to do the following: Allocate datas in a dll and then free them in the main application. I have read there are potentials problems doing this because dll and process don't share the same memory, it depends on the compiler,... don't do this - ok. So I was looking for a solution and I went accross the Heap functions. It seems that when I allocate memory like this: HeapAlloc(GetProcessHeap(),... the default Heap is used to store the memory. Then the dll and my application will share the same Heap for that data. So they share the same Heap, but I still wonder if it safe is to call HeapAlloc from the dll and HeapFree from the application for the same data ? Any idea ?
-
Hi, I would like to do the following: Allocate datas in a dll and then free them in the main application. I have read there are potentials problems doing this because dll and process don't share the same memory, it depends on the compiler,... don't do this - ok. So I was looking for a solution and I went accross the Heap functions. It seems that when I allocate memory like this: HeapAlloc(GetProcessHeap(),... the default Heap is used to store the memory. Then the dll and my application will share the same Heap for that data. So they share the same Heap, but I still wonder if it safe is to call HeapAlloc from the dll and HeapFree from the application for the same data ? Any idea ?
why can't you provide your own FreeMyDllObject() funtion inside the DLL, matching the way your DLL code allocates its stuff. Then the caller doesn't have to know the messy details, and there is less dependency between EXE and DLL. Later on, you could even change your DLL's memory management without changing the EXE at all. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
why can't you provide your own FreeMyDllObject() funtion inside the DLL, matching the way your DLL code allocates its stuff. Then the caller doesn't have to know the messy details, and there is less dependency between EXE and DLL. Later on, you could even change your DLL's memory management without changing the EXE at all. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Hi Luc, There are various situations and memory is not always allocated in the same place. I have got 3 different case: - a DIB allocated in main thread and used in the dll - a DIB loaded in a thread used in the dll - a DIB loaded in a dll used in main thread I would need 3 different FreeMyObject for a unique DIB class while this HeapAlloc / HeapFree possible solution is common, and as simple as new / delete. I see it as a great simplification. Provided it is reliable on all systems. Thanks for your answer.