开发者

How can I delete a SAFEARRAY returned from a .NET function in native code?

开发者 https://www.devze.com 2023-02-17 12:52 出处:网络
I\'m hosting a .NET library in my C++ program using the following methods, though not an exhaustive list:

I'm hosting a .NET library in my C++ program using the following methods, though not an exhaustive list:

CorBindToRuntimeEx()
GetDefaultDomain()
CreateInstance()
GetIDsOfNames()

And eventually a call to Invoke().

This works well enough, but I'm leaking memory whenever a .NET function returns an array, such as:

public int[] getArray() { int[] i = { 0, 1, 2, 3 }; return i; }

In this case, the function returns a VARIANT of type VT_SAFEARRAY|VT_I4. I've tried delete and delete[] on both the descriptor and pvData member, and this always fails. I've tried to SafeArrayDestroy() the descriptor, which succeeds but corrupts the heap. I can call SafeArrayDestroyData() on variant.parray->pvData which works fine, but SafeArr开发者_高级运维ayDestroyDescriptor() again corrupts the heap. I'm still leaking the array descriptors.

So, how can I deallocate the SAFEARRAY descriptor and plug this memory leak?


Impossible to guess without seeing code. The heap might have been corrupted long before anyway.

You should use VariantClear().

0

精彩评论

暂无评论...
验证码 换一张
取 消