Open colin-si opened 9 months ago
reflect.StringHeader is unsafe
type StringHeader struct {
Data uintptr
Len int
}
Garbage Collector (GC) : Go's garbage collector automatically tracks all valid Pointers to ensure that the memory they point to is not reclaimed. This means that as long as there is a valid pointer to a memory region, that memory region will not be collected by the garbage collector.
StringHeader which uses uintptr is a typeless integer that can be used to hold pointer values, but it is not really a pointer type. Therefore, the garbage collector will not regard uintptr as a valid pointer to track. Causes StringHeader to be affected.
Describe the bug The return result of the StringToBytesWithNoCopy function will be recycled by GC when a large number of objects are created.
To Reproduce The following demo can reproduce this problem
Expected behavior When we executed the above demo, we found that the returned results were recycled after GC. This was unexpected. The memory where ResultKey is located is recycled and reallocated for use elsewhere.
Platform And Runtime (please complete the following information):
Platform
Runtime
Additional context Add any other context about the problem here.