🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Another object handle trouble...

Started by
1 comment, last by Lbas 18 years, 6 months ago
Sorry, another thing that still confuse me... I've registered functions/methods which creates instances of some object. These functions can be both called from C & AS. My app allow user to run scripts to create these objects, but when the scripts ends, objects are deleted (while it shouldn't). It's look like : // in script cObject@ pObject; ... pObject = pMain.CreateAnObject(); My understanding is that CreateAnObject() don't call AddRef(), because it can be called from C too where calling AddRef() doesn't make sense. That means I would have to declare a script dedicated method (or maybe use autohandles), which would discord with the reason I found AS great (because you didn't have to declare script dedicated function like in most other languages). So whys the reason I get 2 Release() here (and so have to AddRef()) ? Thanks a lot for your work yet, Lbas
Lbas
Advertisement
The handle returned by CreateAnObject() is released, after it is no longer used. The handle assignment to the pObject variable calls AddRef, and this is released when the variable goes out of scope.

You can solve this in two ways:

1. Register the CreateAnObject() to return a reference to the object. Though this might not be intuitive and prevents you from returning null-handles.

RegisterObjectMethod("main", "cObject &CreateAnObject()", asMETHOD(main, CreateAnObject), asCALL_THISCALL);


2. Register the CreateAnObject() so that AngelScript automatically calls AddRef on the returned pointer. This is the reason why I implemented autohandles in the first place, to avoid having to create wrapper functions when dealing with object handles. The drawback is a slight overhead because of the extra dynamic processing needed by AngelScript, but in most cases you'll probably not notice a difference.

RegisterObjectMethod("main", "cObject @+ CreateAnObject()", asMETHOD(main, CreateAnObject), asCALL_THISCALL);


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

Ah ok, that's great.
I think I would prefer 2nd option, and no I don't think I'll see huge extra overhead :)

Thanks a lot for your time.
Regards,

Lbas
Lbas

This topic is closed to new replies.

Advertisement