Re: Leaks in nsSessionStore.js?
Jeff Walden <[email protected]> Sat, 17 Nov 2007 12:55:09 -0500
| Newsgroups | gmane.comp.mozilla.performance |
|---|---|
| Message-ID | <[email protected]> |
Michael Vincent van Rantwijk, MultiZilla wrote: > However, what I still fail to understand is _when_ I should (must?) use > weak references instead of a strong one; because most observers in the > CVS tree seem to be using a strong reference. I'm not sure there are entirely clear guidelines here, but a first cut is this: are you already holding a long-lived reference to the function/object being used as an observer? If you aren't, the observer is going to mysteriously go away when that reference is dropped, and you might miss whatever you're trying to observe. If you are, the observer's going to be around and live as long as you'd need it, so there's no reason for the observer service to hold a reference. In the sessionstore case, the service is the observer, which will be kept alive by the XPCOM component manager if by nothing else, and that reference will definitely stay around until the pref observer is no longer needed. In many cases, however, the observer service might have to hold the reference -- and that's where you can run into problems. If the observer has access to a reference to the observer service, then you have an unfreeable cycle. Playing it safe, this basically means you can't cache any references to the observer service in the code that's adding the observer (and probably further, too; further analysis might be needed). If you're careful you might be able to keep a reference, but the object that holds it can't be stored in a variable which is within the scope of the o bserver -- which, given how much closures capture, eliminates a lot of storage places. > Also, is it possible that I use a weak reference which somehow gets > cleared, giving me a nsnull instead of a proxied object? Calling QueryReferent on a weak reference either returns the original object or throws an exception, which answers your question if I understand it correctly. Jeff