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>
141 lines
3.6 KiB
Markdown
141 lines
3.6 KiB
Markdown
# 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:
|
|
|
|
```rex
|
|
func main() {
|
|
var a = [1, 2, 3]
|
|
var b = a // b is a real copy
|
|
b.push(4)
|
|
print(a, b)
|
|
}
|
|
```
|
|
```text
|
|
[1, 2, 3] [1, 2, 3, 4]
|
|
```
|
|
|
|
You don't pay for copies you don't need:
|
|
|
|
- If `a` is never used again, `var b = a` just *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`:
|
|
|
|
```rex
|
|
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)
|
|
}
|
|
```
|
|
```text
|
|
2 2
|
|
```
|
|
|
|
- `shared Texture{...}` creates the shared value. The type is written `shared 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.
|
|
|
|
```rex
|
|
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()
|
|
}
|
|
```
|
|
```text
|
|
OK clicked in Main
|
|
OK clicked, window is gone
|
|
```
|
|
|
|
- `weak x` makes a weak pointer to the shared value `x`.
|
|
- A weak pointer is **nilable**: `w == nil` tells you the value is gone. `weak T` and
|
|
`weak T?` mean the same thing, and it starts as `nil`.
|
|
- Using a gone weak pointer stops the program with a clear message.
|
|
- To use the value as an owner, call `w.upgrade()`. It gives a `shared T?`: the value
|
|
(now with one more owner, so it can't vanish while you hold it) or `nil`.
|
|
|
|
```rex
|
|
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)
|
|
}
|
|
}
|
|
```
|
|
```text
|
|
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](09-generics-and-interfaces.md)
|