Skip to content

Commit 80e6a9d

Browse files
committed
memory: set cleanup on *Memory instead of **Memory
Flagged in #1862. Stupid oversight on my part. Added a simple GC test to make sure the backing array stays mapped. Signed-off-by: Timo Beckers <[email protected]>
1 parent dc25617 commit 80e6a9d

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

memory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func newMemory(fd, size int) (*Memory, error) {
6565
}
6666

6767
mm := &Memory{b: b, ro: ro, heap: false}
68-
mm.cleanup = runtime.AddCleanup(&mm, memoryCleanupFunc(), b)
68+
mm.cleanup = runtime.AddCleanup(mm, memoryCleanupFunc(), b)
6969

7070
return mm, nil
7171
}

memory_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"io"
55
"math"
66
"os"
7+
"runtime"
78
"testing"
89

910
"github.com/go-quicktest/qt"
@@ -32,6 +33,10 @@ func TestMemory(t *testing.T) {
3233
mm, err := mustMmapableArray(t, 0).Memory()
3334
qt.Assert(t, qt.IsNil(err))
3435

36+
// Ensure the cleanup is set correctly and doesn't unmap the region while
37+
// we're using it.
38+
runtime.GC()
39+
3540
// The mapping is always at least one page long, and the Map created here fits
3641
// in a single page.
3742
qt.Assert(t, qt.Equals(mm.Size(), os.Getpagesize()))

0 commit comments

Comments
 (0)