Responsibility for memory deallocation in COM Interop

匆匆过客 提交于 2019-12-11 08:14:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!