[webkit-dev] Stop Using Raw Pointers & References in New Code

Fujii Hironori fujii.hironori at gmail.com
Fri Jan 27 04:28:54 PST 2023

On Wed, Jan 25, 2023 at 5:54 AM Michael Catanzaro via webkit-dev <
webkit-dev at lists.webkit.org> wrote:

> Thinking about this more, I'm not sure this plan works for WeakPtrs?
> Say we have:
> WeakPtr<Foo> f = /* initialized somehow */;
> if (Foo* f = f.get())
> {
>   // do something
> }
> Then we already broke the rule against using a raw pointer in a local
> variable. That's the only way to use a WeakPtr, so we kind of have to,
> but as long as you have it stored in a raw pointer, then we gain no
> additional safety from the WeakPtr. CheckedPtr would work better in
> local variables, but again that's not an option for types we don't
> control.

We need to keep a RefPtr<Foo> to do something.
 if (RefPtr f = f.get())
It's not a problem of the circular dependency in this case because the
local variable will be destructed soon.
The problem of WeakPtr is that it may return a pointer of half-destructed
Foo object if this code is called during the Foo destructor.
This is being discussed in https://github.com/WebKit/WebKit/pull/8748 .
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20230127/715a465b/attachment.htm>

More information about the webkit-dev mailing list