Skip to content

Commit

Permalink
修复:开启跟踪可能导致内存泄漏
Browse files Browse the repository at this point in the history
  • Loading branch information
SimingLiu committed Dec 10, 2024
1 parent 3916c39 commit eae8f06
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**/.idea/
.local
go.sum
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/dbjtech/rtreego

go 1.20

require github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
25 changes: 25 additions & 0 deletions rtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ package rtreego
import (
"fmt"
"math"
"reflect"
"sort"

"github.com/mohae/deepcopy"
)

// Comparator compares two spatials and returns whether they are equal.
Expand Down Expand Up @@ -756,7 +759,18 @@ func (tree *Rtree) searchIntersect(results []Spatial, n *node, bb Rect, needTrac
if !refuse {
searchOut := e.obj
if needTrace {
valueType := reflect.TypeOf(searchOut)
valueKind := valueType.Kind()
if valueKind == reflect.Pointer {
copyValue := deepcopy.Copy(searchOut)
searchOut = copyValue.(Spatial)
}
searchOut = searchOut.AppendTraceBox(n.ComputeBoundingBox())
np := n.parent
for np != nil {
searchOut = searchOut.AppendTraceBox(np.ComputeBoundingBox())
np = np.parent
}
}
results = append(results, searchOut)
}
Expand Down Expand Up @@ -840,7 +854,18 @@ func (tree *Rtree) nearestNeighbor(p Point, n *node, d float64, nearest Spatial,
d = dist
nearest = e.obj
if needTrace {
valueType := reflect.TypeOf(nearest)
valueKind := valueType.Kind()
if valueKind == reflect.Pointer {
copyValue := deepcopy.Copy(nearest)
nearest = copyValue.(Spatial)
}
nearest = nearest.AppendTraceBox(n.ComputeBoundingBox())
np := n.parent
for np != nil {
nearest = nearest.AppendTraceBox(np.ComputeBoundingBox())
np = np.parent
}
}
}
}
Expand Down

0 comments on commit eae8f06

Please sign in to comment.