Python question to BGE core developpers

Hi, I’ve been playing with dynamic room creation and one question came up: I keep a list of object references that are created while building the room. I use this list to kick-off object destruction when the rooms needs to be deleted (via an “end” property). Now here is the question: if an object dies in between, what happens with its reference that is still in my list? I tried to use GameLogic.getCurrentScene().getObjectList().index(objref) to check if the object still exist but Blender crashes when I do that.
According to Python rules, as long as a reference hangs out somewhere, the memory should not be released and I can safely use the reference to access the object properties. So it should be alright and the memory will finally be released when I delete my own reference. At least, my test setup doesn’t crash. Can you confirm this?

When you get object reference in Python, in fact you get C++ pointer to object wrapped in Python structure. But engine itself keeps list of C++ objects, not Python object wrappers. Hence, when engine deletes object, your Python wrapper won’t be informed about it, and it keeps pointer to nonexisting C++ object (memory is released or used for something else). That’s why use of such object results in core dump.

And what’s solution? Let your object inform you when it dies - send a message, set a property, anything…

That makes sense. Thanks for both the question and the answer.