我有一个到 C 库的绑定。
(define-cstruct _rect
([x _int]
[y _int]
[w _int]
[h _int]))
我想在程序中使用这个对象。
(require ffi/unsafe
(rename-in "sdl-structs.rkt"
[make-rect _make-rect]))
(module rect racket
(provide (all-defined-out)))
(define (make-rect x y w h)
(define new-rect (_make-rect x y w h))
new-rect)
(define (left rect)
(rect-x rect))
(define (top rect)
(rect-y rect))
...
(define (bottom-left rect)
(values (left rect) (bottom rect)))
(define (bottom-right rect)
(values (right rect) (bottom rect)))
但目前存在内存泄漏。崩溃需要一段时间,但它仍然会崩溃。
(define (eat-memory)
(define memory (current-memory-use))
(define (eat-loop)
(begin
(make-rect 0 0 1 1)
(displayln memory)
(set! memory (current-memory-use))
(eat-loop)))
(eat-loop))
我想让内存自动回收,我该怎么做呢?我查看了有关 finalization of memory 的文档,但我不明白这应该有什么帮助。反正我不需要手动调用每个函数吗?