From: Ken O. <kd...@co...> - 2009-03-16 20:19:28
|
I'm having a problem where my garbage is not getting collected after the function that was using it returns. I've attempted to isolate the problem into a simple test case. It seems to me that some location on the stack is not getting cleared, so that things used by previously-called functions are retained. I understand that the conservative GC can sometimes retain your garbage, for example because of integer-pointer confusions, but I don't think it should happen this easily. I put the following in the file gc-bug.lisp: ---------------------------------------------------------------------- (defvar marker (gensym)) ;A unique object we can find later ;;Recursively create a list of our objects of the given length. (defun make-markers-1 (count) (if (plusp count) (cons marker (make-markers-1 (1- count))) nil)) ;;Make a list, but don't return it or store it anywhere (defun make-markers (count) (make-markers-1 count) nil) ;;Look through dynamic space to see if any of the cons cells ;;of our list are still there. (defun find-markers (when) (let ((count 0)) (sb-vm::map-allocated-objects #'(lambda (object type size) (declare (ignore type size)) (when (and (consp object) (eq (car object) marker)) (incf count))) :dynamic) (format t "~&~A, there are ~D markers~%" when count))) (defun cons-markers () (make-markers 10) ;Create (but don't return) the list (gc :full t) ;This should free it (find-markers "Inside cons-markers") ;See if it is still there ) (defun test () (gc :full t) ;GC first just to make sure (find-markers "After initial GC") ;Is anything there beforehand? (cons-markers) ;Now create, GC, check once (gc :full t) ;GC again (find-markers "After final GC") ;check again ) ---------------------------------------------------------------------- I get: This is SBCL 1.0.25, an implementation of ANSI Common Lisp. More information about SBCL is available at <http://www.sbcl.org/>.... * (progn (load "gc-bug") (test)) After initial GC, there are 0 markers Inside cons-markers, there are 10 markers After final GC, there are 0 markers So inside CONS-MARKERS, there's still a pointer to this list, even though MAKE-MARKERS did not return it. But outside, in TEST, the pointer is no longer active. Ken |