Re: portable finalizer



Marco Gidde <marco.gidde@xxxxxxxxxx> writes:

Tamas Papp <tkpapp@xxxxxxxxx> writes:

I am wrapping a void * cffi pointer and some extra information in a
CLOS object. When the object is garbage-collected, I would like to
have the memory location freed. I have found out how to do
finalization in SBCL, but I want to make my code portable.

I have seen finalize implementations with #+ directives floating
around, could somebody post a reasonably comprehensive version? Or is
there a library to do this?

there is a small library out there called trivial-garbage with
finalization code for several CL implementations.

Thanks Marco,

I also found that there is a cffi:finalize that does the thing I want.

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?

Thanks,

Tamas
.



Relevant Pages

  • Re: Tutorial for CComPtr and CComQIPtr
    ... Apologies for confusing the topic with the mention of "garbage ... pointer" and the "Smart pointer". ... The template class Ptrimplements a garbage collecting pointer to any ... class derived from RefCount. ...
    (microsoft.public.vc.atl)
  • Re: Arithmetic on function address
    ... The Standard permits you to cast a pointer ... garbage out" is the rule that applies here. ... You mean that on this compiler, ...
    (comp.lang.c)
  • Garbage Collection - Pointer out of scope, or not?
    ... I've got a question about .NET and pointer scope. ... to find out how smart .NET is with it's garbage collection and realizing ... that in one of my functions, I create a different FileStream, call it "fs2", ...
    (microsoft.public.dotnet.languages.vc)
  • Re: How to return VB "Nothing"
    ... That last advice is wrong. ... The fact that the garbage in the ... Microsoft MVP, MCSD ... > If fact the pointer that VB will have created to receive the return value ...
    (microsoft.public.vc.atl)
  • Re: Class Instantiation Question
    ... But there is more to finalization than this. ... then a pointer to the object is placed on the finalization queue. ... The garbage collector scans the finalization queue looking for pointers to these objects. ... is removed from the finalization queue and appended to the freachable queue. ...
    (microsoft.public.vb.general.discussion)