nsresult res = factory->QueryInterface(kFactoryIID, (void **) aFactory); if (NS_FAILED(res)) { *aFactory = NULL; delete factory;
if (factory) { res = factory->QueryInterface(kFactoryIID, (void **) aFactory); if (NS_FAILED(res)) { *aFactory = NULL; delete factory; } } else { res = NS_ERROR_OUT_OF_MEMORY;
extern "C" NS_EXPORT nsresult NSGetFactory(    nsISupports* serviceMgr,    const nsCID &aClass,    const char *aClassName,    const char *aProgID,    nsIFactory **aFactory)//----------------------------------------------------------------------------------------{    if (aFactory == NULL)        return NS_ERROR_NULL_POINTER;    if (aClass.Equals(kPrefCID))    {        nsPrefFactory *factory = new nsPrefFactory();        nsresult res = factory->QueryInterface(kFactoryIID, (void **) aFactory);        if (NS_FAILED(res))        {            *aFactory = NULL;            delete factory;        }        return res;    }    return NS_NOINTERFACE;}