On Wed, Aug 21, 2019 at 5:55 AM zct <
tangzho...@bytedance.com> wrote:
>
> I recently had a goroutine leak problem, the code i reduced is like this:
https://play.golang.org/p/YW4hWoZZ7CD.
>
> The program is long-running and the finalizer is not called
>
> The RoomObj is deleted from map, why was it not released? Is it refereed by the asyncChan object?
>
> I don't understand here, can somebody explain the reason
Your finalizer itself is keeping the value alive.
runtime.SetFinalizer(a, func(r *RoomTest) {
fmt.Println("SetFinalizer")
close(a.asyncChan.a)
})
You need to write the finalizer function to refer to r, not a. The
reference to a in the finalizer function ensures that a is always
live.
Ian