O/H Boris Zbarsky έγραψε:
On 11/3/09 1:53 PM, Georgios Petasis wrote:
It might help to carefully trace through the functions you're
executing. I can't tell from here which they are, but for example
Browser::GetEncoder leaks |doc|. Browser::SaveDocument leaks |document|.
Can you please suggest what is the correct way to call GetEncoder?

It's not how you call it. It's what the function does:

1208 nsIDOMDocument *doc;
1209 mNavigation->GetDocument(&doc);

That call just incremented the refcount by 1.

1210 if (doc) doc->AddRef();

And now you've incremented it by 2.

1211 NS_ConvertASCIItoUTF16 newContentType(formatType);
1212 rv = encoder->Init(doc, newContentType, encodingFlags);
1213 if (doc) doc->Release();

And now it's back to only being incremented by 1. But that 1 is never released anywhere I see. I have no idea why you aren't using nsCOMPtr here; it's designed to prevent just such mistakes.
Dear Boris,

Thank you very much for the tip. I tried to use nsCOMPtr, but I got compilation errors (GetDocument didn't accept a nsCOMPtr value), so I simulated what nsCOMPtr would have done. Maybe a getterAddRef is needed if I use a nsCOMPtr call?
How would you write such a call?

Am I still leaking |doc|?

Yes, see above.
To say the truth, I didn't know that GetDocument kept a reference to the provided doc. Is there a list of methods that behave in a similar way?

Thank you,

George
_______________________________________________
dev-embedding mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-embedding

Reply via email to