如何 AddRef() 一个 nsCOMPtr 用作输出参数?
我的班级有一个成员:
class MyNativeXPCOMObject ... {
...
private:
nsCOMPtr<nsISomeInterface> someInterface_;
...
};
我有执行此操作的方法:
NS_IMETHODIMP MyNativeXPCOMObject::GetSomeObject(nsISomeInterface** aSomeInterface) {
NS_IF_ADDREF(someInterface_);
*aSomeInterface = someInterface_;
return NS_OK;
}
但是 Gecko 9 强制不允许您在 AddRef()
或 Release()
上调用 <代码>nsCOMPtr<>s。所以现在我正在这样做:
NS_IMETHODIMP MyNativeXPCOMObject::GetSomeObject(nsISomeInterface** aSomeInterface) {
NS_IF_ADDREF(someInterface_.get()); // <--- Added .get()!
*aSomeInterface = someInterface_;
return NS_OK;
}
但是直接访问原始指针让我感觉很脏。对于 getter 中的 out params,AddRef()
的正确方法是什么?
文档没有帮助。
I have a member for my class:
class MyNativeXPCOMObject ... {
...
private:
nsCOMPtr<nsISomeInterface> someInterface_;
...
};
I have methods that do this:
NS_IMETHODIMP MyNativeXPCOMObject::GetSomeObject(nsISomeInterface** aSomeInterface) {
NS_IF_ADDREF(someInterface_);
*aSomeInterface = someInterface_;
return NS_OK;
}
But Gecko 9 enforces that you're not allowed to call AddRef()
or Release()
on nsCOMPtr<>
s. So now I'm doing this:
NS_IMETHODIMP MyNativeXPCOMObject::GetSomeObject(nsISomeInterface** aSomeInterface) {
NS_IF_ADDREF(someInterface_.get()); // <--- Added .get()!
*aSomeInterface = someInterface_;
return NS_OK;
}
But directly accessing the raw pointer makes me feel dirty. What is the proper way to AddRef()
for out params in getters?
The documentation was not helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需更改指令顺序:
*aSomeInterface
是一个原始指针,因此您可以在其上使用NS_IF_ADDREF
。大多数 Gecko 代码似乎就是这样做的。You simply change the instruction order:
*aSomeInterface
is a raw pointer so you can useNS_IF_ADDREF
on it. That's how most Gecko code seems to do it.