-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix for reference leak issue (#1029) #1030
Conversation
As far as I can see, the new negative refcount issue has to do with types derived in Python: class PyDog(m.Dog):
pass
instance = PyDog("Molly")
del instance # `m.Dog` is decremented twice Looks like the issue is here in The solution could be to replace the if (type->tp_dealloc == pybind11_object_dealloc)
Py_DECREF(type); Unfortunately, while this does fix everything in |
That should do it. One issue was types derived in Python as mentioned above. The other issue had to do with types which are defined without a scope, e.g. like |
I think that is a very reasonable fix (thank you for tracking down the details)! |
By the way: I noticed this issue in one of my projects where I had been using a I thus switched to something like the following py::cpp_function cleanup_callback(
[](py::handle weakref) {
/* ... shutdown logic .. */
weakref.dec_ref();
}
);
(void) py::weakref(m.attr("BaseClass"), cleanup_callback).release(); and was surprised that it stopped getting called as soon as any In any case, now that this is fixed, it might be worth mentioning this alternative destruction logic in the docs. What do you think? PS: Looks like we're on hacker news front page (https://news.ycombinator.com/item?id=15095757), cool! |
Fantastic, all tests check out 👍 |
Seems like it would go along nicely with module destructors. As long as the refcounts are working, I think a simple m.attr("BaseClass").attr("_cleanup") = py::capsule(cleanup_callback);
Neat. It's certainly an interesting spike in traffic :) |
Indeed, putting the capsule into the |
I went ahead and squash-merged this. |
Upon further thought, I think it's helpful to document the |
I know I'm a little late (I'm in the middle of a move, and will probably be mostly inactive for the next 2-3 days as well), but the fix looks good; 👍 to @dean0x7d for tracking that down! |
See issue #1029