0

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.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222

1 Answers1

1

You simply change the instruction order:

*aSomeInterface = someInterface_;
NS_IF_ADDREF(*aSomeInterface);

*aSomeInterface is a raw pointer so you can use NS_IF_ADDREF on it. That's how most Gecko code seems to do it.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126