From hans at hanshoglund.se Sat Dec 8 22:57:50 2012 From: hans at hanshoglund.se (=?iso-8859-1?Q?Hans_H=F6glund?=) Date: Sat, 8 Dec 2012 23:57:50 +0100 Subject: [trivial-garbage-devel] Finalize with closure Message-ID: Hello, I found myself wanting to reference the finalized object from a finalizer, so I added this wrapper around trivial-garbage:finalize, using a weak pointer to avoid permanently referencing the object from the finalizer closure. (defun add-finalizer (x f) (setf p (make-weak-pointer x)) (setf g (lambda () (funcall f (weak-pointer-value p)))) (finalize x g)) Would there be any problems with adding this function to the main library? I could probably provide a patch if requested. Best regards, Hans From loliveira at common-lisp.net Sun Dec 9 21:51:42 2012 From: loliveira at common-lisp.net (=?ISO-8859-1?Q?Lu=EDs_Oliveira?=) Date: Sun, 9 Dec 2012 21:51:42 +0000 Subject: [trivial-garbage-devel] Finalize with closure In-Reply-To: References: Message-ID: On Sat, Dec 8, 2012 at 10:57 PM, Hans H?glund wrote: > I found myself wanting to reference the finalized object from a finalizer, so I added this wrapper around trivial-garbage:finalize, using a weak pointer to avoid permanently referencing the object from the finalizer closure. Which Lisp are you using. IIRC, some (most?) Lisps run finalizers /after/ the object's memory is reclaimed, in which case the weak pointer will be null. -- Lu?s Oliveira http://r42.eu/~luis/ From loliveira at common-lisp.net Thu Dec 13 14:48:07 2012 From: loliveira at common-lisp.net (=?ISO-8859-1?Q?Lu=EDs_Oliveira?=) Date: Thu, 13 Dec 2012 14:48:07 +0000 Subject: [trivial-garbage-devel] Finalize with closure In-Reply-To: <77E7EFFF-E376-4E94-9590-916E83DF59B1@hanshoglund.se> References: <77E7EFFF-E376-4E94-9590-916E83DF59B1@hanshoglund.se> Message-ID: On Wed, Dec 12, 2012 at 9:36 PM, Hans H?glund wrote: > I use 32-bit LispWorks on OS X 10.7 and it works well enough. > > I do not know the behavior in other implementations. Right, I re-checked the documentation and Lispworks indeed runs the finalizer before reclaiming the object, which is why your approach works there. It won't work in SBCL, for example. As such, we cannot support this feature in a portable fashion. :-/ I've created a new issue to document this: . Thanks, -- Lu?s Oliveira http://r42.eu/~luis/ From hans at hanshoglund.se Thu Dec 13 15:41:17 2012 From: hans at hanshoglund.se (=?iso-8859-1?Q?Hans_H=F6glund?=) Date: Thu, 13 Dec 2012 16:41:17 +0100 Subject: [trivial-garbage-devel] Finalize with closure In-Reply-To: References: <77E7EFFF-E376-4E94-9590-916E83DF59B1@hanshoglund.se> Message-ID: OK, thanks for the clarification. Hans On 13 dec 2012, at 15:48, Lu?s Oliveira wrote: > On Wed, Dec 12, 2012 at 9:36 PM, Hans H?glund wrote: >> I use 32-bit LispWorks on OS X 10.7 and it works well enough. >> >> I do not know the behavior in other implementations. > > Right, I re-checked the documentation and Lispworks indeed runs the > finalizer before reclaiming the object, which is why your approach > works there. It won't work in SBCL, for example. As such, we cannot > support this feature in a portable fashion. :-/ > > I've created a new issue to document this: > . > > Thanks, > > -- > Lu?s Oliveira > http://r42.eu/~luis/