Invoking vative C++ with SAFEARRAY - Memory leak !

  • Thread starter Thread starter Sharon
  • Start date Start date
S

Sharon

Hello Gurus,

I have a native DLL with a C++ function as follow:

int __stdcall GetDeliveryRetry(
LPCSTR request,
SAFEARRAY** response,
LONG* outAS400ErrorCode,
LONG retryCount);

So I wrote a PInvoke for it as follow:

[DllImport("AS400Transport.dll", EntryPoint = "GetDeliveryRetry")]
private static extern int GetDeliveryRetry(string request,
[MarshalAs(UnmanagedType.SafeArray)]out string[] response, out int
outAS400ErrorCode, int retryCount);

And used like this:

int error;
string[] response = null;
string request = "8888000000DLV#ITRD";
int ret = AS400_GetDeliveryRetry(request, out response, out error, 3);

The trouble is that the out responsem, which is an array of safearray, is
not freed, and therefor I’m having a memory leak.


I was expecting the marshal to do the freeing after it copies the data to
the string array. but it does not, and I have a memeoy leak.

I know that I need to use the SafeArrayDestroy(...), but in the C# code I do
not have the pointer to free.


So how can I free this array of safearray?
 
Back
Top