Mark Hammond - 2008-07-12

Logged In: YES
user_id=14198
Originator: YES

From initial bug report on the python-win32 mailing list:

I have run across what I believe may be a shortcoming with win32com when trying to call functions that expect one-based indexed arrays as input. The function I'm trying to invoke which demonstrates the problem is defined in my documentation as follows:

HRESULT SyncRead(
[in] SHORT Source,
[in] LONG NumItems,
[in] SAFEARRAY(LONG) * ServerHandles,
[out] SAFEARRAY(VARIANT) * Values,
[out] SAFEARRAY(LONG) * Errors,
[out,optional] VARIANT * Qualities,
[out,optional] VARIANT * TimeStamps);

In Python, this looks like:

server_handles = [16384,16385]
num_items = 2
values, errors, qualities, timestamps = groups.SyncRead(2, num_items, server_handles)

The num_items parameter is supposed to tell the function the total number sever_handles being passed in. Anyway, the above code always throws a com exception and fails. I am assuming this issue is due to the SyncRead function using one-based indexing for its array collection.

However, if I append an extra "dummy" argument to the beginning of the server_handles list, it will always work.

server_handles = [0, 16384,16385]
num_items = 2
values, errors, qualities, timestamps = groups.SyncRead(2, num_items, server_handles)

The above example is a very poor solution since it appears to produce a slow memory leak in my application. Every 10 to 12 times the SyncRead call is invoked using the exact same server_handles, memory consumption increases by 4kb. This memory leak problem does not happen when called from VB.

As an amusing test, I tried setting the variables passed to the function as follows:

server_handles = [0, 16384, 16385, 0, 0, 0, 0, 0, 0, 0, 0, 0]
num_items = 2

This makes an even bigger memory leak, leading me to believe any extra elements passed in the list beyond the num_items passed will always be allocated but never freed.

Does Mark or anyone else know how to correctly pass a collection to a COM call using one-based indexing that won't cause a mem leak? I couldn't find any mention of this issue in the Python Win32 book other than an Excel example which didn't seem to apply.

-BB