-
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 Python object lifetimes associated with shared_ptrs #1566
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -375,3 +375,22 @@ def test_issue_1454(): | |
# Fix issue #1454 (crash when acquiring/releasing GIL on another thread in Python 2.7) | ||
m.test_gil() | ||
m.test_gil_from_thread() | ||
|
||
|
||
def test_issue_1546(): | ||
# Fix issue #1546 (Python object not kept alive by shared_ptr) | ||
somelist = [] | ||
|
||
class Derived(m.SharedPtrBase): | ||
def __init__(self): | ||
super(Derived, self).__init__() | ||
|
||
def f(self, value): | ||
somelist.append(value) | ||
print("Right one", 42) | ||
|
||
holder = m.SharedPtrHolder(Derived()) | ||
# At this point, our 'Derived' instance has gone out of scope in Python but | ||
# is being kept alive by a shared_ptr inside 'holder'. | ||
holder.run(42) | ||
assert somelist == [42] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way that you can check and ensure that all objects get destroyed when the go out of scope? See usages of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (This is to address your point on limiting scope, or at least documenting the scope in code) I'm still not sure of is if this creates a sort of reference cycle, and effectively cannot be GC'd until the interpreter dies... If it does this, it'd be nice to show it by testing the number of instances alive? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to be sure, could you add a
pytest.gc_collect()
here?