I have an issue where an object that is created in script code, is getting gc'd twice. Given my past experiences with GC, I'm 100% certain that it's my fault.
I think the cause of the problem is that I'm maintaining a list a std::map of the objects. My map stores gmVariable instances as the value type, which, when added create a gmUserObject via gmVariable::SetUser(). The reason that I do this, is so that when I maintain internal lists of objects, they update reference counts so that if the only reference of the objects is the list, the script object won't be destroyed.
The code to add objects to the map goes a little like this :-
Code:
void sGameObjectLinker::addObject(gmThread* a_thread, const char* pName, sGameObject* pObject)
{
pObject->setNameNative(pName);
pObject->setLinker(this);
gmVariable var;
var.SetUser(a_thread->GetMachine(), pObject, pObject->getType()->getId());
mObjects[pObject->getNameCrc()] = var;
}
And in script code, I do the following :-
Code:
global gObjectSim;
gObjectSim = sGameObjectSim();
myObject = sGameObject();
gObjectSim.addObject("testobj", myObject);
gObjectSim.removeObject(myObject);
What I end up with, is the sGameObject() being gc'd twice. Which makes sense, as I have two references; the local variable in script and the std::map in sGameObjectLinker. Am I supposed to generate a new gmUserObject any time I assign the item to a gmVariable, or is this object passed and there should only be one for every instance of my script exposed types?
Any idea where I'm going wrong?