Responsibility for memory deallocation in COM Interop

335 views Asked by At

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

There are 1 answers

2
noseratio On BEST ANSWER

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).