DLL memory Allocation
-
Hi, I am using LoadLibrary() and GetProcAddress() to get an exported function from a dll. The exported function looks like this: extern "C" { char *getStuff() { return new char[10]; } } When I call this function everything works fine until I try and 'delete' the memory that was allocated in this function. I get this error in the debug version - "HEAP[GenericTester.exe]: Invalid Address specified to RtlValidateHeap( 2f0000, 7b27a0 )". From what I guess that means that the application is trying to free memory from a different heap than the memory was allocated from? My question then is: how do I allocate memory inside of the GetProcAddress() function that can be freed by the calling application? Thanks!
-
Hi, I am using LoadLibrary() and GetProcAddress() to get an exported function from a dll. The exported function looks like this: extern "C" { char *getStuff() { return new char[10]; } } When I call this function everything works fine until I try and 'delete' the memory that was allocated in this function. I get this error in the debug version - "HEAP[GenericTester.exe]: Invalid Address specified to RtlValidateHeap( 2f0000, 7b27a0 )". From what I guess that means that the application is trying to free memory from a different heap than the memory was allocated from? My question then is: how do I allocate memory inside of the GetProcAddress() function that can be freed by the calling application? Thanks!
I suggest you check your project settings for you application and your dll and change them to use a shared C runtime. See this article (posted only a few days ago) for more details. http://www.codeproject.com/script/comments/forums.asp?app=25&df=5&exp=5&forumid=1647&fr=26&main=/script/comments/forums.asp#xx25315xx Stephen Kellett
-
I suggest you check your project settings for you application and your dll and change them to use a shared C runtime. See this article (posted only a few days ago) for more details. http://www.codeproject.com/script/comments/forums.asp?app=25&df=5&exp=5&forumid=1647&fr=26&main=/script/comments/forums.asp#xx25315xx Stephen Kellett
well the hyperlink was supposed to take you to the article, but I tested it and it failed. I should have included the article title DLL newing objects on process heap Its on the next page down after the page this is on (with 20 odd to the page) Stephen Kellett
-
well the hyperlink was supposed to take you to the article, but I tested it and it failed. I should have included the article title DLL newing objects on process heap Its on the next page down after the page this is on (with 20 odd to the page) Stephen Kellett
Thanks! It works now. I linked both with 'MultiThreaded DLL' so that the dll and application both use the same heap manager.
-
Hi, I am using LoadLibrary() and GetProcAddress() to get an exported function from a dll. The exported function looks like this: extern "C" { char *getStuff() { return new char[10]; } } When I call this function everything works fine until I try and 'delete' the memory that was allocated in this function. I get this error in the debug version - "HEAP[GenericTester.exe]: Invalid Address specified to RtlValidateHeap( 2f0000, 7b27a0 )". From what I guess that means that the application is trying to free memory from a different heap than the memory was allocated from? My question then is: how do I allocate memory inside of the GetProcAddress() function that can be freed by the calling application? Thanks!
I usually go a different route in this case. I generally add 2 functions: a CreateXXX and a DestroyXXX. The Create does the allocation and destroy frees the memory. This has the following side benefit: usually you are not simply allocating a block of memory, you are setting up other things like access to ports, hw resources, etc. When it comes time to free the memory, you usually have to cleanup other stuff, so why not do it all in one spot and the end user doesn't have to worry about the details. Mike
-
I usually go a different route in this case. I generally add 2 functions: a CreateXXX and a DestroyXXX. The Create does the allocation and destroy frees the memory. This has the following side benefit: usually you are not simply allocating a block of memory, you are setting up other things like access to ports, hw resources, etc. When it comes time to free the memory, you usually have to cleanup other stuff, so why not do it all in one spot and the end user doesn't have to worry about the details. Mike
That is a good idea. I'm not sure that I want to use it in this case though? The function in the DLL is called many times and returns many character buffers. That means that I have to keep track of all of these allocated character buffers in the dll, so that wehn DestroyXXX is called I free them all. I also can't have a DestroyLastAllocatedBuffer() function that frees the most recently allocated character buffer because the function in the DLL can be called by many different threads. (caveat: you probably could do both of the above with some extra work, but it might be more work than it is worth?) I'd prefer for the calling application to be able to call the DLL function to get a characted buffer and then free it right after it is done with the buffer. I think I have another solution though. You can create a C++ object with a destructor that does the memory freeing. It seems to work, anyone see any problems? // Text holder class CTextHolder { public: CTextHolder() ~CTextHolder() { delete text; }; char *text; }; // This is the exported function in the dll extern "C" { CTextHolder * getText() { CTextHolder *blah = new CTextHolder(); blah->text = new char[20]; return blah; } } // This is the function in the application that calls the exported function in the DLL void testCall(void) { HMODULE mod = LoadLibrary("d:\\plugin.dll"); if(mod == NULL) return; // Test function is typdefed as.. typedef CTextHolder * (*aFunc)(void); testFunction getText = (testFunction) GetProcAddress(mod, "getText"); if(getText == NULL) return; CTextHolder *blah = getText(); // Yay.. this seems to work fine delete blah; FreeLibrary(mod); }
-
I usually go a different route in this case. I generally add 2 functions: a CreateXXX and a DestroyXXX. The Create does the allocation and destroy frees the memory. This has the following side benefit: usually you are not simply allocating a block of memory, you are setting up other things like access to ports, hw resources, etc. When it comes time to free the memory, you usually have to cleanup other stuff, so why not do it all in one spot and the end user doesn't have to worry about the details. Mike
-
That is a good idea. I'm not sure that I want to use it in this case though? The function in the DLL is called many times and returns many character buffers. That means that I have to keep track of all of these allocated character buffers in the dll, so that wehn DestroyXXX is called I free them all. I also can't have a DestroyLastAllocatedBuffer() function that frees the most recently allocated character buffer because the function in the DLL can be called by many different threads. (caveat: you probably could do both of the above with some extra work, but it might be more work than it is worth?) I'd prefer for the calling application to be able to call the DLL function to get a characted buffer and then free it right after it is done with the buffer. I think I have another solution though. You can create a C++ object with a destructor that does the memory freeing. It seems to work, anyone see any problems? // Text holder class CTextHolder { public: CTextHolder() ~CTextHolder() { delete text; }; char *text; }; // This is the exported function in the dll extern "C" { CTextHolder * getText() { CTextHolder *blah = new CTextHolder(); blah->text = new char[20]; return blah; } } // This is the function in the application that calls the exported function in the DLL void testCall(void) { HMODULE mod = LoadLibrary("d:\\plugin.dll"); if(mod == NULL) return; // Test function is typdefed as.. typedef CTextHolder * (*aFunc)(void); testFunction getText = (testFunction) GetProcAddress(mod, "getText"); if(getText == NULL) return; CTextHolder *blah = getText(); // Yay.. this seems to work fine delete blah; FreeLibrary(mod); }
You wouldn't need to keep a list of anything. s = CreateXXX(); ... DestroyXXX( s ); The functions would be something like this: char *CreateXXX() { return new char[20]; } /// //I would actually Code CreateXXX as follows //ERRCODE CreateXXX( char **s ) //{ // *s = new char[20]; // return NOERR; ///} ERRCODE DestroyXXX( char *s ) { delete s[]; }
-
You wouldn't need to keep a list of anything. s = CreateXXX(); ... DestroyXXX( s ); The functions would be something like this: char *CreateXXX() { return new char[20]; } /// //I would actually Code CreateXXX as follows //ERRCODE CreateXXX( char **s ) //{ // *s = new char[20]; // return NOERR; ///} ERRCODE DestroyXXX( char *s ) { delete s[]; }
That seemed like a good solution to me too and I tried it, but it gave me an access violation. There may have been some bugs in my code (not allocating a large enough buffer and overflowing it), I'll try it again. Thanks!
-
I usually go a different route in this case. I generally add 2 functions: a CreateXXX and a DestroyXXX. The Create does the allocation and destroy frees the memory. This has the following side benefit: usually you are not simply allocating a block of memory, you are setting up other things like access to ports, hw resources, etc. When it comes time to free the memory, you usually have to cleanup other stuff, so why not do it all in one spot and the end user doesn't have to worry about the details. Mike
I agree, The CreateXXX,DestroyXXX approach is a good approach *if* the DLL the is designed to be used as a black box, where it is always responsible for allocation and deallocation of resources. However the original poster did not specify that. Sometimes the black box approach is not appropriate for architectural or performance reasons. Other times it is. I think both approaches have their merits. Stephen Kellett