(use objc) (require-library typetest) (define-objc-classes TypeTest) ;;; test (define-objc-class MyPoint NSObject ((outlet: window) (DBL x) (DBL y) (ID i) (slot: table)) (- ID init (set! @x 3.0) (set! @y 4.0) (set! @window (@ (@ TypeTest alloc) init)) ;; will not auto-refcnt (print "window: " @window) (set! @i @[ @[TypeTest alloc] init]) ;; will auto-refcnt (print "i: " @i) (set! @table (vector 3 4 5)) ;;(set! @table (lambda (x) (+ x 1))) ;; works in compiler, not interpreter ;; (closure captures self) ;;(set! @table add1) ;; works fine anywhere (set-finalizer! @table (lambda (x) (print "finalizing table " x))) self) (- INT ((invoke-table: INT y)) (vector-ref @table y) ;(@table y) ) (- ID get-table (objc:wrap @table)) (- INT return-three 3)) (define-objc-class MyCPoint MyPoint ((slot: color)) (- ID init (let ((self (@ super init))) (set! @color (list 'red 'green 'blue)) (set-finalizer! @color (lambda (x) (print "finalizing color " x))) self))) ;;; Test finalizer (let ((p (@ (@ MyCPoint alloc) init))) (print "-- Running GC prior to release of MyCPoint instance") (gc #t) (print p)) (print "-- Running GC after releasing MyCPoint") (gc #t) ;; Before the GC, the TypeTest instance assigned to "window" should be ;; released prematurely as we will not memory manage outlets. A ;; message from dealloc will appear if enabled. ;; After the GC, you should get messages saying that color and table ;; have been finalized, and the TypeTest instance in "i" will be correctly released. ;; --- Example run --- ;; window: #> ;; i: #> ;; -- Running GC prior to release of MyCPoint instance ;; deallocating ;; #> ;; -- Running GC after releasing MyCPoint ;; deallocating ;; finalizing color (red green blue) ;; finalizing table #(3 4 5) ;;; Some extraneous stuff used for optimizing callback tests, disregard. (define o (@ TypeTest alloc)) (define (safe n) (if (fx= n 0) 'done (begin (objc:send/safe o retainCount) (safe (fx- n 1))))) (define (unsafe n) (if (fx= n 0) 'done (begin (objc:send o retainCount) (unsafe (fx- n 1))))) (define (maybe n) (if (fx= n 0) 'done (begin (objc:send/maybe-safe o retainCount) (maybe (fx- n 1)))))