Memory leak

E

eduard.antonyan

All I'm doing is going through the webpage and analyzing the data,
here's the outline of the code:

in OnDocumentComplete(LPCTSTR lpszURL)
{
IDispatch *pDispatch = GetHtmlDocument();
IHTMLDocument2 *pDoc;
IHTMLElement *pBody;
BSTR str;

pDispatch->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc);
pDispatch->Release();

pDoc->get_body((IHTMLElement **)&pBody);
pDoc->Release();

pBody->get_innerHTML(&str);
pBody->Release();

//---------------------------
// Analyze the str using wcsstr, Navigate2 depending on the results.
//---------------------------

SysFreeString(str);

CHtmlView::OnDocumentComplete(lpszURL);
}

The above results in a memory leak so what am I doing wrong?

I've tried calling CHtmlView::OnDocumentComplete(lpszURL) before I do
everything else and calling SysFreeString before doing Navigate2..
Can't think of anything else..:(
 
J

Jochen Kalmbach [MVP]

Hi eduard!
All I'm doing is going through the webpage and analyzing the data,
here's the outline of the code:

in OnDocumentComplete(LPCTSTR lpszURL)
{
IDispatch *pDispatch = GetHtmlDocument();
IHTMLDocument2 *pDoc;
IHTMLElement *pBody;
BSTR str;

pDispatch->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc);
pDispatch->Release();

pDoc->get_body((IHTMLElement **)&pBody);
pDoc->Release();

pBody->get_innerHTML(&str);
pBody->Release();

//---------------------------
// Analyze the str using wcsstr, Navigate2 depending on the results.
//---------------------------

SysFreeString(str);

CHtmlView::OnDocumentComplete(lpszURL);
}

The above results in a memory leak so what am I doing wrong?

HOW do you detect the memory leak?
Also be aware, that COM uses some "intelligent string pooling", so
"SysFreeString" does not necessarily release the memory!

Greetings
Jochen
 
G

goharik

Thanks for reply.

Well, using the obvious way - task manager (it can grow relatively
fast).. Or crt dumping.

So any way of checking whether SysFreeString releases the memory or
not? And also any ways of making it release the memory (or going around
that problem..:-\)?
 
E

eduard.antonyan

Thanks for reply.

Well, using the obvious way - task manager (it can grow relatively
fast).. Or crt dumping.

So any way of checking whether SysFreeString releases the memory or
not? And also any ways of making it release the memory (or going around
that problem..:-\)?
 
E

eduard.antonyan

Thanks, I'll check it out.

In task manager - yes, both actually.

Actually it seems to me that it leaks regardless of the code, i.e. just
the CHtmlWindow on its own. After a few hundred clicks it already
increases by ~40K (browsing the same pages btw, IE 7).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top