Is it possible to have an object X so that some function gets called when X is gc-ed?
Suppose I have something like
(define-type foo ...) (define bar (foo-create ...))
and inside of bar, there's a C-land resource that I want to have freed up when bar is GC-ed;
Is there a way to have some function like (foo-gc) s.t. when bar is GC-ed, (foo-gc bar) is called?
Thanks!
Afficher les réponses par date
Hi.
I guess that a hashtable with weak entries and wills might get your job done. http://www.iro.umontreal.ca/~gambit/doc/gambit-c.html#Wills
When your object is not strongly reachable anymore, you do your freeing stuff, and then you let the GC do the rest.
P!
2009/6/28 lowly coder lowlycoder@huoyanjinjing.com:
Is it possible to have an object X so that some function gets called when X is gc-ed?
Suppose I have something like
(define-type foo ...) (define bar (foo-create ...))
and inside of bar, there's a C-land resource that I want to have freed up when bar is GC-ed;
Is there a way to have some function like (foo-gc) s.t. when bar is GC-ed, (foo-gc bar) is called?
Thanks!
Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
On 27-Jun-09, at 10:27 PM, lowly coder wrote:
Is it possible to have an object X so that some function gets called when X is gc-ed?
Suppose I have something like
(define-type foo ...) (define bar (foo-create ...))
and inside of bar, there's a C-land resource that I want to have freed up when bar is GC-ed;
Is there a way to have some function like (foo-gc) s.t. when bar is GC-ed, (foo-gc bar) is called?
Thanks!
There are (at least) two ways to achieve this:
1) wills:
(define alloc-ptr (c-lambda () (pointer char) "alloc_ptr")) (define free-ptr (c-lambda ((pointer char)) void "free_ptr")) (define-type foo ptr) (define bar (make-foo (alloc-ptr)))) (make-will bar (lambda (obj) (free-ptr (foo-ptr obj)))) ...
2) release function on "pointer" FFI type: see https://webmail.iro.umontreal.ca/pipermail/gambit-list/2009-May/003475.html for an example.
Marc