Sign in to follow this  
zopenge

I found an reference bug in angelscript.

Recommended Posts

class A { A& do( ); }; A& A::do( ) { ... do something. .. } AS Code : { A a; a.do( ); } I use asOBJ_REF to count the object used number. Release() will call 2 times. ( 1 time is correct, My refcount is 1 )

Share this post


Link to post
Share on other sites
I haven't been able to reproduce this problem.

This is the class I wrote to test it:


class CRefClass
{
public:
CRefClass()
{
refCount = 1;
}
~CRefClass()
{
}
int AddRef()
{
return ++refCount;
}
int Release()
{
int r = --refCount;
if( refCount == 0 ) delete this;
return r;
}
CRefClass &Do()
{
return *this;
}
int refCount;
};

CRefClass *Factory()
{
return new CRefClass;
}


And this is how I register it:


r = engine->RegisterObjectType("refclass", sizeof(CRefClass), asOBJ_REF); assert(r >= 0);
r = engine->RegisterObjectBehaviour("refclass", asBEHAVE_FACTORY, "refclass@ f()", asFUNCTION(Factory), asCALL_CDECL); assert(r >= 0);
r = engine->RegisterObjectBehaviour("refclass", asBEHAVE_ADDREF, "void f()", asMETHOD(CRefClass, AddRef), asCALL_THISCALL); assert(r >= 0);
r = engine->RegisterObjectBehaviour("refclass", asBEHAVE_RELEASE, "void f()", asMETHOD(CRefClass, Release), asCALL_THISCALL); assert(r >= 0);
r = engine->RegisterObjectMethod("refclass", "refclass &Do()", asMETHOD(CRefClass,Do), asCALL_THISCALL); assert(r >= 0);


This only calls Release 1 time:


r = engine->ExecuteString(0, "refclass ref; ref.Do()");


Did you perhaps register the Do() method with "refclass @Do()"? That would explains why AngelScript called Release() twice in your case.

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