如何将本地 CComSafeArray 返回到 LPSAFEARRAY 输出参数?
我有一个 COM 函数,应该通过 LPSAFEARRAY* 输出参数返回 SafeArray。 该函数使用 ATL 的 CComSafeArray
模板类创建 SafeArray。 我的幼稚实现使用 CComSafeArray
来将所有权从局部变量转移到输出参数:
void foo(LPSAFEARRAY* psa)
{
CComSafeArray<VARIANT> ret;
ret.Add(CComVariant(42));
*psa = ret.Detach();
}
int main()
{
CComSafeArray<VARIANT> sa;
foo(sa.GetSafeArrayPtr());
std::cout << sa[0].lVal << std::endl;
}
问题在于 CComSafeArray::Detach()
> 执行 Unlock
操作,以便当 SafeArray 的新所有者(本例中为 main 的 sa
)被销毁时,锁不为零并且 Destroy 无法使用
E_UNEXPECTED
解锁 SafeArray(这会导致内存泄漏,因为 SafeArray 未释放)。
通过 COM 方法边界在 CComSafeArrays 之间转移所有权的正确方法是什么?
编辑:从到目前为止的单个答案来看,错误似乎是在客户端(main
)而不是服务器端(foo
) ),但我很难相信 CComSafeArray
不是为这个琐碎的用例而设计的,必须有一种优雅的方法将 SafeArray 从 COM 方法中获取到 CComSafeArray
。
I have a COM function that should return a SafeArray via a LPSAFEARRAY*
out parameter.
The function creates the SafeArray using ATL's CComSafeArray
template class.
My naive implementation uses CComSafeArray<T>::Detach()
in order to move ownership from the local variable to the output parameter:
void foo(LPSAFEARRAY* psa)
{
CComSafeArray<VARIANT> ret;
ret.Add(CComVariant(42));
*psa = ret.Detach();
}
int main()
{
CComSafeArray<VARIANT> sa;
foo(sa.GetSafeArrayPtr());
std::cout << sa[0].lVal << std::endl;
}
The problem is that CComSafeArray::Detach()
performs an Unlock
operation so that when the new owner of the SafeArray (main's sa
in this case) is destroyed the lock isn't zero and Destroy
fails to unlock the SafeArray with E_UNEXPECTED
(this leads to a memory leak since the SafeArray isn't deallocated).
What is the correct way to transfer ownership between to CComSafeArrays through a COM method boundary?
Edit: From the single answer so far it seems that the error is on the client side (main
) and not from the server side (foo
), but I find it hard to believe that CComSafeArray
wasn't designed for this trivial use-case, there must be an elegant way to get a SafeArray out of a COM method into a CComSafeArray
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您直接设置接收
CComSafeArray
的内部指针。使用
Attach()
方法将现有SAFEARRAY
附加到CComSafeArray
:The problem is that you set the receiving
CComSafeArray
's internal pointer directly.Use the
Attach()
method to attach an existingSAFEARRAY
to aCComSafeArray
:只是为了确认标记的答案是正确的。 RAII 包装器无法跨 COM 边界工作。
发布的方法实现不正确,您不能假设调用者将提供有效的 SAFEARRAY。只是 [out] 不是 Automation 中的有效属性,它必须是 [out,retval] 或 [in,out]。如果它是 [out,retval],就像它看起来的那样,那么该方法必须从头开始创建一个新数组。如果它是 [in,out],则该方法必须销毁传入的数组(如果它与预期的数组类型不匹配)并创建一个新数组。
Just to confirm that the marked answer is the correct one. RAII wrappers cannot work across COM boundaries.
The posted method implementation is not correct, you cannot assume that the caller is going to supply a valid SAFEARRAY. Just [out] is not a valid attribute in Automation, it must be either [out,retval] or [in,out]. If it is [out,retval], which is what it looks like, then the method must create a new array from scratch. If it is [in,out] then the method must destroy the passed-in array if it doesn't match the expected array type and create a new one.
我猜想哪里无意允许这样的用例。编写 CComVariant 和 CComVariant 的开发人员可能不是同一个人。
CComPtr
:)我相信
CComSafeArray
的作者将值语义视为主要目标;连接/分离可能只是一个“奖励”功能。I'd guess that where was no intent to allow such a use case. Probably it was not the same developer who wrote
CComVariant
&CComPtr
:)I believe that
CComSafeArray
's author considered value semantics as major goal; Attach/Detach might simply be a "bonus" feature.