WeakPtrFactory::GetMutableWeakPtr() proposal

233 views
Skip to first unread message

Alan Cutter

unread,
Sep 1, 2022, 8:24:28 PM9/1/22
to cxx
Currently it is easy to accidentally acquire a mutable this pointer in a const method if you own a WeakPtrFactory. This is due to its GetWeakPtr() const interface dispensing a WeakPtr<T>. I've fixed up one accidental logical const violation I came across and I suspect there may be dozens of others in the codebase.

Given the guidance on using const in Chromium I think we should change the WeakPtrFactory interface from:
  WeakPtr<T> GetWeakPtr() const;
to:
  WeakPtr<T> GetWeakPtr();
  WeakPtr<const T> GetWeakPtr() const;
  WeakPtr<T> GetMutableWeakPtr() const;

Using GetMutableWeakPtr() is still valid in certain scenarios e.g. your WeakPtrFactory is const while your this context is mutable. By giving it an explicit non-const name this should prevent further accidental const violations slipping through in future.

David Benjamin

unread,
Sep 2, 2022, 10:28:40 AM9/2/22
to Alan Cutter, cxx
This sounds like a good change to me!

What's the reason for making the WeakPtrFactory const? I guess it prevents you from calling InvalidateWeakPtrs(), but I dunno how important that is. If we removed the const from those classes, would we still need GetMutableWeakPtr? (Callers that need to get non-const WeakPtr from const this can always const_cast first, which matches what you'd have to do for a raw pointer anyway.)

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/086167b4-7428-48f6-a1a4-bace8fc669den%40chromium.org.

Alan Cutter

unread,
Sep 5, 2022, 7:21:03 PM9/5/22
to cxx, David Benjamin, cxx, Alan Cutter
I suspect we don't want to allow const_cast usage in general and specific cases like these can be "blessed" as explicit exceptions.
It would be nice to not have GetMutableWeakPtr() but I think its usage is prevalent enough that it'll be too much work to refactor those uses out of the codebase.

dan...@chromium.org

unread,
Sep 6, 2022, 10:01:07 AM9/6/22
to Alan Cutter, cxx
+1 to better const correctness. Explicit internal mutability in a library type like this makes sense to me. Would you be undertaking this change?

If I was to more closely match the naming patterns of well-established const-correct libraries, I'd name it GetWeakPtrMut(). 😇

On Thu, Sep 1, 2022 at 8:24 PM Alan Cutter <alanc...@chromium.org> wrote:
--

Alan Cutter

unread,
Sep 7, 2022, 12:16:44 AM9/7/22
to cxx, danakj, cxx, Alan Cutter
On Wednesday, 7 September 2022 at 12:01:07 am UTC+10 danakj wrote:
+1 to better const correctness. Explicit internal mutability in a library type like this makes sense to me. Would you be undertaking this change?

I'll pursue seeing how much work is involved in my spare cycles. Just wanted to check for high level agreement before diving into the task.
 

If I was to more closely match the naming patterns of well-established const-correct libraries, I'd name it GetWeakPtrMut(). 😇

SGTM. What are those libraries? This pattern is new to me.

dan...@chromium.org

unread,
Sep 7, 2022, 10:15:34 AM9/7/22
to Alan Cutter, cxx
On Wed, Sep 7, 2022 at 12:16 AM Alan Cutter <alanc...@chromium.org> wrote:


On Wednesday, 7 September 2022 at 12:01:07 am UTC+10 danakj wrote:
+1 to better const correctness. Explicit internal mutability in a library type like this makes sense to me. Would you be undertaking this change?

I'll pursue seeing how much work is involved in my spare cycles. Just wanted to check for high level agreement before diving into the task.
 

If I was to more closely match the naming patterns of well-established const-correct libraries, I'd name it GetWeakPtrMut(). 😇

SGTM. What are those libraries? This pattern is new to me.

Alan Cutter

unread,
Sep 7, 2022, 8:09:39 PM9/7/22
to cxx, danakj, cxx, Alan Cutter
On Thursday, 8 September 2022 at 12:15:34 am UTC+10 danakj wrote:
On Wed, Sep 7, 2022 at 12:16 AM Alan Cutter <alanc...@chromium.org> wrote:


On Wednesday, 7 September 2022 at 12:01:07 am UTC+10 danakj wrote:
+1 to better const correctness. Explicit internal mutability in a library type like this makes sense to me. Would you be undertaking this change?

I'll pursue seeing how much work is involved in my spare cycles. Just wanted to check for high level agreement before diving into the task.
 

If I was to more closely match the naming patterns of well-established const-correct libraries, I'd name it GetWeakPtrMut(). 😇

SGTM. What are those libraries? This pattern is new to me.


Hmm, I'm a bit less convinced by Rust libraries where mut is a language level keyword. Feels like language syntax cross-contamination.

Alan Cutter

unread,
Oct 24, 2022, 9:52:46 PM10/24/22
to cxx, Alan Cutter, danakj, cxx
FYI: This change has been completed: CL
GetWeakPtr() const now returns WeakPtr<const T> instead of WeakPtr<T>.

dan...@chromium.org

unread,
Oct 25, 2022, 12:03:53 PM10/25/22
to Alan Cutter, cxx
Thank you Alan, this is a great contribution to reducing global knowledge / increasing our ability to reason locally by making mutation paths more clear and less viral.
Reply all
Reply to author
Forward
0 new messages