Skip to content

M0108: Wrong allocator when freeing ​

error

What it means ​

alloc.free(x) names an allocator, but x demonstrably came from a different one, as far as the compiler can tell statically.

Minimal failing example ​

From the test corpus: compiler/testdata/memory/free_wrong_allocator.kt. The corpus expects 15:13: M0108 (the ^ comment in the source marks the column).

kotlin
// REJECT M0108: `pool.free(x)` for an object that came from the Heap. Spec 3.3, 7.8
@file:MemoryMode(Mode.Strict)

package memory.free_wrong_allocator

import mini.memory.*

class Box(val v: Int)

fun main() {
    Pool<Box>(2).use { pool ->
        val heap = Heap()
        context(heap) {
            val b = Box(1)
            pool.free(b)
//          ^ M0108
        }
    }
}

Compiler output ​

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

text
$ ./minic check compiler/testdata/memory/free_wrong_allocator.kt
error[M0108]: `b` was allocated by `Heap`, not `Pool`
  --> compiler/testdata/memory/free_wrong_allocator.kt:15:13   wrong allocator
   15 |             pool.free(b)
      |             ^^^^
  help: use `b.free()` or the allocator that created it

note: M0108 (Wrong allocator when freeing): https://henryschwerdt.github.io/mini/reference/diagnostics/M0108

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

How to fix ​

Use x.free(), which finds the right allocator by itself, or name the allocator that actually produced the object.

Corpus programs for this code ​

See also: Diagnostics overview and the memory guide.

Released under the Apache License 2.0.