Skip to main content
GameDev.net gamedev.net
🔒 Locked

Memory Heap Problems

Started by cybrgeek Mar 20, 2003 at 2:12 PM 3 replies 500+ views
Original Post
cybrgeek
cybrgeek
I’m writing a program that uses both an exe and dll using Microsoft Visual C++ 7. I need the dll to hold the mouse and keyboard hook procs for idle monitoring for older OS’s that don’t support the GetLastInputInfo function, as well as to store the COM interfaces to use context menus for files and folders in explorer. The program uses a large tree structure that is shared by the dll and the exe, and the memory for this tree is allocated dynamically with the new and delete operators in both the dll and exe. Because the memory is allocated in mixed places, some in the dll’s memory heap and some in the exe’s memory heap, I get an "...Invalid Address specified to RtlValidateHeap..." error when I try to free all the memory within the exe, because the delete operator can not free the memory allocated in the dll’s memory heap, even through the exe can read and write the memory for the dll and visa versa. The way around this is to make sure that they share the same heap. The only way I know of is to set the runtime library for both the dll and exe to "Multi-theaded DLL". This works fine, except the dll and exe both link to msvcr70.dll instead of the libraries being included within the exe and dll files. I’d prefer not to have to distribute this dll with my program because not all computers have this file, especially operating systems older than Windows XP. Other than overriding the new and delete operators to use VirtualAlloc to allocate the memory, (I use VirtualAlloc to share memory with system dll’s in my program, so I’d assume this would work), what can I do?
niyaw
niyaw
release all memory in the same module it's allocated in.

and how do you use VirtualAlloc to share memory? all dlls are already in your address space, there's nothing else to share. "sharing memory" generally is only applicable to doing so between different processes.

[edited by - niyaw on March 20, 2003 3:17:49 PM]
cybrgeek
cybrgeek
Like I explained, parts of the memory used in the tree are loaded in the DLL and other parts of the tree are loaded in the EXE, depending on the actions of the user since the DLL is loaded by explorer when you right click on a file or folder and the memory from the tree is shared with the exe if it happens to be running at the same time. Releasing or creating all the memory in the same module would mean a lot of code changes, which is why I''m trying to find another way, although that may be the path I decide to take. I use VirtualAlloc to allocate memory for calls to ReadDirectoryChangesW, because when I allocate memory with the new operator, which is the same as calling malloc, the function crashes and takes the whole app down with it.
niyaw
niyaw
use HeapAlloc. VirtualAlloc in your case is wasteful, it pays off when you need to allocate big chunks of memory (several mb, for instance).

but honestly, it sounds like your design isn''t clean, and now you''re trying to work around design deficiencies by hacking code.
cybrgeek
cybrgeek
thanks for your help, although HeapAlloc produced the same issues as new and malloc for calls to ReadDirectoryChangesW. Most of these changes are just quick hacks because I completely redesigned the program and was trying to salvage as much code until I have time to do it right, which I all ready have started to. It looks like I should be able to move all the memory allocation for the tree into the dll to fix the problem.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.