Ivan Shmakov | 22 Jul 04:12

finalizers vs. weak pointers

	Seemingly, weak pointers are invalidated before the finalizers
	for the object are run.  I wonder, what is the reason behind
	such a behaviour?

foo> (define weak-pointer
       (let* ((s (string #\f #\o #\o))
              (w (make-weak-pointer s)))
         (add-finalizer! s
                         (lambda (obj)
                           (write `(finalizing: ,obj))
                           (newline)
                           (write `(weak-pointer
                                    => ,(weak-pointer-ref w)))
                           (newline)))
         w))
; no values returned
foo> ,collect
Before: 2009301 out of 3000000 words available
After:  2038984 out of 3000000 words available
foo> (finalizing: "foo")
(weak-pointer => #f)

foo> 

Michael Sperber | 22 Jul 09:39

Re: finalizers vs. weak pointers


Ivan Shmakov <ivan <at> theory.asu.ru> writes:

> 	Seemingly, weak pointers are invalidated before the finalizers
> 	for the object are run.  I wonder, what is the reason behind
> 	such a behaviour?

While weak pointers are invalidated by the GC itself, finalizers are run
*after* the GC, so this order is pretty much intrinsic.  (Programs are
generally ill-advised to rely on the ordering of finalizer-like GC
actions.)

--

-- 
Cheers =8-} Mike
Friede, Völkerverständigung und überhaupt blabla

Ivan Shmakov | 22 Jul 10:13

Re: finalizers vs. weak pointers

>>>>> Michael Sperber <sperber <at> deinprogramm.de> writes:

 >> Seemingly, weak pointers are invalidated before the finalizers for
 >> the object are run.  I wonder, what is the reason behind such a
 >> behaviour?

 > While weak pointers are invalidated by the GC itself, finalizers are
 > run *after* the GC,

	I. e., after all the garbage objects were ``marked'' as such,
	but before they're actually removed from memory?

 > so this order is pretty much intrinsic.  (Programs are generally
 > ill-advised to rely on the ordering of finalizer-like GC actions.)

	Indeed, I was quite surprised that the issue like this has ever
	appeared on my way.

Richard Kelsey | 22 Jul 11:32

Re: finalizers vs. weak pointers

   From: Ivan Shmakov <ivan <at> theory.asu.ru>
   Date: Tue, 22 Jul 2008 09:14:02 +0700

   Seemingly, weak pointers are invalidated before the finalizers
   for the object are run.  I wonder, what is the reason behind
   such a behaviour?

The rule is that when a finalizer is called it is passed the
only existing refererence to the finalized object.  If the
weak pointers were not invalidated first there would be
other references to the finalized object, allowing it to be
accessed after finalization.
                                   -Richard Kelsey

Ivan Shmakov | 22 Jul 13:42

Re: finalizers vs. weak pointers

>>>>> Richard Kelsey <kelsey <at> s48.org> writes:

 >> Seemingly, weak pointers are invalidated before the finalizers for
 >> the object are run.  I wonder, what is the reason behind such a
 >> behaviour?

 > The rule is that when a finalizer is called it is passed the only
 > existing refererence to the finalized object.  If the weak pointers
 > were not invalidated first there would be other references to the
 > finalized object, allowing it to be accessed after finalization.

	I've got it.  Thanks!


Gmane