SetFinalizer on pointer to local variable

瀏覽次數:260 次
跳到第一則未讀訊息

elias...@gmail.com

未讀,
2016年9月22日 凌晨3:28:482016/9/22
收件者: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

未讀,
2016年9月22日 凌晨3:43:312016/9/22
收件者: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

未讀,
2016年10月1日 上午8:27:192016/10/1
收件者: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

未讀,
2016年10月1日 上午8:45:012016/10/1
收件者: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

未讀,
2016年10月1日 上午9:39:532016/10/1
收件者: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

未讀,
2016年10月1日 上午9:42:292016/10/1
收件者: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

未讀,
2016年10月1日 晚上10:59:162016/10/1
收件者: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:
回覆所有人
回覆作者
轉寄
0 則新訊息