Re: portable finalizer
- From: Tamas Papp <tkpapp@xxxxxxxxx>
- Date: Wed, 04 Jul 2007 13:49:45 +0200
Rudi Schlatte <rudi@xxxxxxxxxxxxx> writes:
Tamas Papp <tkpapp@xxxxxxxxx> writes:
[...]
Since this thread has drifted towards resources, I have a conceptual
question. Suppose I have a resource (eg a Cairo context in
cl-cairo2), is a pointer to a C structure.
(defclass foo () ((pointer :initform nil)))
The user has to be able to close the resource explicitly (eg in
cl-cairo2, this indicates that he has finished drawing and the file
can be closed), so there is a destroy method:
(defmethod destroy ((object foo))
(with-slots (pointer) object
(when pointer
(call_c_api_to_destroy pointer)
(setf pointer nil))))
But suppose that I also want to register a finalizer, and have the
object destroyed when it is garbage collected. The documentation of
cffi:finalize says
For portability reasons, function should not attempt to look at
object by closing over it because, in some lisps, object will
already have been garbage-collected and is therefore not accessible
when function is invoked.
So how is the finalizer supposed to look at pointer to determine if
the resource has already been freed explicitly with destroy?
It's not supposed to:
(defmethod destroy ((object foo))
(with-slots (pointer) object
(when pointer
(call_c_api_to_destroy pointer)
(setf pointer nil)))
(cancel-finalization object))
Thanks Rudi,
This was enlightening.
Tamas
.
- References:
- portable finalizer
- From: Tamas Papp
- Re: portable finalizer
- From: Marco Gidde
- Re: portable finalizer
- From: Tamas Papp
- Re: portable finalizer
- From: Rudi Schlatte
- portable finalizer
- Prev by Date: Re: socially challenged???!!??
- Next by Date: Re: socially challenged???!!??
- Previous by thread: Re: portable finalizer
- Next by thread: Re: portable finalizer
- Index(es):
Relevant Pages
|