Skip to content

Commit 1128642

Browse files
authored
fix VM not clearing memory references when reused (#813)
1 parent f1c65fa commit 1128642

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

vm/vm.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ func (vm *VM) Run(program *Program, env any) (_ any, err error) {
6767
if vm.Stack == nil {
6868
vm.Stack = make([]any, 0, 2)
6969
} else {
70+
clearSlice(vm.Stack)
7071
vm.Stack = vm.Stack[0:0]
7172
}
7273
if vm.Scopes != nil {
74+
clearSlice(vm.Scopes)
7375
vm.Scopes = vm.Scopes[0:0]
7476
}
7577
if len(vm.Variables) < program.variables {
@@ -614,3 +616,10 @@ func (vm *VM) Step() {
614616
func (vm *VM) Position() chan int {
615617
return vm.curr
616618
}
619+
620+
func clearSlice[S ~[]E, E any](s S) {
621+
var zero E
622+
for i := range s {
623+
s[i] = zero // clear mem, optimized by the compiler, in Go 1.21 the "clear" builtin can be used
624+
}
625+
}

0 commit comments

Comments
 (0)