Advertisement

question about timing of call destructor

Started by March 23, 2010 10:11 AM
3 comments, last by hoboaki 14 years, 8 months ago
I have a question about timing of call destructor. Please look at a script code and a output. //----------------------------------------------------------- // script code

class ObjType
{
};

class HaveObj
{
    HaveObj()
    {
        Print( "HaveObj::ctor\n" );
    }
    ~HaveObj()
    {
        Print( "HaveObj::dtor\n" );
    }
    ObjType o;
};

class HaveHandle
{
    HaveHandle()
    {
        Print( "HaveHandle::ctor\n" );
    }
    ~HaveHandle()
    {
        Print( "HaveHandle::dtor\n" );
    }
    ObjType@ o;
};

void testCase1()
{
    Print( "testCase1 start\n" );
    {
        HaveObj o;
    }// call o.~HaveObj();
    {
        HaveHandle o;
    }// not call o.~HaveHandle();
    
    Print( "testCase1 end\n" );
} 

void main()
{
    testCase1();
}
//----------------------------------------------------------- //----------------------------------------------------------- // output

testCase1 start
HaveObj::ctor
HaveObj::dtor
HaveHandle::ctor
testCase1 end
//----------------------------------------------------------- I expect that AngelScript call HaveHandle's destructor as well as HaveObj. Why don't AngelScript call HaveHandle's destructor as well as HaveObj?
As with any language with automatic memory management, it can be hard to predict when the destructor is called.

In your case you have created a situation where the ordinary reference counting is not enough to control the life time of the objects, so the garbage collector is notified to keep a watch on the object instance. However, the garbage collector is not invoked automatically by the script engine (so that the application will have full control of when that is done). This means that unless you manually call the GC, the object will only be destroyed when you release the script engine.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
Thank you for your reply.

I have another question about AngelScript's implementation.

In this case.
・ObjTypeFlags of HaveObj is asOBJ_REF.
・ObjTypeFlags of HaveHandle is asOBJ_REF | asOBJ_GC.
Is this correct?

[Edited by - hoboaki on March 25, 2010 9:15:45 AM]
Yes. That is basically the difference.

The compiler can see that HaveObj has no way of forming circular references, so it will not need full garbage collection as the basic reference counting is enough.

For the HaveHandle there is a chance that the ObjType handle holds a reference back to the HaveHandle object, e.g. if the handle points to an object type that derives from the ObjType and includes the handle to HaveHandle.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thanks for an intelligible explanation.

I have been convinced about the difference in operation of HaveObj and HaveHandle.
Thank you!

This topic is closed to new replies.

Advertisement