• Advertisement
Sign in to follow this  

I don't understand the documentation for auto handles.

Recommended Posts

I'm trying to figure out the purpose of the '@+' token in Angelscript, this [http://www.angelcode.com/angelscript/sdk/docs/manual/doc_obj_handle.html] lists them as auto handles near the bottom of the page, and if I'm reading it right, then this in action would look like this:

Obj@+ ChooseObj(Obj@+, Obj@+);

void main()
{
    Obj@ A, B;

    @A = new Obj();
    @B = new Obj(); 
    
// at this point A and B have a refcount of 1.
    Obj@ C = ChooseObj(A, B);
//Let C = B, so A got decref'd and was deleted 

// this should be true, because while 
// the A's refcount hit 0, and it was
// deleted; the pointer wasn't reassigned.
    if(A !is null)
// this should crash from an invalid pointer
        A.doMethod();
}

Meanwhile, B got decref'd and incref'd, so it has a refcount of 1 when it gets returned, then gets addrefed again when it gets assigned to C. So C/B correctly has a value of 2, but that seems to be more a coincidence than anything.

In short I feel like I must be reading this wrong, because I can't think of any practical upshot of this behavior. What is it talking about?

EDIT: clarification

Edited by Geat_Masta

Share this post


Link to post
Share on other sites
Advertisement

Basically the auto handles were implemented to attempt to reduce the amount of work that the developer has to do to bind functions with the AngelScript engine.

Instead of manually having to decrease handles received from the script in parameters, the function can be registered using auto handles so that AngelScript will do the decrease automatically. The same thing for returned handles, instead of having to manually increase the handle that will be returned the function can be registered with auto handle so AngelScript will automatically increase the reference counter.

To the script there is no difference at all as it happens behind the scene.

Share this post


Link to post
Share on other sites

Are you implying that when handles are put on the stack they get AddRef'd? If so that could be explained better.

Edited by Geat_Masta

Share this post


Link to post
Share on other sites

Every reference to the object is counted, whether it is from a local variable on the stack, or an argument sent to a function, or member of a class, etc.

To the script writer this is all transparent and he doesn't have to care about it.

How the reference counter should be managed by application is explained on the same page under Managing the reference counter in functions.

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  

  • Advertisement