summaryrefslogtreecommitdiffhomepage
path: root/pkg/ilist
diff options
context:
space:
mode:
authorTamir Duberstein <tamird@google.com>2020-03-06 12:30:37 -0800
committergVisor bot <gvisor-bot@google.com>2020-03-06 12:31:43 -0800
commit6fa5cee82c0f515b001dee5f3840e1f875b2f477 (patch)
treeadec65de9c23995348e898b3e52077edabf3b0c9 /pkg/ilist
parent18d41cf15368c4d091ffdf84da655994eb1a1099 (diff)
Prevent memory leaks in ilist
When list elements are removed from a list but not discarded, it becomes important to invalidate the references they hold to their former neighbors to prevent memory leaks. PiperOrigin-RevId: 299412421
Diffstat (limited to 'pkg/ilist')
-rw-r--r--pkg/ilist/list.go8
1 files changed, 6 insertions, 2 deletions
diff --git a/pkg/ilist/list.go b/pkg/ilist/list.go
index f3a609b57..8f93e4d6d 100644
--- a/pkg/ilist/list.go
+++ b/pkg/ilist/list.go
@@ -169,8 +169,9 @@ func (l *List) InsertBefore(a, e Element) {
// Remove removes e from l.
func (l *List) Remove(e Element) {
- prev := ElementMapper{}.linkerFor(e).Prev()
- next := ElementMapper{}.linkerFor(e).Next()
+ linker := ElementMapper{}.linkerFor(e)
+ prev := linker.Prev()
+ next := linker.Next()
if prev != nil {
ElementMapper{}.linkerFor(prev).SetNext(next)
@@ -183,6 +184,9 @@ func (l *List) Remove(e Element) {
} else {
l.tail = prev
}
+
+ linker.SetNext(nil)
+ linker.SetPrev(nil)
}
// Entry is a default implementation of Linker. Users can add anonymous fields