Skip to content

Modern SharedPtr Lock

Paul Squires edited this page Oct 19, 2026 · 5 revisions

Home › Smart Pointers › SharedPtr and WeakPtr › Lock

Lock

Returns a SharedPtr to the object if it still exists, or a null one if not.

Syntax

Lock( ) as SharedPtr( of T )

Examples

#import "std/memory"
using memory

type Node
    as int32 id
end type

dim w as WeakPtr( of Node )

scope
    dim p as SharedPtr( of Node ) = MakeShared( of Node )( )
    p.Ref( ).id = 7
    w = p

    '' Lock( ) promotes the observer back to an owner, if the object is
    '' still alive. This is the ONLY safe way to use a WeakPtr.
    dim s as SharedPtr( of Node ) = w.Lock( )
    print s.IsNull( )        '' false
    print s.Ref( ).id        '' 7
end scope

'' The last strong handle is gone, so the object is destroyed and the
'' promotion fails rather than handing back a dangling pointer
dim s2 as SharedPtr( of Node ) = w.Lock( )
print s2.IsNull( )           '' true

Library: SharedPtr and WeakPtr
Category: Smart Pointers
Documented under: Members

Clone this wiki locally