Re: portable finalizer



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))

Rudi
.



Relevant Pages

  • Re: [PATCH] x86, ioremap: use %pR in printk
    ... the first patch introduces a "small" resource pointer printout ... The output format is - minimum width is 8. ... return string(buf, end, sym, field_width, precision, flags); ...
    (Linux-Kernel)
  • Re: Summary of resource management discussion
    ... Resource management generally works by apply resource controls over a -group- ... The patches introduce a new pointer in task_struct, ... and a new structure 'struct container'. ... one/more/all resource controllers to a hierarchy). ...
    (Linux-Kernel)
  • Re: portable finalizer
    ... is a pointer to a C structure. ... The user has to be able to close the resource explicitly (eg in ... so there is a destroy method: ... (setf pointer nil)))) ...
    (comp.lang.lisp)
  • when is a database resource pointer not valid, and what test can be run?
    ... what I should be expecting. ... Any ideas why the resource pointer might still test valid, ... returned dataset gets passed into this method ...
    (comp.lang.php)
  • Re: Is There Any Reason to Even Use VC++ Anymore?
    ... pointer if you own it and a reference if owned externally is broken (not ... especially if this number is only limited by available memory or dictated ... It doesn't know if A still exists, so it can't destroy X. ... Note that A is the allocator of X, but can't be the destroyer if A goes away ...
    (microsoft.public.dotnet.languages.vc)