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

Null Pointer

Started by
2 comments, last by WitchLord 19 years ago
Hello all. At use AngelScript there were certain problems. I wraped a class on C ++, for use in a script. One of methods can return the null pointer at assignment, comparison or other operations with returned value the script throws out exception " access null pointer ". But in with C++ I can check up on Null and work further, in this case - only exception. Whether probably it to bypass or something to make? That I could work with null, at least compare?
Advertisement
You seem to be implementing some non-standard operator overloads.

By default, an assignment always returns a reference to the left hand object. In what situation do you need the assignment to return something else?

A comparison operator normally returns a bool type, why would you do something else?

I'm just trying to understand why AngelScript needs to support any other behaviour than the default. It is normally not recommended to implement overloaded operators that does something very different from the original intended behaviour, because it can confuse the programmer.

Can you show me some of your implementation that throws these exceptions. Maybe together we can find another way to implement it so that it doesn't break.

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

Probably I have badly explained. A code about the following
-----script---
void F ()
{
Control o;
Control test;
o. GetParent (); // exception is not thrown out
test=o. GetParent ();
/*member of asIScriptContext Execute () returns value asEXECUTION_EXCEPTION and the test of exception " Null pointer access "
*/
};
----------
Class Control a wrapper for C++ a class cControl at it is some members among which GetParent. In general C++ the code looks as follows
---------
class cControl
{
.....
cControl* GetParent ()
{
......
return FindParent (name); // the result can be null
}
};
......
engine-> RegisterObjectType ("Control", sizeof (cControl), asOBJ_CLASS);
engine-> RegisterObjectMethod (Name, " Control and GetParent () ", asMETHODPR (cControl, GetParent (), cControl *), asCALL_THISCALL);
......
/* Further call script*/
--------------

The question actually in that how to be if function returns null, whether it is possible to work with it: to check up on null for example. Ejection it does not arrange exception.
It would be desirable to see in a script similar to the following:
void F ()
{
Control o;
Control test;
if (o. GetParent () == null)
{
// some operations
return;
}
// oter some operations
};


Quote: Original post by WitchLord
Can you show me some of your implementation that throws these exceptions. Maybe together we can find another way to implement it so that it doesn't break.

Regards,
Andreas


Ok, I understand now.

What you need is to implement support for handles in your control class. Object handles are reference counted pointers, and they can be compared against null.

-----script---void F (){  Control o;  Control @test;  @test = o.GetParent(); // Get the handle to the parent control  if( test == null )  {    // Do something  }};


To have the control class support handles you need to implement the behaviours addref and release.

class Control{  Control()  {    refCount = 1;  }    Control *GetParent()  {    Control *p = FindParent(name);    p->AddRef(); // Increase the parents reference count for the returned handle    return p;  }  void AddRef()  {    refCount++;  }    void Release()  {    refCount--;    if( refCount == 0 )    {      delete this;    }  }   Control &operator=(const Control &other)  {    // Copy everything except the reference counter    ...    return *this;  }}


You'd register this class as follows:

int r;r = engine->RegisterObjectType("Control", sizeof(Control), asOBJ_CLASS_CDA); assert(r >= 0);r = engine->RegisterObjectBehaviour("Control", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(Control_Construct), asCALL_CDECL); assert(r >= 0);r = engine->RegisterObjectBehaviour("Control", asBEHAVE_ADDREF, "void f()", asMETHOD(Control, AddRef), asCALL_THISCALL); assert(r >= 0);r = engine->RegisterObjectBehaviour("Control", asBEHAVE_RELEASE, "void f()", asMETHOD(Control, Release), asCALL_THISCALL); assert(r >= 0);r = engine->RegisterObjectBehaviour("Control", asBEHAVE_ASSIGNMENT, "Control &f(const Control ∈)", asMETHOD(Control, operator=), asCALL_THISCALL); assert(r >= 0);r = engine->RegisterObjectMethod("Control", "Control @GetParent()", asMETHOD(Control, GetParent), asCALL_THISCALL); assert(r >= 0);


If you are not able to or don't want to change your cControl class implementation, then maybe you could use wrappers (take a look at how asCScriptString is implemented for an idea of how this would work). Also AngelScript (2.2.0+) can take care of some of the work with treating the reference counting by using the autohandles @+. If autohandles are used AngelScript will automatically increase or decrease the refcount as handles are passed to/from application functions.

Another solution would be to register a type that represents a pointer and for this implement operators that would allow your to compare with null. I don't recommend this solution however, as it could cause security risks with dangling pointers, i.e. pointers to objects that have already been deleted, unless handled carefully.

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

This topic is closed to new replies.

Advertisement