Advertisement

reference counting problem

Started by March 08, 2009 11:57 AM
6 comments, last by WitchLord 15 years, 8 months ago
hello again! I've been recently trying to plug all memory leaks releated to AS in our game. Here's the problem I've encountered. Imagine the following object method:
cVec3 @getOrigin();
which creates a new cVec3 object and returns a handle to it. Now, whenever such object is passed as an argument to a function or method, the reference counter for it is never decremented:

cVec3 @origin = target.getOrigin();
if ( ( G_PointContents( @origin ) & CONTENTS_NODROP ) == 0 )
...

Now, if I comment out everything below the getOrigin() call, no memory leaks are detected.
Ok, the problem seems to be solved by changing the declaration of G_PointContents from

int G_PointContents( cVec3 &origin )

to
int G_PointContents( cVec3 @origin )


I still don't get it though :/
Advertisement
Manual: Object handles


It's responsibility of the called function to release the handles that it receives by parameters. Apparently your G_PointContents function doesn't do that.

Changing the registration to take the handle by reference is one way of solving it. Another option would be to register the function and tell AngelScript to release the handle after the function returns, this is done with the autohandles, e.g:

int G_PointContents( cVec3 @+ origin )



Regards,
Andreas

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

argh, that explains everything. thanks!
To clear it up, let's say I have a method that takes three object handles as parameters, either of which can be optional (null). Any not-null variables passed by handle will be modified, based on the provided by the method calling object. What is the proper way to declare such function to prevent passed handles from being leaked?
void angleVectors (cVec3 @+, cVec3 @+, cVec3 @+))

sample call:
cVec3 in, fwd, up;...in.angleVectors (fwd, null, up))
Both reference and auto handles will work. The difference between the two is that with the first AngelScript will guarantee that an actual reference is passed in, and not a null pointer.

Performance wise, passing by reference is likely the fastest, unless your function implementation can skip a lot of processing by receiving null pointers.

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

Advertisement
Well, using handles as-is (without the autodeallocation "+") leaks (correct me if I'm wrong) and using references is surely going to be slower in case only one or two of the three vectors are needed.
Yes, if you register the function as taking handles without the auto-release, then the arguments are going to leak, unless you manually release the handles.



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

This topic is closed to new replies.

Advertisement