]> Cypherpunks repositories - gostls13.git/commitdiff
container/heap: avoid memory leak in example
authorWhisperRain <2516435583@qq.com>
Tue, 26 Feb 2019 23:01:53 +0000 (23:01 +0000)
committerIan Lance Taylor <iant@golang.org>
Tue, 26 Feb 2019 23:42:26 +0000 (23:42 +0000)
Set element in slice to nil avoiding memory leak.

Change-Id: I9dbef9a0466407011e326725d3a0b681cd815389
GitHub-Last-Rev: 1bae5d375876a7f146eb04c2a5ba88d079264eb3
GitHub-Pull-Request: golang/go#30386
Reviewed-on: https://go-review.googlesource.com/c/163601
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
src/container/heap/example_pq_test.go

index 7017095cb8496316e03ef96f09a28d45b9c0b4fd..da1a233b7075756cc113d1ee018d4b123af3af68 100644 (file)
@@ -45,6 +45,7 @@ func (pq *PriorityQueue) Pop() interface{} {
        old := *pq
        n := len(old)
        item := old[n-1]
+       old[n-1] = nil  // avoid memory leak
        item.index = -1 // for safety
        *pq = old[0 : n-1]
        return item