Sign in to follow this  
zopenge

Why angelscript doesn't call Release( ) ?

Recommended Posts

r = engine->RegisterObjectType( "Matrix2", sizeof( ScriptMatrix2 ), asOBJ_REF ); r = engine->RegisterObjectBehaviour( "Matrix2", asBEHAVE_FACTORY, "Matrix2 @f()", asFUNCTION( ScriptMatrix2DefaultFactory ), asCALL_CDECL ); r = engine->RegisterObjectBehaviour( "Matrix2", asBEHAVE_FACTORY, "Matrix2 @f(float, float, float, float)", asFUNCTION( ScriptMatrix2Factory ), asCALL_GENERIC ); r = engine->RegisterObjectBehaviour( "Matrix2", asBEHAVE_ADDREF, "void f()",asMETHOD( ScriptMatrix2, AddRef ), asCALL_THISCALL ); r = engine->RegisterObjectBehaviour( "Matrix2", asBEHAVE_RELEASE, "void f()", asMETHOD( ScriptMatrix2, Release ), asCALL_THISCALL ); // AS Code : Matrix2 m( 0, 0, 0, 0 ); But it had memory leak, Release of Matrix2 didn't been called. I dont't know why, Thanks very much guys !

Share this post


Link to post
Share on other sites
Can you show us the implementation of the ScriptMatrix2 class, and the factory functions? The problem is most likely with the implementation of the class and or factory functions.

Share this post


Link to post
Share on other sites
ScriptMatrix2::ScriptMatrix2( )
{
// Increase reference
IncreaseRefCount( );
}

ScriptMatrix2::ScriptMatrix2( _float m00, _float m01, _float m10, _float m11 ) : Matrix2( m00, m01, m10, m11 )
{
// Increase reference
IncreaseRefCount( );
}

ScriptMatrix2::~ScriptMatrix2( )
{
EGE_ASSERT( GetRefCount( ) == 0 );
}

//-------------------
// reference counting
//-------------------

_void ScriptMatrix2::AddRef( )
{
IncreaseRefCount( );
}

_void ScriptMatrix2::Release( )
{
// Decrease reference
DecreaseRefCount( );

if ( GetRefCount( ) == 0 )
delete this;
}

//-----------------------
// AngelScript functions
//-----------------------

static ScriptMatrix2* ScriptMatrix2DefaultFactory( )
{
return new ScriptMatrix2( );
}

static ScriptMatrix2* ScriptMatrix2Factory( asIScriptGeneric* gen )
{
return new ScriptMatrix2( gen->GetArgFloat( 0 ), gen->GetArgFloat( 1 ), gen->GetArgFloat( 2 ), gen->GetArgFloat( 3 ) );
}

_void ScriptMatrix2::Register( asIScriptEngine* engine )
{
_long r;

// Register the type
r = engine->RegisterObjectType( "Matrix2", sizeof( ScriptMatrix2 ), asOBJ_REF ); EGE_ASSERT( r >= 0 );

r = engine->RegisterObjectBehaviour( "Matrix2", asBEHAVE_FACTORY, "Matrix2 @f()", asFUNCTION( ScriptMatrix2DefaultFactory ), asCALL_CDECL ); EGE_ASSERT( r >= 0 );
r = engine->RegisterObjectBehaviour( "Matrix2", asBEHAVE_FACTORY, "Matrix2 @f(float, float, float, float)", asFUNCTION( ScriptMatrix2Factory ), asCALL_GENERIC ); EGE_ASSERT( r >= 0 );

r = engine->RegisterObjectBehaviour( "Matrix2", asBEHAVE_ADDREF, "void f()", asMETHOD( ScriptMatrix2, AddRef ), asCALL_THISCALL ); EGE_ASSERT( r >= 0 );
r = engine->RegisterObjectBehaviour( "Matrix2", asBEHAVE_RELEASE, "void f()", asMETHOD( ScriptMatrix2, Release ), asCALL_THISCALL ); EGE_ASSERT( r >= 0 );

// Register methods

}


Thanks for help :)

Share this post


Link to post
Share on other sites
Where do you initialize the refCount? You must set the refCount to 0 in the constructor before you call the IncreaseRefCount() method.


ScriptMatrix2::ScriptMatrix2( )
{
refCount = 0;

// Increase reference
IncreaseRefCount( );
}


That's the only problem I could see.

Have you tried debugging the code? When the factory functions return the reference counter should be 1, to account for the pointer that is being returned.

Regards,
Andreas

Share this post


Link to post
Share on other sites
I use Matrix2 matrix, without parameters, nothing memory leak happened. Release( ) functions is called. But with parameters, Release( ) is not called.

refCount is zero value when Factory Functions call. Initialize between class Construction.

Is other problem in my code ?

Share this post


Link to post
Share on other sites
Just saw your problem. You're using the generic calling convention incorrectly.

This is what it should be:


static void ScriptMatrix2Factory( asIScriptGeneric* gen )
{
ScriptMatrix2 *mtx = new ScriptMatrix2( gen->GetArgFloat( 0 ), gen->GetArgFloat( 1 ), gen->GetArgFloat( 2 ), gen->GetArgFloat( 3 ) );

*(ScriptMatrix2**)gen->GetReturnPointer() = mtx;
}


Regards,
Andreas

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