Jump to content

  • Log In with Google      Sign In   
  • Create Account

- - - - -

Registering an object property


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 BlackMoons   Members   -  Reputation: 439

Like
0Likes
Like

Posted 17 September 2013 - 05:07 PM

Hi, I am having a small problem.

I have 'ObjectRef' registered as a reference type

It has the standard ref counting/addref/release functionality.

I also have a copy constructor registered as:

r = scriptEngine->RegisterObjectMethod("ObjectRef", "ObjectRef@ opAssign(const ObjectRef &in other)", asMETHOD(ObjectRef,operator=), asCALL_THISCALL); assert( r >= 0 );

 

And right now I am encountering two problems...

I have an angelscript class like this:

class FireSource
{
    ObjectRef @ mOwner;
    Light mLight;
    FireSource()
    {
        gConsole.AddItem("Constructed!");
    }

    void Initilize(ObjectRef @ ref)
    {
        gConsole.AddItem("Testing!");
        mOwner = ref;
        gConsole.AddItem("Testing!1.01");
        mLight.mOwner = ref;
        gConsole.AddItem("Testing!1.1");
    }
};

I construct the class on my C++ side and call Initilize:


        asIScriptFunction * init = objDetails->mUserDataType->GetMethodByDecl("void Initilize(ObjectRef @)");

        Engine::mScript->mContext->Prepare(init);
        ObjectRef * ref = new ObjectRef();
        ref->SetObject(this);
        Engine::mScript->mContext->SetArgObject(0, ref);
        Engine::mScript->mContext->SetObject(mObjectData);
        // Execute the call
        Engine::mScript->mContext->Execute();
        ref->Release();

I can confirm the object is constructed, and the first 'gConsole.AddItem("Testing!");' is executed.

After that, it returns from "Engine::mScript->mContext->Execute();" without calling the ObjectRef copy assignment or any further instructions, No error is logged or reported?

 

If I comment out '//mOwner = ref;', it continues on to call the ObjectRef copy constructor for the line "mLight.mOwner = ref;"

 

And then it crashes. (MSVC2010)

"File crt\src\dbgdel.cpp line 52

Expression _BLOCK_TYPE_IS_VALID(pHead->nBlockUSE)"

 

 

For reference, the C++ side of Light is (its also registered as ref type)

class Light : public ScriptRef
{
public:
    ObjectRef mOwner;
};
...
r = scriptEngine->RegisterObjectProperty("Light", "ObjectRef mOwner", asOFFSET(Light,mOwner)); assert( r >= 0 );

So yea, two problems, I am not exactly sure what I did wrong.


Edited by BlackMoons, 17 September 2013 - 05:25 PM.

Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com


Sponsor:

#2 BlackMoons   Members   -  Reputation: 439

Like
0Likes
Like

Posted 17 September 2013 - 05:30 PM

PS, why does virtual int asIScriptContext::SetObject(void*obj) take a void*? Shouldn't it take a asIScriptObject* instead for slightly more type safety?


Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com


#3 BlackMoons   Members   -  Reputation: 439

Like
0Likes
Like

Posted 17 September 2013 - 05:59 PM

I think I figured out the first problem was the line should of been '@mOwner = @ref;'

But I am still wondering about the crash when assigning "mLight.mOwner = ref;"


Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com


#4 BlackMoons   Members   -  Reputation: 439

Like
0Likes
Like

Posted 17 September 2013 - 07:47 PM

