如何释放封装在 VARIANT 中的 IDL 用户定义结构的成员(即 BSTR、SAFEARRAY、VARIANT)?
我有一个在 IDL 中定义的结构。该结构具有以下成员:
{
BSTR m_sFirst;
BSTR m_sSecond;
VARIANT m_vChildStruct; //This member encapsulate a sub structure
SAFEARRAY __RPC_FAR * m_saArray;
}CustomINFO;
我使用 CoTaskMemAlloc 为结构分配内存并将其封装在 Variant 中,如下所示:
vV->vt = VT_RECORD;
vV->pvRecord = pStruct; //Pointer of sturct
vV->pRecInfo = pRI; //RecordInfo Interface
调用 VariantClear 来释放结构及其成员的内存是否足够? 是否也会发布IRecordInfo接口?
或者我必须手动获取封装的结构并自己释放每个成员,然后使用 CoTaskMemFree 释放结构。
感
谢皮卡罗·德·沃西奥
I have a structure defined in IDL. This structure has following members:
{
BSTR m_sFirst;
BSTR m_sSecond;
VARIANT m_vChildStruct; //This member encapsulate a sub structure
SAFEARRAY __RPC_FAR * m_saArray;
}CustomINFO;
I am allocating the memory for the structs using CoTaskMemAlloc and encapsulating it in Variant as follows:
vV->vt = VT_RECORD;
vV->pvRecord = pStruct; //Pointer of sturct
vV->pRecInfo = pRI; //RecordInfo Interface
Is it enough to call VariantClear to deallocate the memory of struct and its members?
Will it also release the IRecordInfo interface?
Or i have to manually get the encapsulated struct and deallocate each member myself and then use CoTaskMemFree to deallocate sturct.
Thanks
Picaro De Vosio
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
VariantClear
将调用IRecordInfo::Clear
,它释放结构成员持有的内存,但据说不会释放结构本身(这就是为什么你不能正确地返回[out] VARIANT
中的结构体)。 IRecordInfo 也应该被释放。(“应该”的意思是“否则,许多现有代码将被破坏/被破坏”)。
更多信息请参见:http://vcfaq.mvps.org/com/4.htm
VariantClear
will callIRecordInfo::Clear
, which releases the memory held by members of the struct, but supposedly doesn't release the struct itself (that's why you can't correctly return a struct in an[out] VARIANT
). the IRecordInfo should be released, too.("should" means "otherwise, a lot of existing code would break/be broken").
Some more info here: http://vcfaq.mvps.org/com/4.htm