🎉 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!

AS 2.0: handle as parameter sent to application function.

Started by
5 comments, last by WitchLord 19 years, 4 months ago
This is more like a documentation issue than a bug. When making an application side function to receive a handle to an object there is very strange (at first look) way of declaring that function. This is a simple cast function and you use it in the script like this: Obj1@ o1; Obj2@ o2; @o2 = @other_o2; o1 = @Cast2To1(@o2); Here is the engine function registration (the method that works): as_engine->RegisterObjectMethod(class_name, "Obj1@ Cast2To1(Obj2@ ∈ obj2)", asFUNCTION(func_pnt), asCALL_CDECL_OBJFIRST); which look very strange (Obj2@ ∈). Nowhere is documented that you should use that sort of declaration. The application function looks like this: Obj1* Cast2To1(Obj2 *&o2) { return (Obj1*)o2; } I've first tried this: "Obj1@ Cast2To1(Obj2@ obj2)" and received a NULL (zero) pointer in my application function, which was declared like this: Obj1* Cast2To1(Obj2 *o2) { // o2 is always NULL here !!! return (Obj1*)o2; } Don't know why you should declare a reference to a pointer as function argument (when for the Obj1 return handle you use a simple pointer and not *&) but this is the way is working. I think all these kind of issues should be included in the documentation. There is no example of sending a handle parameter to an application function (not even in the feature test sources).
Advertisement
The second case should work as well. I'll make some tests and see if I can find out why you are always receiving null. Since you say there is no examples of passing an object handle to a function I must have forgotten to test this case, which would explain how the bug could slip through.

Passing an object handle by reference to a function is useful when you want to return more than one value, or if you don't want to have to deal with addref/release in the function.

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

Bug fix:

Exchange the if expression on line 327 in as_callfunc_x86.cpp (CallSystemFunction() function):

if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].isExplicitHandle && !descr->parameterTypes[n].isReference )


The change is the addition of the check for isExplicitHandle.

I'll hopefully be able to upload 2.0.0a today with all of these latest bug fixes. Thank you for identifying the problems for me.

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

Quote: Original post by WitchLord
Passing an object handle by reference to a function is useful when you want to return more than one value, or if you don't want to have to deal with addref/release in the function.


So basically pasing a reference to a handle is faster since the addref/release functions are not called, right ?

Also, what I've noticed is that there are a lot of addref/release calls when (for me) it seems unnecessary. One example is when you call a method of a class through a handle.

Obj@ h;
@h = @o; // Addref, which is good.
h.FunctionCall(); // Addref on h before call, release after call, BUT WHY ?

Thanks
Licu

[Edited by - Licu on February 2, 2005 3:27:24 AM]
You would think passing a reference would be faster even in AngelScript, but it isn't. Because I have to make the script environment safe, I'm forced to make a copy of the object before passing it as a reference to the function. When the function returns the value stored in the object is copied back to the true object again. This is the reason for the in/out/inout keywords, which tell the script compiler which copy can be skipped.

The fastest way to pass an object to a function is with an object handle by value. The overhead of calling AddRef/Release is small compared to the copy of the object.

The addref before and release after method calls are also due to safety. I have to protect the object from being released before the method is using its attributes. It's quite easy to think of cases where this would cause possible crashes without the extra addref/release calls.

Should you feel that you don't need this safety then you can simply remove the extra calls to addref/release in the CallSystemFunction() in as_callfunc_x86.cpp.


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

Quote: Original post by WitchLord
The addref before and release after method calls are also due to safety. I have to protect the object from being released before the method is using its attributes. It's quite easy to think of cases where this would cause possible crashes without the extra addref/release calls.


I can figure out a case when this crashes unless you are referring to multithreading. Why a handle refcount will reach zero during my function call ?
I'm talking about an application function not a scripted one.
It depends on the method really. Say you have a method that manipulates an array of objects, and the object happens to be stored in this array. Should the method resize the array, it is possible that the object is released before the method returns.

This is obviously a very rare situation, but since it is possible I'll have to do something about it. Though I'd have to agree that the class method itself could add this protection if needed.

I'll think some more about this, maybe I'll decide to remove the extra protection again, and instead make it the responsibility of the application to provide that security.

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