M0101: Use after free
error
What it means
A value is read, called or borrowed after free(), close() or the end of a use {} block released it. It also fires for a borrowed alias (val b = a) or a @Borrowed result once the owner it derives from was freed. If only one control-flow path frees the value, the message says it "may have been freed".
Minimal failing example
From the test corpus: compiler/testdata/memory/uaf_simple.kt. The corpus expects 14:13: M0101 (the ^ comment in the source marks the column).
// REJECT M0101: read after free. Spec 7.1
@file:MemoryMode(Mode.Strict)
package memory.uaf_simple
import mini.memory.*
class Box(val v: Int)
context(h: Heap)
fun run() {
val b = Box(1)
b.free()
println(b.v)
// ^ M0101
}
fun main() {
context(Heap()) { run() }
}Compiler output
Generated by running ./minic check compiler/testdata/memory/uaf_simple.kt (exit status 1).
$ ./minic check compiler/testdata/memory/uaf_simple.kt
error[M0101]: use of `b` after it was freed
--> compiler/testdata/memory/uaf_simple.kt:14:13 `b` was freed before
14 | println(b.v)
| ^
help: do not use `b` after `b.free()`; move the free to the end or keep the value alive
note: M0101 (Use after free): https://henryschwerdt.github.io/mini/reference/diagnostics/M0101
error: could not compile uaf_simple.kt (1 error, 0 warnings)How to fix
Move the use before the release, or release later. If you need the value after the call that frees it, keep ownership (do not pass it to an @Owned parameter) or copy out the data you need first.
Corpus programs for this code
borrow_use_after_free.kt(16:13)uaf_borrowed_result.kt(19:13)uaf_finally.kt(18:13)uaf_loop.kt(14:17)uaf_simple.kt(14:13), shown above
See also: Diagnostics overview and the memory guide.