sproutcore/sproutcore — pull request #789 by publickeating (2012-05-29 04:45:06+00:00)
Title: Fixes memory leaks using child records (aka nested records)

I found that unloading and reloading a parent record recreated all of its child record objects when they were accessed, if those child records didn't have ids. This also turned out to be the case when setting the child record property on the parent record to a new object, to a new child record instance or to null. The result was that the store would have unreachable cached values and objects in: parentRecords, childRecords, records and dataHashes.

I did two fixes that prevent this from happening. The first was to ensure that child records that don't have an id are given a re-useable generated id, so that each time the child record is recreated (say after a reload on the parent), then the child gets the same unique id each time. The generated id is simply the parent's storeKey + the path to the child record, which ensures that it is always unique amongst all of that same child record type.

The second fix was that when calling set on the child record property of the parent, I added a check for an existing value and added functionality to SC.Record and SC.Store to unregister the previous child record before registering the new child record.

The first commits are unit tests to show the existence of the memory leak and the last few commits are the fixes.