🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Angelscript - C++ Object Destructor Not Getting Called

Started by
3 comments, last by GuyWithBeard 7 years, 10 months ago

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.

Advertisement

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?

What does asGetTypeTraits return?

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.

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.

This topic is closed to new replies.

Advertisement