Jump to content
  • Advertisement
Sign in to follow this  
wilberolive

Access Violation when Calling Release on asIScriptObject

This topic is 2067 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

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?

Edited by wilberolive

Share this post


Link to post
Share on other sites
Advertisement

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){}
};
Edited by wilberolive

Share this post


Link to post
Share on other sites

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

 

How is the CreateNewData() function implemented?

Share this post


Link to post
Share on other sites

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.

Edited by wilberolive

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!