Files use .rex, the command is rex, and the crate, runtime prefixes, docs, editor files and examples all use the new name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3.6 KiB
8. Memory: copies, sharing, and weak pointers
You never write free, new or &. Here is what the compiler does for you.
One owner
Every value has exactly one owner: a variable, a field, a list slot. When the owner's block
ends, the value is freed. That is true for strings, lists, maps, structs, enums and errors,
and it also happens on an early return, break, continue or ?.
Giving a value a new owner copies it:
func main() {
var a = [1, 2, 3]
var b = a // b is a real copy
b.push(4)
print(a, b)
}
[1, 2, 3] [1, 2, 3, 4]
You don't pay for copies you don't need:
- If
ais never used again,var b = ajust moves the value, with no copying. - A function that only reads a parameter gets it without a copy.
- Returning a local variable moves it out.
- Strings are immutable and counted, so copying one only bumps a counter.
All tests of the compiler run under a memory checker, so leaks and double frees are caught.
shared: one value, several owners
Sometimes you really do want several owners of one value (a texture used by many
sprites, a window and its buttons). Write shared:
struct Texture {
name: string
uses: int
func touch() { uses += 1 }
}
struct Sprite {
tex: shared Texture
x: int
}
func main() {
var tex = shared Texture{"grass", 0}
var a = Sprite{tex, 1}
var b = Sprite{tex, 2}
a.tex.touch()
b.tex.touch()
print(tex.uses, a.tex.uses)
}
2 2
shared Texture{...}creates the shared value. The type is writtenshared Texture.var b = a, passing it to a function, or putting it in a list or field gives another owner instead of copying the value.- A change through any owner is seen by all.
- The value is freed when the last owner goes away.
- Use it just like the value inside:
tex.name,tex.touch(). - Only structs and enums can be shared for now.
weak: pointing without owning
If a window owns its buttons and each button owns its window, neither would ever be freed.
Make one side weak: it points at the value without keeping it alive.
struct Window {
title: string
buttons: []shared Button
func add(b: shared Button) { buttons.push(b) }
}
struct Button {
label: string
window: weak Window
func click() {
if window != nil {
print(label, "clicked in", window.title)
} else {
print(label, "clicked, window is gone")
}
}
}
func main() {
var win = shared Window{"Main", []}
var ok = shared Button{"OK", weak win}
win.add(ok)
ok.click()
win = shared Window{"Other", []} // the first window is freed
ok.click()
}
OK clicked in Main
OK clicked, window is gone
weak xmakes a weak pointer to the shared valuex.- A weak pointer is nilable:
w == niltells you the value is gone.weak Tandweak T?mean the same thing, and it starts asnil. - Using a gone weak pointer stops the program with a clear message.
- To use the value as an owner, call
w.upgrade(). It gives ashared T?: the value (now with one more owner, so it can't vanish while you hold it) ornil.
struct Node { name: string }
func main() {
var n = shared Node{"root"}
var w = weak n
var again = w.upgrade()
if again != nil {
print(again.name)
}
}
root
Rule of thumb: the owner of something holds it shared; anything that merely points
back at it (a child to its parent, a button to its window) holds it weak.
Next: Generics and interfaces