Jump to content

  • Log In with Google      Sign In   
  • Create Account


- - - - -

A Question of Array of Handles


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 jnbrq   Members   -  Reputation: 115

Like
0Likes
Like

Posted 06 February 2014 - 12:34 PM

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



Sponsor:

#2 iraxef   Members   -  Reputation: 303

Like
0Likes
Like

Posted 06 February 2014 - 10:00 PM

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.



#3 jnbrq   Members   -  Reputation: 115

Like
0Likes
Like

Posted 07 February 2014 - 01:35 AM

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.



#4 Andreas Jonsson   Moderators   -  Reputation: 3239

Like
0Likes
Like

Posted 07 February 2014 - 07:28 AM

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

#5 jnbrq   Members   -  Reputation: 115

Like
0Likes
Like

Posted 07 February 2014 - 03:04 PM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS