Sign in to follow this  
GuyWithBeard

Angelscript - C++ Object Destructor Not Getting Called

Recommended Posts

Hey,

 

I have a feeling I am doing something wrong. My code technically works but my destructor isn't called when I expect it to be so I figured I would check with you just to be sure.

 

So I have a small value type class that is exposed to AS. I looked at the registration code for std::string in the string addon and pretty much copied it for my own type. The registration is done like this:

namespace
{
    void construct(StringHash h, GameObjectRef* mem)
    {
        new(mem) GameObjectRef(h);
    }

    void destruct(GameObjectRef* pointer)
    {
        pointer->~GameObjectRef();
    }
}

int r = scriptEngine->RegisterObjectType("gameobject", sizeof(GameObjectRef), asOBJ_VALUE | asGetTypeTraits<GameObjectRef>()); ASSERT(r >= 0);
r = scriptEngine->RegisterObjectBehaviour("gameobject", asBEHAVE_CONSTRUCT, "void f(uint)", asFUNCTION(construct), asCALL_CDECL_OBJLAST); ASSERT(r >= 0);
r = scriptEngine->RegisterObjectBehaviour("gameobject", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(destruct), asCALL_CDECL_OBJLAST); ASSERT(r >= 0);

I then have a small function in AS which is run every frame:

void test()
{
    gameobject test("123");
}

With this setup the construct() function is run and the object is properly created but the destruct() function is never run, not even when shutting down the app. I tried registering the type with the POD flag too, but that did not help. I am using AS version 2.30.2.

Share this post


Link to post
Share on other sites

And, for the record, when creating a temporary string in a similar fashion in AS the DestructString() function is run. I have a hard time determining what the difference between my case and the string case is.

 

Any ideas?

Share this post


Link to post
Share on other sites

What does asGetTypeTraits return?

 

Stepping into the function with the debugger shows that it correctly detects the constructor, destructor, assignment op and copy constructor. So that part should be ok.

 

EDIT: So to answer your question it returns asOBJ_APP_CLASS | asOBJ_APP_CLASS_CONSTRUCTOR | asOBJ_APP_CLASS_DESTRUCTOR | asOBJ_APP_CLASS_ASSIGNMENT | asOBJ_APP_CLASS_COPY_CONSTRUCTOR.

Edited by GuyWithBeard

Share this post


Link to post
Share on other sites

Curious, I just checked the code again and now it works. To my knowledge, the only thing I added since starting this topic was to add the assignment operator (for other reasons), but commenting that out does not seem to stop the destructor from being called.

 

I honestly don't know what made it work but I will let you know what the problem was if I figure it out.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this