A Question of Array of Handles

Started by
3 comments, last by Canberk S 10 years, 2 months ago

Let's consider that I have C++ function which is returning an array of handles of an registered reference type (with reference counting). The object type registered in script engine is "ref" and in C++ it's CRef

The function is registered as "array<ref@>@ CreateArrayOfHandles"

it's looking like this :


CScriptArray *CreateArrayOfHandles()
{
asIScriptContext *ctx = asGetActiveContext();
if( ctx )
{
asIScriptEngine* engine = ctx->GetEngine();
asIObjectType* t = engine->GetObjectTypeById(engine->GetTypeIdByDecl("array<ref@>"));
CScriptArray* arr = new CScriptArray(3, t);
for( unsigned int i = 0; i < arr->GetSize(); i++ )
{
CRef *ptr = new CRef;
arr->SetValue(i, &ptr);
}
return arr;
}
return 0;
}

First, Is it good ?

Second, Will the CRef objects destroyed automatically by script engine ? (CRef is registered as reference type with AddRef and Release functions are registered.)

Advertisement

I have a nearly-identical function, except my array holds a type directly (e.g. "array<MyClass@>") and that type is not ref-counted.

I can tell you that in my case, I've set breakpoints in the CScriptArray destructor and it seemed to be hit, as you'd think.

If you look at the code for CScriptArray::Destruct(), it does look like it's calling ReleaseScriptObject() for every object in the array.

I've put cout << "CRef::~Cref()" << endl; to the destructor of CRef. But nothing is printed to the stdout. I have started a memory checking program; and I had a lot of memory leaks about CRef.

You memory leak is here:

CRef *ptr = new CRef;
arr->SetValue(i, &ptr);
, i.e

SetValue() will increase the reference count of the CRef object, so you need to release the reference that was accounted for in the constructor, i.e.

CRef *ptr = new CRef;
arr->SetValue(i, &ptr);
ptr->Release();

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

It worked !! Thank you very much biggrin.png !!!

This topic is closed to new replies.

Advertisement