From 9cda4e30bb3bdd4f7e8ae79f795c0aeaf2b2efc3 Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Mon, 23 May 2016 15:54:40 -0700 Subject: [PATCH] set removed items to nil This fixes an issue where the Go garbage collector does not release some items which have been removed from the BTree because a reference to the item still exists in the items and children slices. --- btree.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/btree.go b/btree.go index bcefaf5..fc5aaaa 100644 --- a/btree.go +++ b/btree.go @@ -138,6 +138,7 @@ func (s *items) insertAt(index int, item Item) { // back. func (s *items) removeAt(index int) Item { item := (*s)[index] + (*s)[index] = nil copy((*s)[index:], (*s)[index+1:]) *s = (*s)[:len(*s)-1] return item @@ -146,7 +147,9 @@ func (s *items) removeAt(index int) Item { // pop removes and returns the last element in the list. func (s *items) pop() (out Item) { index := len(*s) - 1 - out, *s = (*s)[index], (*s)[:index] + out = (*s)[index] + (*s)[index] = nil + *s = (*s)[:index] return } @@ -180,6 +183,7 @@ func (s *children) insertAt(index int, n *node) { // back. func (s *children) removeAt(index int) *node { n := (*s)[index] + (*s)[index] = nil copy((*s)[index:], (*s)[index+1:]) *s = (*s)[:len(*s)-1] return n @@ -188,7 +192,9 @@ func (s *children) removeAt(index int) *node { // pop removes and returns the last element in the list. func (s *children) pop() (out *node) { index := len(*s) - 1 - out, *s = (*s)[index], (*s)[:index] + out = (*s)[index] + (*s)[index] = nil + *s = (*s)[:index] return }