Advertisement

Trying to return a reference to set from c++ to angelscript

Started by January 12, 2020 02:22 AM
5 comments, last by Michael Marchesan 4 years, 9 months ago

[URGENT]Hi, straight to the point:

I have the following class method :

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Graph</span>
    {</span>
    <span class="hljs-keyword">const</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">set</span><<span class="hljs-keyword">size_t</span>>&amp; get_selected_nodes() <span class="hljs-keyword">const</span>;
    …
    }

the following binding to angelscript:

engine->RegisterObjectType(<span class="hljs-string">"Graph"</span>, <span class="hljs-number">0</span>, asOBJ_REF);
engine->RegisterObjectMethod(<span class="hljs-string">"Graph"</span>, <span class="hljs-string">"set<uint>@ get_selected_nodes()"</span>, asMETHOD(gph::Graph, get_selected_nodes), asCALL_THISCALL);

engine->RegisterGlobalProperty("Graph g", g);

and i'm including and registering all the containers that are available in the angelscript repository.

The RegisterObjectMethod succeeds (i get no error when it happens)

Then i'm trying to run the following script:

 g.get_selected_nodes().size(); 

i get crazy numbers, and with

<span class="hljs-built_in">set</span><uint>@ selected = g.get_selected_nodes();
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">auto</span> it = selected.begin(); it++;)
    {
    print(<span class="hljs-string">""</span> + it.current());
    dfs_(it.current(), <span class="hljs-number">0</span>);
    }

i get crazy content.

Any idea on the possible cause?

I think we need to see more of your implementation code to really answer this. (Also the forum seems to have messed up your code formatting, bleh.) By the way, size_t is not always uint, for example on 64 bit architectures.

Advertisement

Your C++ method is returning the set by reference, but you have registered the method as returning it by handle. This is likely causing angelscript to release the handle and destroy the set before it is used, hence the crazy values.

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

WitchLord said:

Your C++ method is returning the set by reference, but you have registered the method as returning it by handle. This is likely causing angelscript to release the handle and destroy the set before it is used, hence the crazy values.

Can you explain how exactly? Returning a reference is, as far as I know, internally the same as returning a pointer (or handle), right? I'm also unsure how you'd properly refcount a std::set like that, like what do you bind asBEHAVE_ADDREF and RELEASE to? Wouldn't you have to wrap it in some proxy object?

In terms of the calling convention, returning a reference or a pointer is indeed the same thing. The difference is on angelscript's side. When returning a reference, angelscript will not take ownership of the reference, thus won't decrement the refcounter when it is done with it.

I can't say how exactly Michael registered the std::set, but it is possible to do it is by holding the ref count in a separate structure globally available so the ADDREF and RELEASE behaviours can work,. Or even register the type with asOBJ_NOCOUNT if the memory management in the application doesn't rely on reference counting at all. Of course, the application would have to be careful not to share any std::set instances allocated on the stack with angelscript so it won't try to delete objects on the C++ stack.

Since Michael didn't share exactly how it had been registered, I assumed he had done it in some way that works ?

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

@WitchLord sorry for the late reply; i registered it with the std containers registering functions available in angelscript's addon template library https://github.com/Sami-Vuorela/aatc

I'll try passing it by reference in the angelscript side

This topic is closed to new replies.

Advertisement