Thread: [GD-Windows] new/delete across DLL's
Brought to you by:
vexxed72
From: Ben H. <cr...@ca...> - 2003-03-24 17:08:58
|
Am I doing something wrong, or is this correct behaviour: in core.dll I have a class CFooBar. in the game exe, I call wibble =3D new CFooBar; Later on, within the exe, I call delete wibble; and get an Assert= warning because I'm deallocating from the wrong heap. This seems very strange, that I can only deallocate a class= defined in a DLL from within that DLL, even if the instance was= allocated somewhere else. Am I understanding this correctly, or= am I doing something else to cause the problem? Is there any way= round this? [ cruise / casual-tempest.net / transference.org ] |
From: Jon W. <hp...@mi...> - 2003-03-24 18:21:08
|
Welcome to DLL hell. There are two kinds of DLL hell: the Unix hell, where some library can=20 decide to replace malloc() in the global symbol table, and suddenly=20 you're running with some other allocator than you tested your code=20 with; and the Windows hell, where each DLL can run with its own=20 allocator (say, debug vs release vs version 7.0), which means that you=20 have to balance all allocations and deletions. The only sane way around this that I've found is to base your cross- DLL object model on factory functions and abstract interfaces, and use=20 "deleteYourself()" as a virtual function to delete an instance (or go=20 all the way and acquire()/release() with ref counting). You may note that COM does a lot of these things, btw :-) Also, for the same reasons, inline functions are often a very bad idea=20 for objects that are supposed to live in DLLs. First, because you have=20 to re-compile the user when you change the implementation of the object=20 (so you can't rev the DLL separately unless you take a LOT of care).=20 Second, because if you happen to allocate memory, then the inline may=20 live in one of many different modules. This is especially bad with STL=20 style containers. Cheers, / h+ > -----Original Message----- > From: gam...@li... > [mailto:gam...@li...]On Behalf Of > Ben Hawes > Sent: Monday, March 24, 2003 9:08 AM > To: gam...@li... > Subject: [GD-Windows] new/delete across DLL's >=20 >=20 > Am I doing something wrong, or is this correct behaviour: >=20 > in core.dll I have a class CFooBar. >=20 > in the game exe, I call wibble =3D new CFooBar; > Later on, within the exe, I call delete wibble; and get an Assert=20 > warning because I'm deallocating from the wrong heap. >=20 > This seems very strange, that I can only deallocate a class=20 > defined in a DLL from within that DLL, even if the instance was=20 > allocated somewhere else. Am I understanding this correctly, or=20 > am I doing something else to cause the problem? Is there any way=20 > round this? >=20 > [ cruise / casual-tempest.net / transference.org ] >=20 >=20 >=20 > ------------------------------------------------------- > This sf.net email is sponsored by:ThinkGeek > Welcome to geek heaven. > http://thinkgeek.com/sf > _______________________________________________ > Gamedevlists-windows mailing list > Gam...@li... > https://lists.sourceforge.net/lists/listinfo/gamedevlists-windows > Archives: > http://sourceforge.net/mailarchive/forum.php?forum_idU5 >=20 |
From: Adrian C. <ce...@ce...> - 2003-03-25 20:52:33
|
Sorry I didn't read all the replies to tis message, I just had to take the chance that this was already solved. The way we solved this in OGRE (which is a library that links dynamically) was to create our own memory manager class (based in the debug version on Paul Nettle's *very* *very* nice one) which makes sure that all the calls to *alloc/free and new/delete are mapped to static member functions of this memory manager class I told you about later. The end product is that all the memory gets (de)allocated only in the main DLL's thread. The side-effect is that you have to include the memory manager's header file in all the other DLL's as well as in the client application. My 2c, Adrian 'cearny' Cearnau ----- Original Message ----- From: "Ben Hawes" <cr...@ca...> To: <gam...@li...> Sent: Monday, March 24, 2003 7:07 PM Subject: [GD-Windows] new/delete across DLL's Am I doing something wrong, or is this correct behaviour: in core.dll I have a class CFooBar. in the game exe, I call wibble = new CFooBar; Later on, within the exe, I call delete wibble; and get an Assert warning because I'm deallocating from the wrong heap. This seems very strange, that I can only deallocate a class defined in a DLL from within that DLL, even if the instance was allocated somewhere else. Am I understanding this correctly, or am I doing something else to cause the problem? Is there any way round this? [ cruise / casual-tempest.net / transference.org ] ------------------------------------------------------- This sf.net email is sponsored by:ThinkGeek Welcome to geek heaven. http://thinkgeek.com/sf _______________________________________________ Gamedevlists-windows mailing list Gam...@li... https://lists.sourceforge.net/lists/listinfo/gamedevlists-windows Archives: http://sourceforge.net/mailarchive/forum.php?forum_idU5 |