Am 19.12.2013 14:04, schrieb Wedel, Jan:
> Hi Thomas,
>
> Yesterday, in the evening, I actually found a solution (at least I
> believe it). I’ve written some more precise description on StackOverflow:
> http://stackoverflow.com/questions/20546303/ctypes-memory-management-when-returning-pointers-in-com-server
>
> But the summary is:
>
> I had to explicitly allocate memory with windll.ole32.CoTaskMemAlloc for
> anything that is returned by pointer like strings, pointers to structs,
> pointer to arrays of structs.
>
> I saw some C++ implementation that called that function for a pointer to
> an array so I was wandering if com types is doing it. So I found out
> that it’s actually not.
>
> In one of your test cases, I saw one function that creates a COM string
> with this function so I used that as a base for the other allocations I
> needed (structs, arrays). See StackOverflow for the code.
>
> I thought that com types is actually handling that for me by allocating
> memory for everything that is returned.

comtypes uses only two automation compatible types that are probably
allocated by CoTaskMemAlloc internally:  BSTR and SAFEARRAY.  But both
of these types are created by wrapper functions from the windows api,
SysAllocString and SafeArrayCreate which (I assume) use CoTaskMemAlloc 
internally.

> The most interesting thing is that the code worked on XP without
> allocation. But on the MSDN forum, they told me that they’d changed that
> memory manager in Windows 7 /Server 2003.

I found another post on stackoverflow, where in the second answer 
someone writes:

"""This causes undiagnosable memory leaks on Windows XP, a hard 
exception on Vista and up."""

http://stackoverflow.com/questions/10055607/when-using-cotaskmemalloc-should-i-always-call-cotaskmemfree

> Do you think the approach is correct or do see any obvious problems with
> that?

Yes it looks correct to me.

Thomas


------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT 
organizations don't have a clear picture of how application performance 
affects their revenue. With AppDynamics, you get 100% visibility into your 
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
_______________________________________________
comtypes-users mailing list
comtypes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/comtypes-users

Reply via email to