Saturday, 7 September 2013

Responsibility for memory deallocation in COM Interop

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.

No comments:

Post a Comment