mirror of
https://github.com/Monibuca/engine.git
synced 2026-04-23 00:07:06 +08:00
fix: memory leak
This commit is contained in:
+13
-19
@@ -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
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user