We develop custom editor for game, where we need to override component’s virtual LoadXML/SaveXML methods in order to create custom data serialization into/from XML file. Before everything was good, but with ScriptInstance became problem. So we created new class with name ScriptComponent that inherits from ScriptInstance. After ScriptComponent was attached to the node, we called ScriptComponent’s method CreateObject(), which simply call base class (“ScrriptInstance”) method CreateObject with necessary parameters. Look,
void ScriptComponent::CreateScriptObjects()
{
const String& className = this->GetClassName();
if (className != m_ClassName)
{
if (!m_ScriptFilePath.Empty())
{
ScriptFile* scriptFile = g_pApp->GetConstantResCache()->GetResource<ScriptFile>(m_ScriptFilePath);
if (scriptFile)
{
this->CreateObject(scriptFile, m_ClassName);
}
}
}
}
And there is ScriptComponent class declaration
[code]class ScriptComponent : public ScriptInstance
{
URHO3D_OBJECT(ScriptComponent, ScriptInstance)
public:
ScriptComponent(Context* context);
virtual ~ScriptComponent();
static void RegisterObject(Context* context);
/// Save as XML data. Return true if successful.
virtual bool SaveXML(XMLElement& dest) const;
/// Load from XML data. When setInstanceDefault is set to true, after setting the attribute value, store the value as instance's default value. Return true if successful.
virtual bool LoadXML(const XMLElement& source, bool setInstanceDefault = false);
void CreateScriptObjects();
protected:
String m_ScriptFilePath;
String m_ClassName;
};[/code]
In the result i get the memory leak, when exit from the program.
The dump about memory leak - http://pastebin.com/TpJ7n6tz. There you can see that in asCScriptObject::SetUserData() (457 line) was allocated memory and was not released then.
Here is image, which show the place, where happens memory allocation (457 line) http://imgur.com/a/xz0ar
In other words this code raises the memory leak.
if( !extra )
extra = asNEW(SExtra);
I suppose another 4 leaks were raised by the same code.
At once want to ask, is it safely to override ScriptInstance’s LoadXML/SaveXML method ?
Someone has idea why the memory leak happens and how can fix it ?
We use urho3d 1.5 release version.
Thanks.