Wrapping Virtual Destructors

BlitzMax Forums/BlitzMax Programming/Wrapping Virtual Destructors

Gabriel(Posted 2006) [#1]
This one is kicking my butt a little. One of the TV3D classes I'm wrapping has a virtual destructor. I wrap it like this :

extern "C" __cdecl void CTVMesh_Destroy(CTVMesh* Ha) {
	delete Ha;
}


But I get an access violation when I call it.

PLEASE NOTE THESE TWO THINGS :

1) The pointer is valid.

2) The actual code that's calling this is perfect. It's a straight BMax copy of a C++ demo which works fine.

So I'm quite certain that it's this virtual destructor which is getting in the way. I thought I could get around it by downcasting to the derived class, but apparently there are no public derived classes. The class is just a wrapper for some other internal stuff.

Anyway, the author has kindly offered to add another way of doing it if neither of us can come with a better solution. So I'm just posting here to see if anyone has a better solution before I ask him to add anything else.


Chris C(Posted 2006) [#2]
try putting the CTVMesh_Destroy call in the delete method of a max's type

it works fatastically well with my gui module...


Gabriel(Posted 2006) [#3]
Eh? Where I'm calling it from is not the problem. It's within the C++ snippet I've posted above that it crashes.


gman(Posted 2006) [#4]
one thing to try at least would be to wrap it up with a quick validity check.
extern "C" __cdecl void CTVMesh_Destroy(CTVMesh* Ha) {
	if (Ha)
		delete Ha;
}

if that works, then i would look at if its possible that its being called twice. this of course assumes that in your code somewhere after calling this you are setting the handle back to null on the BMAX side.


Gabriel(Posted 2006) [#5]
It's in the Delete() Method as Chris suggests, so it can't be called more than once and the pointer is already nulled before the Delete() method gets called.

The pointer is valid, I've checked. I have also added the checking you suggest, just for testing purposes and it zips right past the error checking and crashed anyway. So I'm pretty sure it's the Destructor and not the pointer.


Gabriel(Posted 2006) [#6]
Sorry guys, I've been giving out wrong information. I changed the demo and forgot about it. I'll blame it on too many 5AM work sessions.

I don't create the object in my wrapper, it is returned from the method of another Class. EG:

MyObject2->CreateObject1() - returns the MyObject.

This apparently is what is making all the difference.

If I code the entire app in C++ I can delete it just fine. But not this way because another class in the DLL created it. Is there another way of deleting it?


N(Posted 2006) [#7]
You could add delete code to the other class (assuming you have the option of doing so), for one.

Beyond that, I just don't think there's any way I can provide a better response.


Gabriel(Posted 2006) [#8]
I think I'm gonna give this up and wait for Sylvain to look into this. I've found another class which is created exactly the same way ( through another class ) and it can be deleted just fine.