问题
I'm working on a C++ code consuming services provided by a .NET dll, which I'm accessing via COM Interop. I'm writing both the C++ and C# side.
One of the methods that is exposed by the dll and is called from the C++, asks the dll to return an allocated byte array containing some information. After creating that method in my C# code, the .tlb generator exposed it as follows:
HRESULT _stdcall DownloadData(
[out] SAFEARRAY(unsigned char)* outputBuf);
Testing has shown that when I send the pointer as required I do get the buffer allocated and filled with the information I need, but I don't understand in this scenario whose responsibility it is (C#\C++) to deallocate this memory and how.
Any advice? Thank you.
回答1:
The standard COM memory allocation rules still apply to interop. It's the responsibility of the caller (the client code consuming the C# DLL) to release outputBuf
array (using SafeArrayDestroy).
来源:https://stackoverflow.com/questions/18679659/responsibility-for-memory-deallocation-in-com-interop