Skip to main content
GameDev.net gamedev.net
🔒 Locked

asOBJ_REF without AddRef and Release?

Started by virious Dec 9, 2011 at 12:58 PM 10 replies 3.4k views
Original Post
virious
virious
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
3kimr
3kimr
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.
virious
virious
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
WitchLord
WitchLord
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 - <a href="http://www.angelcode.com/tower" rel
virious
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.


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

Regards
virious
virious
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
WitchLord
WitchLord
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 - <a href="http://www.angelcode.com/tower" rel
_Vicious_
_Vicious_
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.
virious
virious

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

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 - <a href="http://www.angelcode.com/tower" rel
_Vicious_
_Vicious_
So, for such object handles it's not necessary to manually increase the reference counter when returning them as function values, right?
WitchLord
WitchLord
Of course. Most likely the object won't even have a reference counter, because if it had it would likely be best to just register it without the NOCOUNT anyway
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.