Skip to content

Commit

Permalink
set removed items to nil
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tidwall committed May 23, 2016
1 parent 00edb8c commit 9cda4e3
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down

0 comments on commit 9cda4e3

Please sign in to comment.