Aha, indeed it works much better that yay ! Thank you !
Now that we're at it, I've got another problem that looks like this one (it crashes in the same fctions) but with opIndex, and I didn't manage to solve it by using references. Problem is I didn't manage to write a short example to reproduce it.
This is the opIndex method I registered :
engine->RegisterObjectMethod (dataClass, "Data opIndex(string &in)", asMETHODPR(Data,operator[], (const std::string&), Data), asCALL_THISCALL);
The operator[] itself returns a copy and that's the whole point (Data is actually a wrapper for a tree, and using copies it allows you to dynamically add branches, or explore branches that don't exist yet). Thus I don't know if it is possible or wise to return Data& on the AS side ?
I tried using both these prototypes for AS : "Data opIndex(string)" and "Data& opIndex(string)" : the outcome is the same, but the causes differ. The only way to solve it is to ask AS not to collect garbage on these objects (and that's not really an alternative ^^).
If I use Data&, it looks like there's a buffer overload or something like that : because when AS calls my C++ method, it does so with a "this" that has been allocated by scriptstdstring.
This is the valgrind output that shows this :
==29662== Invalid read of size 4
==29662== at 0x80BFCED: std::list<DataBranch*, std::allocator<DataBranch*> >::begin() (stl_list.h:762)
==29662== by 0x80D8124: Data::operator[](std::string const&) (data.cpp:74)
==29662== by 0x4FC0EAA: CallThisCallFunction(void const*, unsigned long const*, int, void (*)()) (in /usr/lib/libangelscript-2.23.1.so)
==29662== by 0xBEB58D3F: ???
==29662== Address 0xddd94b4 is 0 bytes after a block of size 20 alloc'd
==29662== at 0x402AB64: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==29662== by 0x51126B3: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.17)
==29662== by 0x5114939: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (in /usr/lib/libstdc++.so.6.0.17)
==29662== by 0x5114A11: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.17)
And if I don't use a reference, I have a double-free problem :
==30274== Invalid free() / delete / delete[] / realloc()
==30274== at 0x402A21C: free (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==30274== by 0x5021D4D: asCScriptEngine::CallFree(void*) const (in /usr/lib/libangelscript-2.23.1.so)
==30274== by 0x4FC066B: CallSystemFunction(int, asCContext*, void*) (in /usr/lib/libangelscript-2.23.1.so)
==30274== by 0x4FFAC34: asCContext::ExecuteNext() (in /usr/lib/libangelscript-2.23.1.so)
==30274== by 0x4FFB9B7: asCContext::Execute() (in /usr/lib/libangelscript-2.23.1.so)
==30274== by 0x80D5575: Npc::Run(float) (npc.cpp:43)
==30274== by 0x80DACAD: _ZZN5Level7do_taskEvENKUlP10ObjectNodeE_clES1_ (level.cpp:146)
==30274== by 0x80DBB4D: _ZSt8for_eachISt14_List_iteratorIP10ObjectNodeEZN5Level7do_taskEvEUlS2_E_ET0_T_S7_S6_ (stl_algo.h:4442)
==30274== by 0x80DB0E8: Level::do_task() (level.cpp:147)
==30274== by 0x466FC87: AsyncTask::unlock_and_do_task() (in /usr/lib/panda3d/libpanda.so.1.8)
==30274== by 0x10A: ???
==30274== Address 0x7c993c8 is 4,064 bytes inside a block of size 4,096 alloc'd
==30274== at 0x402B018: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==30274== by 0x4FFBE19: asCContext::Prepare(asIScriptFunction*) (in /usr/lib/libangelscript-2.23.1.so)
==30274== by 0x80DACAD: _ZZN5Level7do_taskEvENKUlP10ObjectNodeE_clES1_ (level.cpp:146)
==30274== by 0x80DBB4D: _ZSt8for_eachISt14_List_iteratorIP10ObjectNodeEZN5Level7do_taskEvEUlS2_E_ET0_T_S7_S6_ (stl_algo.h:4442)
==30274== by 0x80DB0E8: Level::do_task() (level.cpp:147)
==30274== by 0x466FC87: AsyncTask::unlock_and_do_task() (in /usr/lib/panda3d/libpanda.so.1.8)
==30274== by 0x10A: ???
I have good hope that this is the last problem I'll ever have with AS (I already exposed all the interesting parts of my engine's API without issue, that's the last of it : the DataEngine).
This one looks a bit trickier than the chicken/egg issue, but I hope therre's enough indication to see where I did wrong ! Can I have help pretty please
?: because when AS calls my C%2