Skip to main content
GameDev.net gamedev.net
🔒 Locked

Why angelscript doesn't call Release( ) ?

Started by zopenge Mar 3, 2008 at 4:37 AM 5 replies 1.2k views
Original Post
zopenge
zopenge
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 !
WitchLord
WitchLord
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.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel
zopenge
zopenge
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 :)
WitchLord
WitchLord
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
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel
zopenge
zopenge
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 ?
WitchLord
WitchLord
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
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel
zopenge
zopenge
Thanks very much ~~~ :-)

The problem solved ~~~ no memory leak now :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.