chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] Question about gc-roots


From: pluijzer .
Subject: [Chicken-users] Question about gc-roots
Date: Thu, 13 Mar 2014 12:44:44 +0100

I am assigning non-immediate objects to foreign void pointers. To
prevent them from moving during garbage collection I turn the pointer
into a gc-root.

This works like I expect it would work, with one exception:

It seems that the finalizers of the objects assigned to the objects
inside the gc-root get fired prematurely.

I think I'm a little difficult to follow, I'll give an example:

----
#>
void* ptr;
<#

(define init-root
 (foreign-lambda* void ((scheme-object obj)) "
    ptr = CHICKEN_new_gc_root();
    CHICKEN_gc_root_set(ptr, obj);"))

(define root-ref
  (foreign-lambda* scheme-object () "
    C_return(CHICKEN_gc_root_ref(ptr));"))

(define (make-some-string)
  (set-finalizer! "hello" (lambda (s) (print s ", just got freed."))))

(define-record metadata
  data)

(init-root (make-metadata (make-some-string)))

; prints "hello"
(print (metadata-data (root-ref)))

; will fire finalizer printing "hello, just got freed."
; I didn't expect this because there is still a refrence to the object
inside the root.
(gc #t)

; prints "hello", object seems to still be around.
(print (metadata-data (root-ref)))
---

Maybe I don't understand gc-roots and use them wrongly or for the
wrong purpose. If so, would somebody kindly explain.

Thanks in advance,
Pluijzer



reply via email to

[Prev in Thread] Current Thread [Next in Thread]