On Tue, 2005-06-07 at 22:49 -0400, Boris wrote:
> Reshat Sabiq wrote:
> > nsCOMPtr<nsICacheSession> cacheSession;
> > ...
> > nsCOMPtr<nsICacheEntryDescriptor> cacheEntry;
> > result = cacheSession->OpenCacheEntry(abc, sICache::ACCESS_READ,
> > PR_TRUE, getter_AddRefs(cacheEntry));
> > if (NS_FAILED(result))
> > {
> > cacheSession = nsnull; // is this necessary? if yes, i should
> > do it
> > for cacheEntry as well
>
> It's not necessary, no.
>
> > result = cacheService->CreateSession("image",
> > nsICache::STORE_ANYWHERE,
> > PR_TRUE, getter_AddRefs
> > (cacheSession)); // if this assignment, and the one below release the
> > previous pointers, then = nsnull is unnecessary
>
> |getter_AddRefs(cacheSession)| sets the nsCOMPtr to null as soon as you
> try to cast it to an nsICacheSession** (which is what this call does).
> So this code pattern is perfectly fine without manually setting to null.
>
Sweet.
> > nsISupportsVoid *wrapper;
> > componentManager->CreateInstanceByContractID
> > (NS_SUPPORTS_VOID_CONTRACTID, nsnull, NS_GET_IID(nsISupportsVoid),
> > (void**)&wrapper);
>
> |wrapper| is a COM object like any other. You probably just want to use
> nsCOMPtr<nsISupportsVoid> for it....
>
I am using this object after this method returns, therefore i can't have
its wrapper on the stack.
> > is the following the right way to reclaim memory?
> > memoryManager->Free(wrapper);
>
> No, since |wrapper| wasn't allocated by the memory manager... Like I
> said, it's just a refcounted object.
>
For some reason i thought it was allocated by memory manager, but it
does make sense now.
> > I.e., is memoryManager->Free(wrapper); the right way to do it, and will
> > it also Free the void * contained?
>
> No, destruction of the nsISupportsVoid will not do anything with the
> void* (and can't, in fact, since it doesn't know what concrete class it
> is and hence whether any destructors need calling); you have to manually
> free that memory (using the memoryManager, since that's how it was
> allocated) and possiby manually call destructors (if your struct has any).
>
OK, so i'll have to call Release on that pointer in the last method it's
used in.
> -Boris
> _______________________________________________
> Mozilla-xpcom mailing list
> [email protected]
> http://mail.mozilla.org/listinfo/mozilla-xpcom
> But now 2 more questions: I believe the C++ standard says that if new fails, an exception is thrown. Since exceptions are disabled in XPCOM, how can we detect the out of memory error? Does the compiler, perhaps, return 0 as it does for malloc when exceptions are disabled? Also, do i understand correctly that pointers via XPConnect, such as those coming from javascript (e.g., char *), should not be reclaimed in the component called? I see 2 reasons for that: 1. they weren't allocated in the component. 2. we don't know whether malloc or new was used (or do we?). Thanks.
signature.asc
Description: This is a digitally signed message part
