I have made changes to already_AddRefed (Bug 967364) and
getter_AddRefs (Bug 345123) to eliminate some potential mistakes that
can be made with them.

For already_AddRefed:

- already_AddRefed::get() has been renamed to
already_AddRefed::take(), to make the transfer of ownership of the
pointer in question more explicit.
- already_AddRefed::take() and assigning an already_AddRefed to a
nsCOMPtr/nsRefPtr consume the value of the already_AddRefed.  That is,
the mRawPtr is set to nullptr after performing one of these actions.
- already_AddRefed's destructor fatally asserts that the mRawPtr is
now null.  This means you are now required to transfer an
already_AddRefed to a smart pointer or to call take() on it.
- on compilers that support it, take() warns if the result is unused.
A shorthand has been introduced with mozilla::unused to allow
"throwing away" the pointer by directing an already_AddRefed into
unused.
- because take() respects const-ness, already_AddRefed must now be
passed by reference in certain situations (e.g. when it is an
r-value).

These changes are intended to make ownership transfer semantics
clearer and to make it harder to unintentionally leak a pointer from
an already_AddRefed.

For getter_AddRefs:

- The operator nsISupports** has been removed.  Previously,
getter_AddRefs's operator nsISupports** would allow you to write the
following code:

nsresult Foo(nsISupports** aOutparam);

nsCOMPtr<nsIFoo> foo;
Foo(getter_AddRefs(foo));

This is a very dangerous cast, as Foo only promises to return an
nsISupports*, not an nsIFoo*.  Now that the operator nsISupports** has
been removed, this code must be written as:

nsresult Foo(nsISupports** aOutparam);

nsCOMPtr<nsISupports> supports;
Foo(getter_AddRefs(supports));

nsCOMPtr<nsIFoo> foo = do_QueryInterface(supports).

Most of the code I saw in the tree that needed to change was using
nsISimpleEnumerator or nsIObjectInputStream.

Let me know if you have any questions.

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

Reply via email to