Rick Parrish wrote:

>> NS_IMETHODIMP SomeClass::somefunction( nsILocalFile ** aNewFile ) {
>>   nsresult rv;
>>   nsCOMPtr<nsILocalFile> aFile =
>> o_CreateInstance( NS_LOCAL_FILE_CONTRACTID, &rv );
>>   aFile->InitWithPath( "/some/path/file" );
>> 
>>   nsILocalFile *dummy = aFile;
>>   NS_ADDREF( dummy );
>> 
>>   *aNewFile = dummy;
>> 
>>   return NS_OK;
>>   }
> 
> Short answer: yes that will work ... OR ... you could just declare aFile 
> as type "nsILocalFile *" instead of a smart pointer and leave out the 
> AddRef.

Generally, the latter is an undesirable pattern: it requires you to be 
very careful with early returns due to error checking (which the above 
function could really use, to avoid crashing when do_CreateInstance fails).

The cliché in the Mozilla source tree is:

nsresult rv;
nsCOMPtr<nsILocalFile> file = do_CreateInstance(..., &rv);
if (NS_FAILED(rv))
   return rv;
*aNewFile = file;
NS_ADDREF(*aNewFile);

(``aFile'' would indicate the name of an argument -- really a parameter, 
but hey -- so ``file'' is better for a local variable.)

Mike


Reply via email to