user181218
user181218

Reputation: 1685

Responsibility for memory deallocation in COM Interop

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.

Upvotes: 1

Views: 346

Answers (1)

noseratio
noseratio

Reputation: 61686

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

Upvotes: 1

Related Questions