XPCOM开发需要注意的地方

1. 智能指针nsCOMPtr

 

a) 如果一个智能指针作为调用函数的输出参数,必须用getter_AddRefs来调用,例如

 

操作语法:

{

 nsCOMPtr<nsIServiceManager> servMan;
 nsresult rv = NS_GetServiceManager(getter_AddRefs(servMan));

 // do sth

}
它等价于:

{

 nsIServiceManager* servMan;
 nsresult rv = NS_GetServiceManager(&servMan);

 // do sth

 servMan->Release()s

}

 

b)

操作语法:

{

 nsCOMPtr<nsIDirectoryServiceProvider> prov = do_QueryInterface(aElement);

 // do sth

}

它等价于:

{

 aElement->QueryInterface(iid, &prov);

 // do sth

 prov->Release();

}