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:
<br><span><br></span>HRESULT
SyncRead(<span><br></span> [in]<span> </span>SHORT<span> </span><span></span>Source,<span><br> </span>[in]<span> </span>LONG<span> </span>NumItems,
<span><br> </span>[in]<span> </span>SAFEARRAY(LONG)
* ServerHandles,<br> <span></span>[out]<span> </span>SAFEARRAY(VARIANT) * Values,<span></span><br> [out]<span> </span>SAFEARRAY(LONG) * Errors,<span><br> </span>
[out,optional]<span> </span>VARIANT<span> </span>* Qualities,<span><br> </span>[out,optional]<span> </span>VARIANT<span> </span>* TimeStamps);<br><br>In Python, this looks like:<br><br>server_handles = [16384,16385]
<br>num_items = 2<br>values, errors, qualities, timestamps = groups.SyncRead(2, num_items, server_handles)<br><br>
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.
<br><br>However, if I append an extra "dummy" argument to the beginning of the server_handles list, it will always work.<br><br>server_handles = [0, 16384,16385]<br>
num_items = 2<br>
values, errors, qualities, timestamps = groups.SyncRead(2, num_items, server_handles)<br><br>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.
<br><br>As an amusing test, I tried setting the variables passed to the function as follows:<br><br>server_handles = [0, 16384, 16385, 0, 0, 0, 0, 0, 0, 0, 0, 0]<br>num_items = 2<br><br>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.
<br><br>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.
<br><br>-BB<br><br>