Skip to content

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).

kotlin
// 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).

text
$ ./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 ​

See also: Diagnostics overview and the memory guide.

Released under the Apache License 2.0.