[webkit-dev] Smart Pointers Usage Guidelines
Ryosuke Niwa
rniwa at apple.com
Mon Aug 21 16:25:16 PDT 2023
Hi all,
It has been a while since I last announced the plan to adopt smart pointers using clang static analyzer:
https://lists.webkit.org/pipermail/webkit-dev/2020-September/031386.html
Here are some updates.
1. We’ve made a progress in implementing all the rules including rules for local variables in clang static analyzer:
https://github.com/WebKit/WebKit/wiki/Smart-Pointer-Usage-Guidelines#rules-for-using-ref-counted-objects
2. We also have a new kind of smart pointers: CheckedRef <https://github.com/WebKit/WebKit/blob/main/Source/WTF/wtf/CheckedRef.h> / CheckedPtr <https://github.com/WebKit/WebKit/blob/main/Source/WTF/wtf/CheckedPtr.h>. These behave like Ref and RefPtr in that they increment & decrement a counter in an object but unlike them don’t extend the lifetime of the object. Instead, the destructor of the base object release asserts that there are no live CheckedRef / CheckedPtr left.
I added a new section in the guide describing when to use each smart pointer type:
https://github.com/WebKit/WebKit/wiki/Smart-Pointer-Usage-Guidelines#when-to-use-which-smart-pointer
3. I wanted to describe what applying these smart pointer rules mean. A lot of code changes needed for this work involves creating Ref / RefPtr / CheckedRef / CheckedPtr in stack:
https://commits.webkit.org/267082@main
One subtle thing is that even when a member variable is already Ref / RefPtr / CheckedRef / CheckedPtr, we must create another one in stack as seen here:
https://commits.webkit.org/267108@main
This is because these member variables can be cleared during the course of invoking a non-trivial function; or put it another way, it’s not immediately obvious from the code inspection that the object pointed to stays alive over the course of a non-trivial function call.
- R. Niwa
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20230821/fd8b6b5c/attachment.htm>
More information about the webkit-dev
mailing list