nsCOMPtr<nsIMemory> memory = getter_AddRefs(nsMemory::GetGlobalMemoryService());
nsresult NS_COM NS_InitXPCOM2(nsIServiceManager* *result,                               nsIFile* binDirectory,                              nsIDirectoryServiceProvider* appFileLocationProvider){    nsresult rv = NS_OK;#ifdef NS_BUILD_REFCNT_LOGGING    nsTraceRefcnt::Startup();#endif     // Establish the main thread here.    rv = nsIThread::SetMainThread();    if (NS_FAILED(rv)) return rv;    // Startup the memory manager    rv = nsMemoryImpl::Startup();    if (NS_FAILED(rv)) return rv;    NS_StartupLocalFile();    StartupSpecialSystemDirectory();    // Start the directory service so that the component manager init can use it.    rv = nsDirectoryService::Create(nsnull,                                     NS_GET_IID(nsIProperties),                                     (void**)&gDirectoryService);    if (NS_FAILED(rv))        return rv;       nsCOMPtr<nsIDirectoryService> dirService = do_QueryInterface(gDirectoryService, &rv);    if (NS_FAILED(rv))        return rv;    rv = dirService->Init();    if (NS_FAILED(rv))        return rv;        // Create the Component/Service Manager    nsComponentManagerImpl *compMgr = NULL;       if (nsComponentManagerImpl::gComponentManager == NULL)    {        compMgr = new nsComponentManagerImpl();        if (compMgr == NULL)            return NS_ERROR_OUT_OF_MEMORY;        NS_ADDREF(compMgr);                PRBool value;        if (binDirectory)        {            rv = binDirectory->IsDirectory(&value);            if (NS_SUCCEEDED(rv) && value)                gDirectoryService->Define(NS_XPCOM_INIT_CURRENT_PROCESS_DIR, binDirectory);            //Since people are still using the nsSpecialSystemDirectory, we should init it.            char* path;            binDirectory->GetPath(&path);            nsFileSpec spec(path);            nsMemory::Free(path);                        nsSpecialSystemDirectory::Set(nsSpecialSystemDirectory::Moz_BinDirectory, &spec);                    }        if (!appFileLocationProvider) {            appFileLocationProvider = new nsAppFileLocationProvider;            if (!appFileLocationProvider)                return NS_ERROR_OUT_OF_MEMORY;        }        rv = dirService->RegisterProvider(appFileLocationProvider);        if (NS_FAILED(rv)) return rv;                        rv = compMgr->Init();        if (NS_FAILED(rv))        {            NS_RELEASE(compMgr);            return rv;        }                nsIServiceManager *serviceManager = NS_STATIC_CAST(nsIServiceManager*, compMgr);        nsComponentManagerImpl::gComponentManager = compMgr;                if (result) {        	NS_ADDREF(*result = serviceManager);        }    }    nsIServiceManager *serviceManager = NS_STATIC_CAST(nsIServiceManager*, compMgr);    nsCOMPtr<nsIMemory> memory = getter_AddRefs(nsMemory::GetGlobalMemoryService());    // dougt - these calls will be moved into a new interface when nsIComponentManager is frozen.    rv = compMgr->RegisterService(kMemoryCID, memory);    if (NS_FAILED(rv)) return rv;        rv = compMgr->RegisterService(kComponentManagerCID, NS_STATIC_CAST(nsIComponentManager*, compMgr));    if (NS_FAILED(rv)) return rv;    #ifdef GC_LEAK_DETECTOR  rv = NS_InitLeakDetector();    if (NS_FAILED(rv)) return rv;#endif    // 2. Register the global services with the component manager so that    //    clients can create new objects.    // Registry    nsIFactory *registryFactory = NULL;    rv = NS_RegistryGetFactory(&registryFactory);    if (NS_FAILED(rv)) return rv;    NS_DEFINE_CID(kRegistryCID, NS_REGISTRY_CID);    rv = compMgr->RegisterFactory(kRegistryCID,                                  NS_REGISTRY_CLASSNAME,                                  NS_REGISTRY_CONTRACTID,                                  registryFactory, PR_TRUE);    NS_RELEASE(registryFactory);    if (NS_FAILED(rv)) return rv;    // Category Manager    {      nsCOMPtr<nsIFactory> categoryManagerFactory;      if ( NS_FAILED(rv = NS_CategoryManagerGetFactory(getter_AddRefs(categoryManagerFactory))) )        return rv;      NS_DEFINE_CID(kCategoryManagerCID, NS_CATEGORYMANAGER_CID);      rv = compMgr->RegisterFactory(kCategoryManagerCID,                                    NS_CATEGORYMANAGER_CLASSNAME,                                    NS_CATEGORYMANAGER_CONTRACTID,                                    categoryManagerFactory,                                    PR_TRUE);      if ( NS_FAILED(rv) )        return rv;    }    for (int i = 0; i < components_length; i++)        RegisterGenericFactory(compMgr, &components[i]);    // Prepopulate registry for performance    // Ignore return value. It is ok if this fails.    nsComponentManagerImpl::gComponentManager->PlatformPrePopulateRegistry();    // Pay the cost at startup time of starting this singleton.    nsIInterfaceInfoManager* iim = XPTI_GetInterfaceInfoManager();    NS_IF_RELEASE(iim);    return rv;}