How to notify Python about a Deleted Object? (and now invalid ptr) #5028
Unanswered
UnidayStudio
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I use python as the scripting language of my Engine. Nearly all the object construction is done via C++ side and this C++ code is also responsible to manage its lifetime, so I usually pass everything as a reference (py::return_value_policy::reference) and that's it. Problem is that when an object gets deleted by the engine, the Python side is not aware of it, storing now an invalid pointer to an object, that eventually leads to a crash when accessing it.
Here is a very simple example I made to mimic the situation:
Then in my main C++ code, I do something like that:
(NOTE: I know that I could get the
"var"
from thelocals
and set it to something else, but let's pretend this var is actually inside some super complicated python code that the program is not 100% aware of).So how exactly can I notify python that the object has been freed? Blender Game Engine uses python as their scripting language (but not pybind11, tho), and when this happen, python says in the console that "the object has been freed" and doesn't crash. I'm wondering how they do it...
I know that pybind11 have some smart pointer thing and ref counts, but I'm not fully sure how they work, so I appreciate the help.
Beta Was this translation helpful? Give feedback.
All reactions