Skip to content

Commit eae8f06

Browse files
committed
修复:开启跟踪可能导致内存泄漏
1 parent 3916c39 commit eae8f06

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
**/.idea/
22
.local
3+
go.sum

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/dbjtech/rtreego
22

33
go 1.20
4+
5+
require github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect

rtree.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ package rtreego
88
import (
99
"fmt"
1010
"math"
11+
"reflect"
1112
"sort"
13+
14+
"github.com/mohae/deepcopy"
1215
)
1316

1417
// Comparator compares two spatials and returns whether they are equal.
@@ -756,7 +759,18 @@ func (tree *Rtree) searchIntersect(results []Spatial, n *node, bb Rect, needTrac
756759
if !refuse {
757760
searchOut := e.obj
758761
if needTrace {
762+
valueType := reflect.TypeOf(searchOut)
763+
valueKind := valueType.Kind()
764+
if valueKind == reflect.Pointer {
765+
copyValue := deepcopy.Copy(searchOut)
766+
searchOut = copyValue.(Spatial)
767+
}
759768
searchOut = searchOut.AppendTraceBox(n.ComputeBoundingBox())
769+
np := n.parent
770+
for np != nil {
771+
searchOut = searchOut.AppendTraceBox(np.ComputeBoundingBox())
772+
np = np.parent
773+
}
760774
}
761775
results = append(results, searchOut)
762776
}
@@ -840,7 +854,18 @@ func (tree *Rtree) nearestNeighbor(p Point, n *node, d float64, nearest Spatial,
840854
d = dist
841855
nearest = e.obj
842856
if needTrace {
857+
valueType := reflect.TypeOf(nearest)
858+
valueKind := valueType.Kind()
859+
if valueKind == reflect.Pointer {
860+
copyValue := deepcopy.Copy(nearest)
861+
nearest = copyValue.(Spatial)
862+
}
843863
nearest = nearest.AppendTraceBox(n.ComputeBoundingBox())
864+
np := n.parent
865+
for np != nil {
866+
nearest = nearest.AppendTraceBox(np.ComputeBoundingBox())
867+
np = np.parent
868+
}
844869
}
845870
}
846871
}

0 commit comments

Comments
 (0)