Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. DLL memory Allocation

DLL memory Allocation

Scheduled Pinned Locked Moved C / C++ / MFC
questiondebuggingperformancehelpannouncement
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Conor Hunt
    wrote on last edited by
    #1

    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!

    L M 2 Replies Last reply
    0
    • C Conor Hunt

      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!

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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

      L 1 Reply Last reply
      0
      • L Lost User

        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

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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

        C 1 Reply Last reply
        0
        • L Lost User

          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

          C Offline
          C Offline
          Conor Hunt
          wrote on last edited by
          #4

          Thanks! It works now. I linked both with 'MultiThreaded DLL' so that the dll and application both use the same heap manager.

          1 Reply Last reply
          0
          • C Conor Hunt

            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!

            M Offline
            M Offline
            MikeG 0
            wrote on last edited by
            #5

            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

            C L 3 Replies Last reply
            0
            • M MikeG 0

              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

              C Offline
              C Offline
              Conor Hunt
              wrote on last edited by
              #6

              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); }

              M 1 Reply Last reply
              0
              • M MikeG 0

                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

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                I think thats called a destructor. :)

                1 Reply Last reply
                0
                • C Conor Hunt

                  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); }

                  M Offline
                  M Offline
                  MikeG 0
                  wrote on last edited by
                  #8

                  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[]; }

                  C 1 Reply Last reply
                  0
                  • M MikeG 0

                    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[]; }

                    C Offline
                    C Offline
                    Conor Hunt
                    wrote on last edited by
                    #9

                    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!

                    1 Reply Last reply
                    0
                    • M MikeG 0

                      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

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups