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. new in dll, delete in exe, bang :(

new in dll, delete in exe, bang :(

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
4 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.
  • R Offline
    R Offline
    Rupel
    wrote on last edited by
    #1

    hi, i'm writing a (legacy, non-MFC) dll and there might get an exception thrown in there. it is caught in the exe-file that uses that dll. dll: //some error throw new CSomeException(); exe: catch (CSomeException *e) { e->SomeFunction(); // works fine delete e; // crash } the delete-call crashes because the object is not a valid *local* heap-pointer. stepping in dbgheap.c reveales the following passage /* * If this ASSERT fails, a bad pointer has been passed in. It may be * totally bogus, or it may have been allocated from another heap. * The pointer MUST come from the 'local' heap. */ _ASSERTE(_CrtIsValidHeapPointer(pUserData)); // fails so, finally. how am i supposed to handle this? i mean throwing an exception in a library isn't that uncommon, is it? :confused: :wq

    N R 2 Replies Last reply
    0
    • R Rupel

      hi, i'm writing a (legacy, non-MFC) dll and there might get an exception thrown in there. it is caught in the exe-file that uses that dll. dll: //some error throw new CSomeException(); exe: catch (CSomeException *e) { e->SomeFunction(); // works fine delete e; // crash } the delete-call crashes because the object is not a valid *local* heap-pointer. stepping in dbgheap.c reveales the following passage /* * If this ASSERT fails, a bad pointer has been passed in. It may be * totally bogus, or it may have been allocated from another heap. * The pointer MUST come from the 'local' heap. */ _ASSERTE(_CrtIsValidHeapPointer(pUserData)); // fails so, finally. how am i supposed to handle this? i mean throwing an exception in a library isn't that uncommon, is it? :confused: :wq

      N Offline
      N Offline
      Nemanja Trifunovic
      wrote on last edited by
      #2

      I bet at least one of your modules (exe or dll) has CRT statically linked. That means that one instance of CRT is allocating the memory and another one tries to release it ... BANG! A solution is to link all your modules dinamically (option /MD) against CRT. This way they will share the same CRT instance, and you should be able to allocate memory in one module and release it in another. Either that, or make sure that each module cleans its own memory.


      My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

      R 1 Reply Last reply
      0
      • R Rupel

        hi, i'm writing a (legacy, non-MFC) dll and there might get an exception thrown in there. it is caught in the exe-file that uses that dll. dll: //some error throw new CSomeException(); exe: catch (CSomeException *e) { e->SomeFunction(); // works fine delete e; // crash } the delete-call crashes because the object is not a valid *local* heap-pointer. stepping in dbgheap.c reveales the following passage /* * If this ASSERT fails, a bad pointer has been passed in. It may be * totally bogus, or it may have been allocated from another heap. * The pointer MUST come from the 'local' heap. */ _ASSERTE(_CrtIsValidHeapPointer(pUserData)); // fails so, finally. how am i supposed to handle this? i mean throwing an exception in a library isn't that uncommon, is it? :confused: :wq

        R Offline
        R Offline
        Ravi Bhavnani
        wrote on last edited by
        #3

        If my memory serves me right, an object new'd by a regular MFC DLL must be destructed by that DLL. An object new'd by an extension MFC DLL can be destructed by the client. See this[^] article. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

        1 Reply Last reply
        0
        • N Nemanja Trifunovic

          I bet at least one of your modules (exe or dll) has CRT statically linked. That means that one instance of CRT is allocating the memory and another one tries to release it ... BANG! A solution is to link all your modules dinamically (option /MD) against CRT. This way they will share the same CRT instance, and you should be able to allocate memory in one module and release it in another. Either that, or make sure that each module cleans its own memory.


          My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

          R Offline
          R Offline
          Rupel
          wrote on last edited by
          #4

          bingo! the DLL was set to /MT[d] - I changed that to /MD[d] and it seems to work (just gave it a quick shot) thx, man! :rose: :wq

          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