Jump to content
  • Advertisement
Sign in to follow this  
Black Dot

Angelscript bug with freeing strings.

This topic is 4538 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!