Oh, Here a call stack on the crash:

     Client.exe!operator delete(void * pUserData)  Line 52 + 0x51 bytes    C++
     Client.exe!ObjectRef::`scalar deleting destructor'()  + 0x3c bytes    C++
     Client.exe!ScriptRef::Release()  Line 127 + 0x34 bytes    C++
     Client.exe!asCScriptEngine::CallObjectMethod(void * obj, asSSystemFunctionInterface * i, asCScriptFunction * s)  Line 3568 + 0x8 bytes    C++
     Client.exe!asCScriptEngine::CallObjectMethod(void * obj, int func)  Line 3522    C++
     Client.exe!asCContext::ExecuteNext()  Line 2522    C++
     Client.exe!asCContext::Execute()  Line 1144 + 0x8 bytes    C++

It seems to be calling release on mLight.mOwner but why would it do that?

<edit> Did some more testing. Increasing mOwner's refcount in mLight's constructor didn't help anything. So I have no idea what ObjectRef it is releasing or why.

<edit> Updated to 2.27.1, Still crashes.

Adding AddRef() to ObjectRef's constructor solves the crash, but most likely results in memory leaks.

Incrementing ref's ref count or Light.mOwner's ref count did not fix the crash.

<edit>

Ok, I think I figured it out..

the copy constructor was defined like "ObjectRef@ opAssign(const ObjectRef &in other)" meaning it returned a handle that would then be released since my script does not use it right? And my copy constructor if defined like that should increment the handle it returns?

 

Changing it to "ObjectRef& opAssign(const ObjectRef &in other)" seems to have fixed it. But I am wondering is this the correct fix? Or does my copy constructor need to increment the handle it returns instead? I would rather not make duplicate copy constructors for C++ and Angelscript.


Edited by BlackMoons, 17 September 2013 - 11:09 PM.

Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com


#5 Andreas Jonsson   Moderators   -  Reputation: 3366

Like
0Likes
Like

Posted 18 September 2013 - 11:05 AM

There are lots of questions and errors (some of it you appear to have figured out on your already). Let me try to address them all in this post in no specific order.

 

 

1. The script code should probably do handle assignments rather than value assignments. With value assignment the script will attempt to call the opAssign method of the left-hand object, but if the handle in the left-hand object is null then you'll get a script exception. With a handle assignment you set the left-hand handle to refer to the same object given in the right-hand expression.

 

    void Initilize(ObjectRef @ ref)
    {
        gConsole.AddItem("Testing!");
        @mOwner = ref;
        gConsole.AddItem("Testing!1.01");
        @mLight.mOwner = ref;
        gConsole.AddItem("Testing!1.1");
    }

 

2. You are not checking the return code on the context method calls. If you did you would most likely see that the Execute() method returns asEXECUTION_EXCEPTION ( == 3 ), rather than asEXECUTION_FINISHED ( == 0 ). When Execute() returns asEXECUTION_EXCEPTION the script execution failed, and you can use the methods on the context to get information about the location of the error (see e.g. the PrintException helper function in the add-ons).

 

3. Is the Light::mOwner on the C++ side really an instance of the ObjectRef type? Shouldn't it be a pointer to an ObjectRef type? If ObjectRef is a reference counted type, then you should probably not be using it a a value type. Otherwise you may get dangling pointers as the ObjectRef instance can be destroyed while there are still active references to it. Or in your case, it appears that the Release method of ObjectRef is attempting to delete the member of the Light object, which causes the crash as the ObjectRef wasn't allocated by itself.

 

4. asIScriptContext::SetObject(void*obj) takes a void* because it is not always asIScriptObject pointers that are given. You can use asIScriptContext to call object methods on registered types too.

 

5. opAssign is the assignment operator, not the copy constructor. The copy constructor for ObjectRef would look something like this (if you have it):

 

ObjectRef::ObjectRef(const ObjectRef &other)  
{
   // Set the refCount of the new instance to 1
   refCount = 1;
  
   // Copy the content from the other instance (except the refCount)
   ...
}

 

6. Normally the opAssign method should be declared to return the object by reference, and not by handle. If you register it to return the object by handle, then the C++ operator= method must increase the refCount of the before returning, which I assume isn't what you want.

 

 

 

I recommend you read the following entries in the manual to get a better understanding on how handles work:

 

 

Let me know if this clears up your doubts or if you need any further explanations.

 

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

#6 BlackMoons   Members   -  Reputation: 439

Like
0Likes
Like

Posted 18 September 2013 - 11:13 PM

1: Ah.

 

2: Great, I'll work on a system for wrapping all my executes into my error reporting. Man even run time error reporting, Angelscript is great!

 

3: Yes its really an instance of ObjectRef, I thought making it an instance would make things a little cleaner.. One less thing to allocate and keep track of. The problem was returning handle of the ObjectRef in opAssign causing it to deincrement and release itself. mLight never tries to release its mOwner because it knows its local and just needs to be cleaned up as local. You do bring up a valid point about dangling pointers however. I might look into changing that for that reason.

 

4: I can call my C++ code via angelscript via C++ by passing registered objects instead of Angelscript objects? I have no idea why I would want to do that, but that seems cool. Very unified. Wait... That means I can transparently use registered C++ classes or user designed Angelscript classes on a class by class basis?... Awesome!

 

5: sorry called it the wrong thing. Meant assignment operator.

 

6: Ahhh ok. I copied the opAssign prototype from the string registration function, but that is a value type so I guess it was a bad prototype to copy. I have read the entire Angelscript documentation and thank you for taking the time to write it all. Its just the difference between handles, references, value types and the exact details on ref counting is taking a little time to sink in.

 

Some details of the documentation are kind of hidden on only one page and that makes it a little harder to use as a quick reference. While I understand you don't want to be redundant in your documentation, Maybe some more hyperlinking between related pages would help? "See also:" section maybe?

 

Is there any difference in returning a handle instead of a reference other then angelscript will decrement a handles ref count when it expires? I assume if one takes a handle of the returned ref, its ref count will be incremented?

 

Are references there mainly to support C++'s typical 'ignorance' of ref counting?


Edited by BlackMoons, 18 September 2013 - 11:16 PM.

Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com


#7 Andreas Jonsson   Moderators   -  Reputation: 3366

Like
0Likes
Like

Posted 19 September 2013 - 10:21 AM

I try to use 'see also' links in the documentation. But I really am a programmer first hand, and as most programmers I'm not really that good at writing documentation. Any feedback on where improvements is needed is most welcome. :)

 

Perhaps the real difference between handles and references is that handles can be null while references cannot, and handles can be reassigned and references cannot. This is similar to pointers and references in C++.


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

#8 BlackMoons   Members   -  Reputation: 439

Like
0Likes
Like

Posted 19 September 2013 - 05:23 PM

Oh trust me, you are amazing at writing documentation. This is by far the best documented open source library I am using. I love going to read Angelscript documentation because it usually answers my question and even has little source tidbits showing usage.

 

But as a tip, Maybe http://www.angelcode.com/angelscript/sdk/docs/manual/doc_script_handle.html could link back to http://www.angelcode.com/angelscript/sdk/docs/manual/doc_obj_handle.html

 

One links to the other, but no links back, in fact no links leave that page at all.


Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com


#9 Andreas Jonsson   Moderators   -  Reputation: 3366

Like
0Likes
Like

Posted 19 September 2013 - 06:22 PM

Thanks. I believe the documentation is only this detailed because I'm treating it much like the library itself. With each release I try to add a little more information or little tweaks here and there. It's possible to write quite a lot in 10 years, despite putting down only a few hours with each release.

 

Thanks for the feedback. I'll consider adding this back-link.

 

I haven't done so before because I've tried to keep the script language section free of links into the application developer sections because the script writers doesn't really need to know how things work under the hood. I should probably reconsider this entire decision.


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

#10 BlackMoons   Members   -  Reputation: 439

Like
0Likes
Like

Posted 20 September 2013 - 02:27 AM

Understandable, It could confuse some since the C++ code is similar to the anglescript code if a scripter got into the wrong section of the documentation he might think he has to (or can) do things that must be done on the application side.

 

Reconsider the decision for sure, although I am not sure what you should decide as there is pros and cons for each. Maybe making the links back into the application section clearly linked as "For application developers, Also see:" or something similar?


Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS