SetFinalizer on pointer to local variable

260 views
Skip to first unread message

elias...@gmail.com

unread,
Sep 22, 2016, 3:28:48 AM9/22/16
to golang-dev
Hi,

The runtime.SetFinalizer documentation states that

"The argument obj must be a pointer to an object allocated by calling new or by taking the address of a composite literal."

That seems to exclude pointers to local variables, such as:

var o SomeStruct
runtime.SetFinalizer(&o, ...)

That seems like an odd restriction, because I don't see how &SomeStruct{} is different than &o to the GC: o escapes, if for nothing else than calling SetFinalizer on its pointer. Empirical evidence suggests that it does work, contrary to the documentation: see https://play.golang.org/p/j3cbTK4bHW where I get "finalized!", even on the &o variant.

Can I rely on finalizers on pointers to local variables? If so, should the documentation be updated? If not, is it reasonable to lift the restriction in general in the future? How?

 - elias

elias...@gmail.com

unread,
Sep 22, 2016, 3:43:31 AM9/22/16
to golang-dev
After posting this, I found https://github.com/golang/go/issues/6591 which covers exactly my question. As penance, I've mailed https://go-review.googlesource.com/c/29592/ to get the ball rolling on updating the documentation.

 - elias

T L

unread,
Oct 1, 2016, 8:27:19 AM10/1/16
to golang-dev


On Thursday, September 22, 2016 at 3:43:31 PM UTC+8, elias...@gmail.com wrote:
After posting this, I found https://github.com/golang/go/issues/6591 which covers exactly my question. As penance, I've mailed https://go-review.googlesource.com/c/29592/ to get the ball rolling on updating the documentation.

 - elias


By read this issue thread, https://github.com/golang/go/issues/6591,
it looks the new docs changed to:

The argument obj must be a pointer to an object allocated by calling
new, by taking the address of a composite literal, or by taking the
address of a local variable.

This didn't correct the document in principle.

It looks the example provided in this issue has not been valid for newer go versions, at least for go 1.5.3+.
https://play.golang.org/p/v8810GsAzF

In fact, for go 1.5.3+, the argument obj can also be pointers to package level variables.
The new document doesn't include this case.
So there is no need to distinguish which styles of pointers can be used as the argument obj at all,
for it looks any pointers can be used as the
argument obj.

And the other hand,  I think calling SetFinalizer on pointers of package level variables are meaningless, for they will be never released.

 

minux

unread,
Oct 1, 2016, 8:45:01 AM10/1/16
to T L, golang-dev
On Sat, Oct 1, 2016 at 8:27 AM, T L <tapi...@gmail.com> wrote:


On Thursday, September 22, 2016 at 3:43:31 PM UTC+8, elias...@gmail.com wrote:
After posting this, I found https://github.com/golang/go/issues/6591 which covers exactly my question. As penance, I've mailed https://go-review.googlesource.com/c/29592/ to get the ball rolling on updating the documentation.

 - elias


By read this issue thread, https://github.com/golang/go/issues/6591,
it looks the new docs changed to:

The argument obj must be a pointer to an object allocated by calling
new, by taking the address of a composite literal, or by taking the
address of a local variable.

This didn't correct the document in principle.

It looks the example provided in this issue has not been valid for newer go versions, at least for go 1.5.3+.
https://play.golang.org/p/v8810GsAzF

In fact, for go 1.5.3+, the argument obj can also be pointers to package level variables.

That won't work. Because package level variables are never GC'ed, so SetFinalizer will be
no-op for them. Therefore the docs is correct as is.

T L

unread,
Oct 1, 2016, 9:39:53 AM10/1/16
to golang-dev, tapi...@gmail.com


On Saturday, October 1, 2016 at 8:45:01 PM UTC+8, minux wrote:


On Sat, Oct 1, 2016 at 8:27 AM, T L <tapi...@gmail.com> wrote:


On Thursday, September 22, 2016 at 3:43:31 PM UTC+8, elias...@gmail.com wrote:
After posting this, I found https://github.com/golang/go/issues/6591 which covers exactly my question. As penance, I've mailed https://go-review.googlesource.com/c/29592/ to get the ball rolling on updating the documentation.

 - elias


By read this issue thread, https://github.com/golang/go/issues/6591,
it looks the new docs changed to:

The argument obj must be a pointer to an object allocated by calling
new, by taking the address of a composite literal, or by taking the
address of a local variable.

This didn't correct the document in principle.

It looks the example provided in this issue has not been valid for newer go versions, at least for go 1.5.3+.
https://play.golang.org/p/v8810GsAzF

In fact, for go 1.5.3+, the argument obj can also be pointers to package level variables.

That won't work. Because package level variables are never GC'ed, so SetFinalizer will be
no-op for them. Therefore the docs is correct as is.

That issue state, SetFinalizer on pointers of package level variables will panic, but it doesn't panic with newer go compilers now.
Even if SetFinalizer does no-op on addresses of package level variables, it is legal to do so in code.
 

T L

unread,
Oct 1, 2016, 9:42:29 AM10/1/16
to golang-dev, tapi...@gmail.com


On Saturday, October 1, 2016 at 8:45:01 PM UTC+8, minux wrote:


On Sat, Oct 1, 2016 at 8:27 AM, T L <tapi...@gmail.com> wrote:


On Thursday, September 22, 2016 at 3:43:31 PM UTC+8, elias...@gmail.com wrote:
After posting this, I found https://github.com/golang/go/issues/6591 which covers exactly my question. As penance, I've mailed https://go-review.googlesource.com/c/29592/ to get the ball rolling on updating the documentation.

 - elias


By read this issue thread, https://github.com/golang/go/issues/6591,
it looks the new docs changed to:

The argument obj must be a pointer to an object allocated by calling
new, by taking the address of a composite literal, or by taking the
address of a local variable.


I mean, at least the word "must" should be changed to "should".
 

T L

unread,
Oct 1, 2016, 10:59:16 PM10/1/16
to golang-dev, tapi...@gmail.com
@minux, I filed a issue, https://github.com/golang/go/issues/17311
but you closed it.
Howerver I still think the document is wrong.


On Saturday, October 1, 2016 at 8:45:01 PM UTC+8, minux wrote:
Reply all
Reply to author
Forward
0 new messages