Skip to content

M0103: Double free ​

error

What it means ​

free(), close() or the end of a use {} block is applied to a value that is already freed on some path that reaches it, including a free in a loop body that runs twice.

Minimal failing example ​

From the test corpus: compiler/testdata/memory/dfree_simple.kt. The corpus expects 14:5: M0103 (the ^ comment in the source marks the column).

kotlin
// REJECT M0103: second free of the same object. Spec 7.3
@file:MemoryMode(Mode.Strict)

package memory.dfree_simple

import mini.memory.*

class Box(val v: Int)

context(h: Heap)
fun twice() {
    val b = Box(1)
    b.free()
    b.free()
//  ^ M0103
}

fun main() {
    context(Heap()) { twice() }
}

Compiler output ​

Generated by running ./minic check compiler/testdata/memory/dfree_simple.kt (exit status 1).

text
$ ./minic check compiler/testdata/memory/dfree_simple.kt
error[M0103]: `b` is freed twice
  --> compiler/testdata/memory/dfree_simple.kt:14:5   freed again here
   14 |     b.free()
      |     ^
  help: remove the second `free()`

note: M0103 (Double free): https://henryschwerdt.github.io/mini/reference/diagnostics/M0103

error: could not compile dfree_simple.kt (1 error, 0 warnings)

How to fix ​

Delete the second release. If the value is conditionally freed earlier, restructure so that exactly one path frees it, or free it only in one place (for example a finally).

Corpus programs for this code ​

See also: Diagnostics overview and the memory guide.

Released under the Apache License 2.0.