[racket] Memory consumption grows non-stop when re-loading a file and reusing a struct

From: Asumu Takikawa (asumu at ccs.neu.edu)
Date: Fri Aug 9 00:56:08 EDT 2013

On 2013-08-08 01:13:34 -0300, Diogo F. S. Ramos wrote:
> After defining and creating a struct with a `gen:custom-write', if one
> reenters the file and asks for an element of this struct, the REPL
> freezes and memory consumption grows non-stop.

Printing is tricky to debug since triggering an exception can cause an
infinite loop in printing. I believe that's the first step in the
problem here.

> (enter! "foo.rkt")
>   [re-loading /tmp/foo.rkt]
> > (foo-n bar)

In particular, note that this `foo-n` is no longer the same `foo-n`
because structs are generative. What's happening here is that you're
getting an error that `bar` is an instance of a different structure

In printing that error, Racket needs to print `bar` again. This calls
the method implementation, which calls `foo-n` again.

You might think that lexical scope would make sure that the method's
`foo-n` is different from the new `foo-n`, but that isn't the case
because the method refers to the same *top-level* binding. When you
re-define a top-level binding, you are basically just mutating it.


Posted on the users mailing list.