# Passing smart pointers to/from DLL

## Recommended Posts

Hello, I have a smart pointer object system in a set of source files considered the "core" package. This set of files is included in two different projects, one an engine DLL, and the other an Application project. Most of the work I have done so far is in the core package testing it via a console window. Now I'm to the point where I want to connect the dll to the app. I have a function exported from the DLL called GetEngine() which returns an exported class, which has functions like Initialize(), SetRenderer(), DrawScene(), etc. I started coding my first function in the exported class which is Initialize() which takes a smart pointer to a Config class. The config class is populated manually through code or by parsing a config file in the game directory. So I have: in DLL Engine class:
bool Initialize( const ConfigPtr &config )
{
std::cout << config->GetInt( "ScreenWidth" ) << '\n';

return true;
}


in test application:

ConfigPtr config = new Config( "main.cfg" );
engine->Initialize( config );

...


It works fine, up until the ConfigPtr's destructor, which then decreases the reference count to zero and then deletes the Config object. I get a CrtIsValidHeapPointer() function assert error. I've tried passing config by value also, instead of a const reference. Anybody know why it is doing this? I set a breakpoint, and when it goes to delete the "Autos" tab in the debugger displays the object with correct values, so the object should still exist in memory. I checked to see if it was trying to delete the object twice, and it isn't. hmmm Thanks.

##### Share on other sites
The decontructor is being called in the dll which then tries to delete memory that was allocated by your program which is a nono.

##### Share on other sites
Actually when I set the breakpoint in the destructor the stack trace shows that it's being called from the application.

##### Share on other sites
Problem solved, sorry for premature posting...I generally have much more patience and persistence before resorting to forums. I think I need a nap.

Problem was with the config file not actually containing a ScreenWidth property, and invalid use of std::map.

##### Share on other sites
Quote:
 Original post by O_oThe decontructor is being called in the dll which then tries to delete memory that was allocated by your program which is a nono.

I agree, you should generally create and destroy objects in the same module.. However, when considering objects managed by smart pointers, these should deallocate fine as long as you use the DLL version of the runtime library (both in your app, and all your DLLs).

##### Share on other sites
How could the object get deleted from the DLL though? The object is passed as a const reference. When the Initialize() function returns the temporary reference that is created is destroyed, but that's just an address, not the object, so the destructor wouldn't actually get called until the application's ConfigPtr's stack frame is popped. Right?

##### Share on other sites
I didn't actually read too carefully through your particular problem, since you explained it was already solved (and didn't seem to relate to the original question).

My comment was just ment a general input. In a recent project we've been sharing memory across the application and its DLLs through constructs like smart pointer, referenc counted string classes, etc, and it all works fine as long as you make sure to use the DLL runtime libraries.