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().
精彩评论