M0102: Leak: allocation is never released
error
What it means
An owning variable, parameter or field leaves its scope while it still holds a live allocation from a Deallocating allocator (Heap, Pool) or an unclosed Arena/Pool resource. The label names the path that leaks: end of scope, early return, break, continue, throw, not released on all branches, overwritten, result discarded, or an @Owned parameter that is never freed.
Minimal failing example
From the test corpus: compiler/testdata/memory/leak_early_return.kt. The corpus expects 18:15: M0102 (the ^ comment in the source marks the column).
// REJECT M0102: the early return skips the release (the example of plan 4.3). Spec 7.2
@file:MemoryMode(Mode.Strict)
package memory.leak_early_return
import mini.memory.*
class Buffer(val size: Int) : AutoCloseable {
override fun close() {
println("close")
}
}
fun fill(b: Buffer): Boolean = b.size > 100
context(h: Heap)
fun load(n: Int): @Owned Buffer? {
val buf = Buffer(n)
// ^ M0102
if (!fill(buf)) return null
return buf
}
fun main() {
context(Heap()) { load(8).free() }
}Compiler output
Generated by running ./minic check compiler/testdata/memory/leak_early_return.kt (exit status 1).
$ ./minic check compiler/testdata/memory/leak_early_return.kt
error[M0102]: allocation from `Heap` is never released
--> compiler/testdata/memory/leak_early_return.kt:18:15 allocated here; path: early return
18 | val buf = Buffer(n)
| ^^^^^^
help: call `buf.free()` on every path (or `buf.use { ... }` for AutoCloseable), or move it to an owner
note: M0102 (Leak: allocation is never released): https://henryschwerdt.github.io/mini/reference/diagnostics/M0102
error: could not compile leak_early_return.kt (1 error, 0 warnings)How to fix
Release the value on every path (x.free(), x.close(), or wrap it in x.use { } for AutoCloseable classes), or hand ownership on (return it as @Owned, pass it to an @Owned parameter, store it in an owning field). Put the release in a finally block when exceptions are involved.
Corpus programs for this code
leak_arena_not_closed.kt(11:17)leak_branch.kt(12:13)leak_early_return.kt(18:15), shown aboveleak_end_of_scope.kt(12:13)leak_ignored_result.kt(15:5)leak_loop_body.kt(14:17)leak_loop_break.kt(13:17)leak_overwrite.kt(12:13)leak_overwrite_field.kt(14:5)leak_owned_param.kt(10:13)leak_pool.kt(13:21)leak_throw.kt(12:13)leak_when_branch.kt(12:13)
See also: Diagnostics overview and the memory guide.