Advertisement

asOBJ_REF without AddRef and Release?

Started by December 09, 2011 12:58 PM
10 comments, last by WitchLord 12 years, 9 months ago
Is it possible, to register object type as asOBJ_REF (to be able to pass pointers to script) but without AddRef() and Release() behaviours?

We are not going to create objects of this type in scripts, we just want to get handles to the objects created by our game engine and call some methods/set properties etc. on them.
We wanted to avoid additional function calls to AddRef and Release because we simply don't need it in our case.

Regards
virious
You can easily use dummy functions and register them as your AddRef and Release behaviors. I use this for a global Math object that provides functions I would normally register as static member functions in C++.


class SomeGlobalScriptObject
{
void Dummy_AddRef() { /* do nothing */ }
void Dummy_Release() { /* do nothing */ }
};


Just remember that you are responsible for the object you register when you do this. There is really no way to avoid the AddRef and Release calls as the engine uses it for housecleaning, but unless you are working on something reeeeeeeally CPU sensitive (or are extremely anal about unnecessary function calls) it should be a non-issue.
Rantings, ravings, and occasional insight from your typical code-monkey: http://angryprogrammingprimate.blogspot.com/
Advertisement
I've added the new flag asOBJ_NO_ADD_RELEASE and modified the AngelScript code.

Here is diff file (http://pastebin.com/CAMmy6qG) generated from my modifications. I was using AS revision number 1073.
I've also added and changed this:


asOBJ_NO_ADD_RELEASE = 0x20000,
asOBJ_MASK_VALID_FLAGS = 0x3FFFF,

in angelscript.h.


Now the AddRef and Release are no longer required (and called) for the object type registered as asOBJ_REF | asOBJ_NO_ADD_RELEASE.

Is this a correct solution to my problem?

Regards
virious
I'll take a look at the changes you made. I was already thinking about adding a similar option for those who really don't want to use the addref/release behaviours.

Though I would probably call the flag asOBJ_APPMANAGED instead, since the application will have to do the memory management by itself.

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


I'll take a look at the changes you made. I was already thinking about adding a similar option for those who really don't want to use the addref/release behaviours.

Though I would probably call the flag asOBJ_APPMANAGED instead, since the application will have to do the memory management by itself.


It's great to hear that. If you want, just modify my code and I think it should work for this case :).

Regards
virious
Forgot about one thing. In case asBC_FREE: in as_context.cpp I've added:

if (!(objType->flags & asOBJ_NO_ADD_RELEASE))
before

*a = 0;
to prevent AngelScript from clearing object managed by my application.

Regards
virious
Advertisement
I've added this in revision 1165.

I ended up using the name asOBJ_NOCOUNT for this support, as it was shorter and more meaningful than the other options.


Thanks,
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

Does this flag also make objects immune to garbage collecting? Btw, I think it should not be possible to create an object of such class through issuing a 'new' command in the scripts.

I've added this in revision 1165.

I ended up using the name asOBJ_NOCOUNT for this support, as it was shorter and more meaningful than the other options.


Thanks,
Andreas

Great, I will test it :).

Does this flag also make objects immune to garbage collecting? Btw, I think it should not be possible to create an object of such class through issuing a 'new' command in the scripts.


Sorry, I missed this question.

This new flag just mean that angelscript wont notify the application about references it holds. So in effect it puts more responsibility on the application for making the proper memory management.

You can prevent the script from creating objects by not registering the factory behaviours.

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