On Sun, 2005-06-05 at 23:14 -0400, Boris wrote:
> Reshat Sabiq wrote:
> > I'm assigning into the same smart pointer assigned into by method A, by
> > method B if method A fails. Since method A failed, i expect the pointer
> > to not be add-refed by method A. However, i'd like to make sure that
> > that is in fact the case, because smart pointer destruction only calls
> > release once.
> 
> But assigning into a smart pointer (assuming you mean nsCOMPtr or 
> nsRefPtr) releases whatever the previous value was... Otherwise using 
> smart pointers would be well-nigh impossible.  Unless you're doing 
> something _really_ odd with the pointer....  What does the code look like?
> 
> -Boris
> _______________________________________________
> Mozilla-xpcom mailing list
> [email protected]
> http://mail.mozilla.org/listinfo/mozilla-xpcom
> 
The code i was wondering about first, and which you apparently have
answered looks as follows (i haven't verified the source, but your
answer makes sense: new assignment releases the previous pointer; my
questions are marked in inline comments):
        ...
        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
                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
                if (NS_FAILED(result)) return result;

                result = cacheSession->OpenCacheEntry(url, 
nsICache::ACCESS_READ,
                                          PR_TRUE,  getter_AddRefs
(cacheEntry));

I now have a second question as well:
For the code below

                        nsISupportsVoid *wrapper;
                        componentManager->CreateInstanceByContractID
(NS_SUPPORTS_VOID_CONTRACTID, nsnull, NS_GET_IID(nsISupportsVoid),
(void**)&wrapper);
                        if (!wrapper)
                                return NS_ERROR_UNEXPECTED;
                        
                        MYSTRUCT *ms = (MYSTRUCT 
*)memoryManager->Alloc(sizeof(MYSTRUCT));
                        ...
                        result = wrapper->SetData(ms);
is the following the right way to reclaim memory?
                        memoryManager->Free(wrapper);

I.e., is memoryManager->Free(wrapper); the right way to do it, and will
it also Free the void * contained?

Thanks a bunch!

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to