[webkit-dev] Using protect(this) versus ref()ing in its caller

Ryosuke Niwa rniwa at webkit.org
Wed Nov 6 14:01:48 PST 2013


Hi,

This discusion came up multiple times on Bugzilla so I'm posting it here.

When a member function f() of a RefCounted object can end up removing the
last ref to the object itself, should we do:

void Foo::f() {
    RefPtr<Foo> protect(this);
}

or

RefPtr<Foo> foo = rawPointerToFoo;
foo->f()

in its call site.

The first approach guarantees that any call to f() is safe while the second
approach is future proof if we add more code to use foo after calling f().

I think it's good to agree on one convention here since managing lifetime
of objects is tricky, and having one convention will make it easier to
reason about things.

- R. Niwa
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20131106/1b17be10/attachment.html>


More information about the webkit-dev mailing list