This post originated from an RSS feed registered with Ruby Buzz
by Michael Neumann.
Original Post: Snapshots in Wee and Cyclic Weak References
Feed Title: Mike's Weblog
Feed URL: http://www.ntecs.de/blog-old/index.rss?cat=ruby&count=7
Feed Description: Blogging about Ruby and other interesting stuff.
There's a problem with objects registered for backtracking whose snapshots
have a reference back to the registered object itself.
Let's look at some scenarios. I'll call the object to register
"obj" and it's snapshot "snap". I further assume that
"snap" references "obj".
Scenario 1 (Good)
We register "obj" for backtracking.
"obj" goes out of scope -> it gets garbage collected.
We take a snapshot. No snapshot of "obj" is taken.
Here, no problem.
Scenario 2 (Worse)
We register "obj" for backtracking.
We take a snapshot "snap".
Snapshot "snap" goes out of scope.
Now "obj" could go out of scope, too.
Note that as long as a snapshot of "obj" exists, "obj"
cannot go out of scope.
Scenario 3 (Worst)
We register "obj" for backtracking.
We take a snapshot "snap".
Before "snap" goes out of scope, we take another snapshot. This
new snapshot also includes "obj".
As long as at least one snapshot exists that was taken after the initial
"snap" snapshot, "obj" will not be garbage collected.
It's included in every further snapshot.
This might happen in Wee as we store a fixed number of snapshots (there's
always one available). So you have to take care to not introduce a
reference back to the registered object from the snapshot.
Solutions
Whenever possible, avoid to register objects in dynamically created
components (e.g. MessageBox).
The snapshot of a registered object which is taken by calling method
take_snapshot of the objects's class should never contain a
reference back to the referenced object. If this happens, you'll introduce
memory-leaks (for the duration of the session).