Sign in to follow this  
Canberk S

A Question of Array of Handles

Recommended Posts

Canberk S    115

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.)

Share this post


Link to post
Share on other sites
iraxef    451

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.

Share this post


Link to post
Share on other sites
Canberk S    115

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.

Share this post


Link to post
Share on other sites
WitchLord    4677

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();

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