Advertisement

Access Violation when Calling Release on asIScriptObject

Started by February 15, 2014 03:55 AM
2 comments, last by wilberolive 10 years, 9 months ago

I have an object, that I want to add some data members to using script, like this.


shared class TestObject : IObject
{
	TestObject(Object @object)
	{
 me = object;
		object.AddData("TestData1");
		object.AddData("TestData2");
	}

 Object @me;
}

The test data members look like this, just for testing at the moment.


shared class TestData1 : IData
{
	TestData1(Data @data){
me = object;
}
Data @me;
};

shared class TestData2 : IData
{
	TestData2(Data @data){
me = object;
}
Data @me;
};

This all works fine, until I shut down and the TestObject releases the data members. The first data member always releases ok, but the second one will crash with an access violation. If I comment out the second data member (so I'm only adding one), then it works ok. As soon as I try to add more than one, it starts crashing after the first one when calling Release.

Here is the c++ code for adding a data member to an object.


asIScriptObject* Object::AddData(std::string type)
{
	asIScriptObject* data = CreateNewData(type);
	data->AddRef();
	return data;
}

The exception looks like this, and always happens in

asCScriptEngine::CallObjectMethod when calling asCScriptObject::Destruct.


Unhandled exception at 0x00EF5204 in Project.exe: 0xC0000005: Access violation reading location 0xBAADF019.

I've been stumped with this for a long time. Any ideas?

After further investigation, it appears to have something to do with the data members storing a handle of the c++ data object. If I change the data members so they don't store a handle like this, then it all works with no crashing.??


shared class TestData1 : IData
{
	TestData1(Data @data){}
};

shared class TestData2 : IData
{
	TestData2(Data @data){}
};
Advertisement

The problem is most likely caused by missing calls to AddRef() or too many calls to Release().

How is the CreateNewData() function implemented?

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

I too have a feeling it has something to do with the reference counting. I've gone over the code carefully though and can't find anything that looks wrong compared with the sample game and the documentation.

The CreateNewData() function looks like this. mData is just an array that adds a new one on the end. createScript works the same way as in the sample game to load a script file and return the asIScriptObject. The ref count after AddData returns is 2. One for when the object is created, and one for when it is returned back to the script.


asIScriptObject* CreateNewData(std::string type)
{
   Data* data = mData->new();
   data->script = createScript(type);
   return data->script;
}

Turns out that I don't need to store the object in my script anyway, so I have since removed that from the code and the access violations have stopped.

This topic is closed to new replies.

Advertisement