fix: memory leak

This commit is contained in:
langhuihui
2023-08-29 09:41:28 +08:00
parent 85a4432fb1
commit f89b75ecb1
3 changed files with 18 additions and 27 deletions
+13 -19
View File
@@ -13,7 +13,7 @@ type ListItem[T any] struct {
Next, Pre *ListItem[T] `json:"-" yaml:"-"`
Pool *List[T] `json:"-" yaml:"-"` // 回收池
list *List[T]
OnRecycle func() `json:"-" yaml:"-"`
reset bool // 是否需要重置
}
func (item *ListItem[T]) InsertBefore(insert *ListItem[T]) {
@@ -50,18 +50,18 @@ func (item *ListItem[T]) IsRoot() bool {
}
func (item *ListItem[T]) Recycle() {
if item.list != item.Pool && item.Pool != nil {
if item.Pool.Length < PoolSize {
item.Pool.Push(item)
if item.OnRecycle != nil {
item.OnRecycle()
}
} else {
item.Pool = nil
item.list = nil
item.Next = nil
item.Pre = nil
}
hasPool := item.list != item.Pool && item.Pool != nil && item.Pool.Length < PoolSize
if item.reset || !hasPool {
var null T
item.Value = null
}
if hasPool {
item.Pool.Push(item)
} else {
item.Pool = nil
item.list = nil
item.Next = nil
item.Pre = nil
}
}
@@ -162,12 +162,6 @@ func (p *List[T]) Recycle() {
if p.Length != 0 {
panic("recycle list error")
}
// for item := p.Next; item != nil && item.list != nil && !item.IsRoot(); {
// next := item.Next
// item.Recycle()
// item = next
// }
// p.Clear()
}
// Transfer 把链表中的所有元素转移到另一个链表中
+1 -3
View File
@@ -290,9 +290,7 @@ func (p BytesPool) GetShell(b []byte) (item *ListItem[Buffer]) {
}
item = p[0].PoolShift()
item.Value = b
item.OnRecycle = func(){
item.Value = nil
}
item.reset = true
return
}