Repository navigation
Preserve ownership of borrowed COM pointer views - #1012
Robolightning wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1012 +/- ##
==========================================
+ Coverage 92.92% 92.94% +0.02%
==========================================
Files 153 154 +1
Lines 16588 16647 +59
==========================================
+ Hits 15414 15473 +59
Misses 1174 1174 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Thank you for your contributions.
I have also been concerned about the ownership and lifetime of interface pointers assigned to record fields, so I first added #1014 and #1015 as characterization tests.
Since this PR will likely affect the test below, we should probably merge main branch into this branch to verify.
comtypes/comtypes/test/test_out_record_lifetime.py
Lines 259 to 288 in 4d68bfa
Before proceeding with the discussion, allow me to note a few points regarding the basic test structure.
| if isinstance(holder, PointerField): | ||
| view = holder.pointer | ||
| else: | ||
| view = holder.contents |
There was a problem hiding this comment.
We should write two separate tests instead of using if.
| if remaining == 2: | ||
| pointer.Release() |
There was a problem hiding this comment.
This hides what the test is really checking.
Or does this mean the reference count is accidentally incrementing with no reproducibility and for an unknown reason?
Problem
Reading a COM pointer through
ctypes(for example through.contentsor a structure field) creates a view of existing storage without taking a COM reference. The view currently callsRelease()when it is collected. With an in-process test object and an extra reference for safe cleanup, collecting either kind of view lowers the reference count from two to one on the current main branch.Change
Skip the destructor's
Release()for pointer views with actypesbase. In theVT_RECORDandSAFEARRAYpaths that explicitly take a temporary reference, give it an independent pointer wrapper so that the matchingRelease()still occurs. The tests also check that the returnedVARIANTandSAFEARRAYpointers continue to own their own references.The ctypes documentation describes how a view keeps its underlying ctypes storage alive. The COM reference counting rules cover borrowing a reference whose owner outlives the view.
Verification