Python, Pyglet, win32 API, COM: how to call IStream::Release if all I have is a pointer?

421 views Asked by At

I am trying to fix a memory leak in Pyglet. It occurs only on win32, as it involves IStream object, allocated by ole32.CreateStreamOnHGlobal not being freed.

You can find the issue #552 on Pyglet tracker by clicking here.

As you can see in the source code, the release of the stream was left out as a TODO item:

Comment in line 204 even includes my question: How to call IUnknown::Release on stream?

The stream is allocated in line 142. As pyglet does not relies on external dependencies, I don't think I can use pywin32. All we know at the python side about the stream object is gdiplus.py line 53

LPSTREAM = c_void_p

So, this is my background - the situation and the limitations. Now, my question - again. How can I call IStream::Release if all I have is a pointer? I have a long-running process that loads different images every 5 seconds or so and it looks like it is an issue for me. I can understand C/C++ and I even do some programming from time to time, but I am not win32 api expert by any means. Help!

2

There are 2 answers

1
Alex On BEST ANSWER

pyglet includes a COM module that interfaces with IUnknown (pyglet.com, used by the dsound audio driver).

This module didn't exist when the GDI+ interface was written.

You could rewrite the image loading to use COM objects instead of the C interface, or just use the steam pointer you have as the this pointer of an IUnknown.

1
Dima Tisnek On

perhaps this helps, it pertains to same problem.

http://bytes.com/topic/python/answers/23946-closing-excel-application