On 08/13/2014 07:24 PM, Aryeh Gregor wrote:
On Wed, Aug 13, 2014 at 5:44 PM, Ehsan Akhgari <ehsan.akhg...@gmail.com> wrote:
Can't you do the following instead?

unused << MyFunction(); // I know that I'm leaking this ref, but it's ok
somehow

No, because the use-case is where you don't want to leak the ref --
you want it to be released automatically for you.  So for instance,
here's a real-world bit of code from nsWSRunObject:

       if ((aRun->mRightType & WSType::block) &&
           IsBlockNode(nsCOMPtr<nsINode>(GetWSBoundingParent()))) {

GetWSBoundingParent() returns an already_AddRefed<nsINode>, but all we
want is to check if it's a block node and then throw it away (without
leaking it).  We don't want to save it beyond that.  With the proposed
change, this would be (assuming we re-added a .get() method, which
would now be as safe as on nsCOMPtr)

       if ((aRun->mRightType & WSType::block) &&
           IsBlockNode(GetWSBoundingParent().get())) {

which means the caller didn't take ownership, just used it and let the
destructor destroy it.
I'd say it would be a bug to let easily use already_Addrefed return values this 
way.
(Whether or not it is actually already_AddRefed or somehow hidden doesn't 
matter).
AddRef/Release are meaningful operations, and one should always see when they 
actually happen.
They should not be hidden behind some language magic.




-Olli


Similarly, if I would like to pass a string I have to a function that
wants an atom, I would like to be able to do
f(do_getAtom(string).get()) instead of
f(nsCOMPtr<nsIAtom>(do_getAtom(string))).

There are also functions in editor that do something like, say, insert
a new <br>, and return the element they inserted.  I might not want to
get a reference to the element, just want it created.  Currently I
have to make an nsCOMPtr to store the result and then ignore it.  I
don't see the value in requiring this.

I don't understand this.  You are already able to return raw pointers from
functions.  Returning an already_AddRefed is however a very different use
case, it means, I'm giving up ownership to the object I'm returning.

Yes, but what if the caller wants to use the object once (or not at
all) and then have it released immediately?  Is there value in
requiring explicit creation of an nsCOMPtr in that case?


_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to