[Newbies] Weakly holding and circular references

Tm Jhnsn digit at sonic.net
Sun Apr 29 13:35:51 UTC 2018

Hi all,

I've tried to learn how to properly use Weak collections for purposes of 
circular references, but I'm not sure I fully understand -- not enough 
to actually use it in my implementation.

I made a fairly large project a couple of years ago where I ended up 
with a lot of stale information in the image because I had circular 
references keeping objects from being cleared by GC.  I learned my 
lesson, and addressed the situation by building a 'release' mechanism 
and explicitly tearing down objects when I was done with them.

 From what I understand, the Weak collection hierarchy will keep 
references to objects but not in such a way that the GC won't clear them.

If I have, say, an ArchiveItem object, and the ArchiveItem has 
ArchivedFiles, but I want ArchivedFiles to know which ArchiveItem they 
belong to, then I've set myself up with a circular reference situation.

I understand I could have a separate object which allows ArchivedFiles 
to *ask* which ArchiveItem they belong to, instead of storing it within 

But I also understand that I could potentially use, say, a WeakSet in 
ArchiveItem, which will hold weakly onto ArchivedFiles since the 
ArchivedFiles themselves refer back to the ArchiveItem. I get the 
impression that this approach would free me from having to build out a 
complete 'release' mechanism or from doing other explicit clearing and 
removal of references.  But I don't understand if I could find myself in 
a situation where an ArchiveItem thinks it doesn't have any 
ArchivedFiles because they got GCed before I was truly through with them.

Can anyone recommend any approach or any materials on this subject?


More information about the Beginners mailing list