OK, the problem is as I suspected. AngelScript tries to call ADDREF on the string reference you're returning from Get_Name(), it does this so that it can make sure the string reference is guaranteed to stay alive while passing it to the Print() method.
You can fix this particular scenario by changing your registration of the Print() method to:
r = engine->RegisterObjectMethod("ScriptingEngine", "void Print(const string &str)", asMETHOD(ScriptingEngine,PrintString), asCALL_THISCALL); assert (r >= 0);
I just exchanged the
str for
in.
By telling AngelScript that this is only an input reference, AngelScript takes a different approach to guaranteeing the lifetime of the object, i.e. it copies the string to a temporary variable instead of calling ADDREF.
However, you'll still have the same problem if, for example the script declares a function that takes a string as an inout reference, and then calls this function with the return value of Get_Name(). So maybe it is better to change your Get_Name() function to return a real asCScriptString reference instead of just a reference to a std::string, or maybe a new asCScriptString returned as object handle, for example:
asCScriptString *Room::Get_Name_Wrapper(){ return new asCScriptString(Get_Name());}engine->RegisterObjectMethod("Room", "string @ Get_Name()", asMETHOD(Room,Get_Name_Wrapper), asCALL_THISCALL);
You could also register the std::string directly with AngelScript, instead of asCScriptString, but then you wouldn't be able to use object handles, nor inout references, though this may not be a problem for you.