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
U

User 1556219

@User 1556219
About
Posts
1
Topics
1
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Memory Management across DLLs
    U User 1556219

    Hi guys, this is probably a very *niche* question, and I don't suppose I'll get many answers, but here's hoping ... :( My program has two components, let's call them ... program.exe and library.dll - library.dll is statically linked to program.exe throughout program.exe's entire lifetime. Suppose a function in program.exe calls a function in library.dll, which allocates some memory using the "new" operator. I've recently discovered that releasing this allocated memory from a function in program.exe causes a crash. You have to release the dynamic memory from the same *module* it was allocated in. Therefore, if I allocated memory using a statement in library.dll, I have to release it using another statement in library.dll, not in program.exe. It took me long enough to figure that out without any documentation, and that's fine, but my problem is as follows: Suppose I have a structure of two nested classes. class A; class B { A a; // contains an instance of A }; Class A is defined in library.dll, class B in program.exe Class B is instanced in the global scope in program.exe. Now, suppose I call a function in class B (program.exe) that then calls a function in class A (library.dll) to allocate some dynamic memory. Class A has a destructor that should theoretically release the memory once the lifetime of the instance "a" expires. When the program closes, this is usually done inside the exit() function when all destructors are executed, when the program is closing. But here comes my problem: Before the program closes, C++ throws an exception as the "delete" operator on the dynamic memory is executing. This happens, as I see it (and hopefully I'm wrong), because C++ somehow treats the "delete" statement that is defined inside library.dll as a code of program.exe because it's calling it from the global destructor. Having a destructor of class A in library.dll for the dynamic memory causes a crash. However, when I define a destructor of class B in program.exe that calls a function in class A to delete the memory, the program ends fine without any crashes. I hope I have made the matter clear, confusing though it may be. Does anyone have the slightest idea what may be going wrong? I'd be very grateful, thanks.

    C / C++ / MFC question c++ performance help announcement
  • Login

  • Don't have an account? Register

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