Weak<T>.as_raw()

Are the following statements true:

(1) it returns NULL only when the Weak<T> is constructed via Weak::new()

(2) as_raw() always returns the same value regardless of what we do to the Weak<T> / Rc<T> (because i'ts pointing at the T in the RcBox)

If a Weak is created by downgrading an Arc, the raw pointer will be non-null for at least as long as an Arc keeps the backing heap object alive. While the Arc exists, as_raw will always be the same memory location (and this is actually exposed through the Arc::pin API).

In practice, Weak::as_raw always will return the same value (IIRC). However, in theory, nothing prevents as_raw from returning a null pointer once all strong references have been dropped.

2 Likes

From the docs for std::rc::Weak and std::sync::Weak

Returns a raw pointer to the object T pointed to by this Weak<T> .

The pointer is valid only if there are some strong references. The pointer may be dangling or even null otherwise.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.