comtypes icon indicating copy to clipboard operation
comtypes copied to clipboard

List-index access to COM object array crashes python

Open cfarrow opened this issue 11 years ago • 1 comments

This bug happened to me a year ago so I'm a little fuzzy on exactly what happened, but it was a doozy. Here is some code that repros the bug

items = (POINTER(IWiaItem) * NUM_OBJECTS_TO_REQUEST)()
num_fetched = enum.Next(NUM_OBJECTS_TO_REQUEST, items)
print items[0]
print items[0]

where enum.Next has a signature of

COMMETHOD([], HRESULT, 'Next',
( ['in'], ULONG, 'celt'),
( ['in, out'], POINTER(POINTER(IWiaItem)), 'ppIWiaItem'),
( ['out'], POINTER(ULONG), 'pceltFetched'))

The array items is an array of IWiaItems. When you do items[0], it helpfully turns the thing into an actual IWiaItem object for you to use, but when that object is destroyed, we call __com_Release. So when you do items[0] the second time, it does Release again (and it only did AddReference once). So eventually, crash! Yay. Another user reported same in the user reviews on https://sourceforge.net/projects/comtypes/.

cfarrow avatar Feb 20 '14 02:02 cfarrow