PRInt32 i; for (i = 0; i < mObservers.Count(); i++) { nsIDocumentObserver*  observer = (nsIDocumentObserver*)mObservers.ElementAt(i); observer->DocumentWillBeDestroyed(this); if (observer != (nsIDocumentObserver*)mObservers.ElementAt(i)) { i--; } }
XULDocumentImpl::~XULDocumentImpl(){    // mParentDocument is never refcounted    // Delete references to sub-documents    {        PRInt32 i = mSubDocuments.Count();        while (--i >= 0) {            nsIDocument* subdoc = (nsIDocument*) mSubDocuments.ElementAt(i);            NS_RELEASE(subdoc);        }    }    // Delete references to style sheets but only if we aren't a popup document.    if (!mIsPopup) {        PRInt32 i = mStyleSheets.Count();        while (--i >= 0) {            nsIStyleSheet* sheet = (nsIStyleSheet*) mStyleSheets.ElementAt(i);            sheet->SetOwningDocument(nsnull);            NS_RELEASE(sheet);        }    }    // set all builder references to document to nsnull -- out of band notification    // to break ownership cycle    if (mBuilders)    {        PRUint32 cnt = 0;        nsresult rv = mBuilders->Count(&cnt);        NS_ASSERTION(NS_SUCCEEDED(rv), "Count failed");#ifdef	DEBUG        printf("# of builders: %lu\n", (unsigned long)cnt);#endif        for (PRUint32 i = 0; i < cnt; ++i) {          nsIRDFContentModelBuilder* builder            = (nsIRDFContentModelBuilder*) mBuilders->ElementAt(i);          NS_ASSERTION(builder != nsnull, "null ptr");          if (! builder) continue;          rv = builder->SetDocument(nsnull);          NS_ASSERTION(NS_SUCCEEDED(rv), "error unlinking builder from document");          // XXX ignore error code?          rv = builder->SetDataBase(nsnull);          NS_ASSERTION(NS_SUCCEEDED(rv), "error unlinking builder from database");          NS_RELEASE(builder);        }    }    if (mLocalStore) {        nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(mLocalStore);        if (remote)            remote->Flush();    }    if (mCSSLoader) {      mCSSLoader->DropDocumentReference();    }        if (--gRefCnt == 0) {        NS_IF_RELEASE(kCommandUpdaterAtom);        NS_IF_RELEASE(kEventsAtom);        NS_IF_RELEASE(kIdAtom);        NS_IF_RELEASE(kObservesAtom);        NS_IF_RELEASE(kOpenAtom);        NS_IF_RELEASE(kPersistAtom);        NS_IF_RELEASE(kRefAtom);        NS_IF_RELEASE(kRuleAtom);        NS_IF_RELEASE(kTargetsAtom);        NS_IF_RELEASE(kTemplateAtom);        if (gRDFService) {            nsServiceManager::ReleaseService(kRDFServiceCID, gRDFService);            gRDFService = nsnull;        }        if (gXULUtils) {            nsServiceManager::ReleaseService(kXULContentUtilsCID, gXULUtils);            gXULUtils = nsnull;        }    }}