Angelscript bug with freeing strings.

Started by
2 comments, last by WitchLord 18 years, 3 months ago
Hello! I've experienced a rather serious problem recently while using AS. Here is the background of the problem: The script program based on AS loads my dll and successfully executes __declspec(export) int __stdcall RegisterPlugin(asIScriptEngine *ScriptEngine) , a function exported by the dll that registers all objects and functions which are later used from the scripting program. Everything works fine, but the string returning function: r = ScriptEngine->RegisterObjectMethod("CObj", "string& GetName()", asMETHOD(CObj,GetName), asCALL_THISCALL);assert(r >= 0); The method looks like this: string& GetName() { return Name; } and 'Name' is just a simple STL string object. The script code in the scripting program looks similiar to this: CObj obj; Console.WriteLine( obj.GetName() ); STL string objects are constructed in such way that if their length is <=16 they put all of the characters in the object itself (no pointers) and everything is ok when the scripting program tries freeing the string after successfully displaying it's value. The problem arises when the string is longer that 16 characters. In that situation STL string object instead of putting all the characters in it, it allocates new memory for the character chain and puts only the pointer to it in the object. In such situation when the scripting program successfully displays the string and wants to free it, it will want to free that allocated memory for the character chain in the STL string object too. Problem is that the scripting program has no idea about how to free this memory as it was allocated by the dll and not by the scripting program thus it crashes. I hope I'm correct about the reason of the problem, but I don't know how to create the solution to it. If you have any idea how this problem can be solved, please post your advice. Regards, Black Dot
Falling Man GamesFeel free to visit us!
Advertisement
Register the strings destructor.
It is.

It is using the std::string binding that comes with AS
What version of AngelScript are you using?

Since you're using a DLL, you must make sure that all allocation and deallocation of memory occurs in the same module. AngelScript makes this easier for you by allowing you to register the memory management functions it should use, both on a global level and on a per object type level.

Look at the functions asIScriptEngine::SetCommonObjectMemoryFunctions() and the behaviours asBEHAVE_ALLOC and asBEHAVE_FREE, for more information.

You may have a problem if both your dll and your application creates std::strings. The strings created by the application must then be released by the application, and vice versa. This is something that AngelScript cannot help you with.

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