View Full Version : My memory leaks
Indiepath
05-04-2007, 11:53 AM
Any ideas?
CComBSTR p("blank");
char * token;
while (msg.message != WM_QUIT) // Message and COmmand Pump
{
if (PeekMessage(&msg,0,0,0,PM_NOREMOVE))
{
if(GetMessage(&msg,NULL,0,0))
{
// PostMessage(pThis->parent, msg.message, msg.wParam, msg.lParam);
if (msg.message != WM_RBUTTONUP)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
// ** the next line is causing a memory leak
pThis->html->get_title(&p);
/*
LOTS OF NICE LEAK-FREE CODE REMOVED FROM HERE
*/
//BLAH
// SysFreeString(p);
Sleep(1);
}
pThis->brow->Release();
pThis->unkn->Release();
return;
}
gearface
05-04-2007, 12:08 PM
I have no idea what your code is doing, but a first guess is are you supposed to free "p" after you get it?
EDIT: should have read more. I'm guessing "SysFreeString(p)" frees "p".
- Jeremiah
bignobody
05-04-2007, 12:19 PM
Could the leak be occurring elsewhere? (ie: inside the pThis->html->get_title method?)
MikeVitt
05-04-2007, 12:41 PM
I've never used this type of string, but aren't you allocating it once and then deallocating it every iteration within the WHILE loop? I would guess that is the problem.
Indiepath
05-04-2007, 12:50 PM
pThis->html->get_title(&p); I found this on MSN
Automation (previously called OLE Automation and ActiveX Automation) caches the space allocated for BSTR strings. This may cause IMallocSpy to attribute memory leaks to the wrong BSTR user in an application that uses Automation's BSTR strings. The correct source of the leak can be found by disabling Automation's cache by setting the environment variable OANOCACHE=1 before running the application. In many server-based applications, the BSTR cache is not required. There is a hotfix available to disable BSTR caching on Windows 2000 computers. Windows XP and Windows Server 2003 do not require the hotfix to disable BSTR caching. But I am using XP to develop the application? I have not attempted to set the enviroment variable yet since I should not be getting the leak. (this is a client based application with an embedded browser window).
The code snippit above is a Win32 API call, I have no control over it's memory usage.
Indiepath
05-04-2007, 12:51 PM
I've never used this type of string, but aren't you allocating it once and then deallocating it every iteration within the WHILE loop? I would guess that is the problem.
I've taken out the deallocation, sorry that was an attempted debug that also failed.
I have no suggestions for your memory problem, sorry.
But I was curious about something if you don't mind dropping me some info.
What is the point/benefit of calling PeekMessage and then calling GetMessage immediately afterwards?
Just for my curiousity -- I don't mean to derail your thread.
MikeVitt
05-04-2007, 01:07 PM
I found some info about a possible memory leak at this link (http://msdn2.microsoft.com/en-us/library/bdyd6xz6(VS.80).aspx#programmingwithccombstr_memor yleaks). It's hard to tell if this is the case since I don't know what get_title does, but I thought I would post this info anyway.
*****************************
Passing the address of an initialized CComBSTR to a function as an [out] parameter causes a memory leak.
In the example below, the string allocated to hold the string "Initialized" is leaked when the function OutString replaces the string.
CComBSTR bstrLeak(L"Initialized");
HRESULT hr = OutString(&bstrLeak);
To avoid the leak, call the Empty method on existing CComBSTR objects before passing the address as an [out] parameter.
Note that the same code would not cause a leak if the function's parameter was [in, out].
Indiepath
05-04-2007, 01:19 PM
I have no suggestions for your memory problem, sorry.
But I was curious about something if you don't mind dropping me some info.
What is the point/benefit of calling PeekMessage and then calling GetMessage immediately afterwards?
Just for my curiousity -- I don't mean to derail your thread.
I need to peek as the loop is not message dependent, ie. other events may be fired within the thread as a result of actions outside the dll.
** I've removed code - the code I've removed from this post does not contain a leak.
Indiepath
05-04-2007, 01:20 PM
I found some info about a possible memory leak at this link (http://msdn2.microsoft.com/en-us/library/bdyd6xz6%28VS.80%29.aspx#programmingwithccombstr_m emoryleaks). It's hard to tell if this is the case since I don't know what get_title does, but I thought I would post this info anyway.
*****************************
Passing the address of an initialized CComBSTR to a function as an [out] parameter causes a memory leak.
In the example below, the string allocated to hold the string "Initialized" is leaked when the function OutString replaces the string.
CComBSTR bstrLeak(L"Initialized");
HRESULT hr = OutString(&bstrLeak);
To avoid the leak, call the Empty method on existing CComBSTR objects before passing the address as an [out] parameter.
Note that the same code would not cause a leak if the function's parameter was [in, out].
Sounds interesting, thanks Mike I will test this in the morning - I am hoping the overall result will be of benefit to you all.
Indiepath
05-05-2007, 12:42 AM
Got there eventually, it's not just using a BSTR as an out parameter but also convertion using the W2A macro!
Here is my fix :
p.Empty();
pThis->html->get_title(&p);
char * message = new char[p.Length()];
wcstombs(message, p, p.Length());
/* Blah Blah Blah */
delete [] message;
vBulletin v3.6.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.