Repository navigation
Modern SharedPtr WeakPtr
Paul Squires edited this page Oct 19, 2026
·
5 revisions
Home › Smart Pointers › SharedPtr and WeakPtr › WeakPtr
Holds a link to a shared object that does not keep it alive, to break cycles.
WeakPtr( of T )- The only way to break a cycle: two objects holding SharedPtrs to each other never reach zero, and one of them holding a WeakPtr instead is what fixes it.
- It cannot be dereferenced.
Lock( )promotes it to a SharedPtr, or answers null if the object is gone -- and that is the only safe way to use one. -
Expired( )is a question about the instant it was asked; between it and a later use the object may go. Lock and test the handle instead. - The control block outlives the object, which is why a WeakPtr can safely read a count for something already destroyed: strong reaching 0 destroys the object, weak reaching 0 frees the block.
#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
print w.Expired( ) '' false
'' Lock is the ONLY safe way to use one
dim s as SharedPtr( of Node ) = w.Lock( )
print s.Ref( ).id '' 7
end scope
print w.Expired( ) '' true
print w.Lock( ).IsNull( ) '' true -- no dangling pointer handed backLibrary: SharedPtr and WeakPtr
Category: Smart Pointers
Documented under: Members
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing