[plt-scheme] ffi and register-finalizer

From: Michael Reynolds (mtr26 at cs.byu.edu)
Date: Fri May 20 13:39:03 EDT 2005

So my pointers are not created by malloc, and they are (hopefully) 
registered using the code

(register-finalizer ptr destructor ) 

where the destructor is a function that will correctly delete them.  It seems to me that GC decides not to ever run the finalizer's, is that possible? I run a test file and no GC happens. I know that it doesn't call my finalizer's because my finalizers print a message when they are called and also when I close for example a Berkeley DbXml database it would sync it to the disk and update it... which doesn't happen unless I manually call my destructors throughout the program.

Thanks for letting me know that GC runs when it wants to and not by scope, that is good to know and if that is the case perhaps we are better off not relying on it anyway.

Thanks again,

Michael


Jay McCarthy wrote:

>I'm not sure if this answers your question:
>
>1. Pointers returned by (malloc) are garbage collected by the system.
>2. Pointers returned by the library you are interfacing need finalizers.
>
>I'm not if when pointers of type 1 have finalizers they are actually
>called. And remember, garbage collection does not happen as soon as it
>can (i.e. once lexical scope is gone), but when the GC decides to run.
>
>Jay
>
>On 5/20/05, Michael Reynolds <mtr26 at cs.byu.edu> wrote:
>  
>
>>  For list-related administrative tasks:
>>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>>
>>I am doing a project that relies on the ffi and c pointers. As I
>>understood, when you define a c pointer you can register a finalizer for
>>it that scheme will dynamically use to correctly delete and remove the
>>pointer from memory.
>>
>>For me it does not do this, the destructors are not called.  The printf
>>runs and the pointer can be used but the destructor is not called
>>automatically. I was wondering if anyone has more experience with this.
>>Is it supposed to garbage collect your c pointers? If not is there a way
>>to make it do that? Or is there some other error(s) in what I am trying
>>to do?
>>
>>Thanks for any help you can offer,
>>
>>Michael Reynolds
>>
>>
>>(define-syntax dbxml-ctype
>>  (syntax-rules ()
>>    [(_ _name destructor s->c)
>>    (begin
>>     (define-cpointer-type _name #f s->c
>>       (lambda (ptr)
>>         (if ptr
>>           (begin (register-finalizer ptr destructor ) (printf
>>"Registering ptr\n") ptr)
>>           (error '_name "got a NULL pointer"))))
>>     (defdbxml* destructor : _name -> _void))]
>>    [(_ _name destructor) (dbxml-ctype _name destructor #f)]))
>>
>>
>>    
>>
>
>
>  
>



Posted on the users mailing list.