Re: [go-nuts] A problem about runtime.SetFinalizer

87 views
Skip to first unread message

Ian Lance Taylor

unread,
Aug 21, 2019, 11:00:26 AM8/21/19
to zct, golang-nuts
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

zct

unread,
Aug 21, 2019, 11:50:57 AM8/21/19
to golang-nuts
I got it.Thank you for your help

在 2019年8月21日星期三 UTC+8下午11:00:26,Ian Lance Taylor写道:

Robert Engels

unread,
Aug 21, 2019, 12:14:35 PM8/21/19
to zct, golang-nuts
Seems like GoLint should emit an 'unused parameter' in this case.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/906c3d64-9211-4206-a2ee-1a0d881a35b7%40googlegroups.com.



Ian Lance Taylor

unread,
Aug 21, 2019, 1:07:01 PM8/21/19
to Robert Engels, zct, golang-nuts
On Wed, Aug 21, 2019 at 9:14 AM Robert Engels <ren...@ix.netcom.com> wrote:
>
> Seems like GoLint should emit an 'unused parameter' in this case.

It's normal for a function to have an unused parameter. And in
particular it's normal for a finalizer to not refer to the object
being finalized.

Ian
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/821137931.4341.1566404048080%40wamui-kitty.atl.sa.earthlink.net.

Robert Engels

unread,
Aug 21, 2019, 1:18:37 PM8/21/19
to Ian Lance Taylor, zct, golang-nuts
I understand the first part, but the second? Why is the finalizer function passed a reference to the object being finalized then?
>To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcUjmy8%2BMPageq73sjWOR792mkXtLbGtwg6jfB_76R2R1Q%40mail.gmail.com.

Ian Lance Taylor

unread,
Aug 21, 2019, 1:25:31 PM8/21/19
to Robert Engels, zct, golang-nuts
On Wed, Aug 21, 2019 at 10:18 AM Robert Engels <ren...@ix.netcom.com> wrote:
>
> I understand the first part, but the second? Why is the finalizer function passed a reference to the object being finalized then?

There are cases where the finalizer function does want to refer to the
object, such as the finalizer on os.File which closes the descriptor.
And there are cases where the finalizer does not need to refer to the
object, such as a finalizer that just tracks the number of live
objects of some type.

Ian
Reply all
Reply to author
Forward
0 new messages