COM Interop中的内存释放的责任 [英] Responsibility for memory deallocation in COM Interop
问题描述
我正在使用由.NET dll提供的C ++代码服务,我通过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.
其中一个被dll暴露的方法是从C ++调用的,要求dll
以返回包含某些信息的已分配字节数组。在我的C#代码中创建该方法后,.tlb生成器将它暴露如下:
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);
测试表明,当我根据需要发送指针时,我得到缓冲区分配
并填充了我需要的信息,但我不明白在这种情况下,它的责任是(C#\C ++)释放这个内存和如何。
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.
任何咨询?谢谢。
推荐答案
标准 COM内存分配规则仍适用于interop。这是调用者(使用C#DLL的客户端代码)发布 outputBuf
数组的责任(使用 SafeArrayDestroy )。
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).
这篇关于COM Interop中的内存释放的责任的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!