# new / delete within dll

This topic is 4729 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello. I want to get a class from my dll. So I call an exportet function from the dll: void* create() { return (void*)new Class(); } and then I use the object through an Interface. It works but as soon as I want to delete the object I get some pointer errors. I think it is because the dll and exe use different heap spaces. I tried to change the MT and MD but it didn't worked. So is it possible to call new within the dll and delete within the exe? thanks

##### Share on other sites
No, you must either do it in one place or the other. Take a look at this tutorial to see one way of doing it if you were making plugins. The main concept you should look at is the:
extern "C" __declspec(dllexport) void GetPlugin( I_TextOutput** ppOutput ){    *ppOutput = new CConsole;}
Then you handle the delete the same way. I have used this method before and it worked fine. I have my project avaliable if you'd like to have a look. (Far from perfect and final [lol])

- Drew

##### Share on other sites
It should work if you use the same library versions. You're not trying to delete the void pointer returned from the creation function, right? Perhaps you forgot to change the runtime version in debug mode or something (I frequently manage a single configuration only, updating all of them should really have been the default)?
I suppose you could overload a placement delete for the class and implemented it in the external library.

There are other ways of handling this however. Personally I kinda like the COM-style version.
class foo { foo(); ~foo();public: static foo *create(); void release();};foo *foo::create() { return new foo; }void foo::release() { delete this; }

##### Share on other sites
You can always derive your class in the dll from a base class that overloads new and delete. that way the proper new/delete will be called.

CHeers
Chris

##### Share on other sites
Quote:
 Original post by chollida1You can always derive your class in the dll from a base class that overloads new and delete. that way the proper new/delete will be called.
That's a really neat idea - I never thought of that

##### Share on other sites
Smart pointers should also take care of the problem. The pointer instance is on the caller's side, calling ref() and unref() which handle deallocation.

##### Share on other sites
thanks to all for the quick reply.
to doynax: Yes I try to delete the the pointern given by the creation function.

The overloaded new/delete method seems to work :)

thanks

##### Share on other sites
Quote:
 Original post by Shadowgrunzto doynax: Yes I try to delete the the pointern given by the creation function.
Yes, but are you casting it to the correct class first?
Deleting a raw void pointer will neither invoke the destructor nor placement delete.
I suppose you must be, otherwise you wouldn't have much use for the returned object. It was just a potential problem..