Jump to content
  • Advertisement
Sign in to follow this  

new / delete within dll

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by chollida1
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.
That's a really neat idea - I never thought of that

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Shadowgrunz
to 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..

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!