views:

162

answers:

3

Hello there.

I have a Win32 C++ Application. There is the _tWinMain(...) Method with GetMessage(...) in a while loop at the end. Before GetMessage(...) I create the main window with

HWND m_MainHwnd = CreateWindowExW(WS_EX_TOOLWINDOW | WS_EX_LAYERED, CAxWindow::GetWndClassName(), _TEXT("http://www.-website-.com"), WS_POPUP, 0, 0, 1024, 768, NULL, NULL, m_Instance, NULL);

ShowWindow(m_MainHwnd)

If I do not create the main window, my application needs about 150K in memory.

But with creating the main window with the WebBrowser Control inside, the memory usage increases to 8500K.

But, I want to dynamically unload the main window. My _tWinMain(...) keeps running! Im unloading with

DestroyWindow(m_MainHwnd)

But the WebBrowser control won't unload and free up it's memory used!

Application memory used is still 8500K!

I can also get the WebBrowser Instance or with some additional code the WebBrowser HWND

IWebBrowser2* m_pWebBrowser2;

CAxWindow wnd = (CAxWindow)m_MainHwnd;

HRESULT hRet = wnd.QueryControl(IID_IWebBrowser2, (void**)&m_pWebBrowser2); 

So I tried to free up the memory used by main window and WebBrowser control with (let's say it's experimental):

if(m_pWebBrowser2) 
   m_pWebBrowser2->Release();

DestroyWindow(m_hwndWebBrowser);  //<-- just analogous

OleUninitialize();

No success at all.

I also created a wrapper class which creates the main window. I created a pointer and freed it up with delete:

Wrapper* wrapper = new Wrapper();
//wrapper creates main window inside and shows it
//...do some stuff
delete(wrapper);

No success. Still 8500K. So please, how can I get rid of the main window and it's WebBrowser control and free up the memory, returning to about 150K. Later I will recreate the window. It's a dynamically load and unload of the main window, depending on other commands.

Thanks!

Regards

Martin

A: 

The memory isn't always released exactly when you want it to be. There may be some things that the class is doing in the background that it must wait for before the memory can be entirely deleted. Its also possible that the memory has been freed but the kernel holds it as assigned to your process under the assumption that your process will probably want to use it in a bit anyway.

If you create another web browser window after you've destroyed the previous does the memory jump up a further 8350K or does it stay more-or-less the same?

Goz
A: 

You have what is known as a reference leak. Before you call ::Release() on your IWebBrowser2*, see how many references are open. If it's not 1, then something else somewhere has a reference to your object.

Also, don't forget to IWebBrowser2::Quit() your control...

Agoln
A: 

Hello. Thanks for your reply.

Goz you are right. When I recreate the window, the memory does not jump a 8500K further. It only jumps about 600K. But this is continiously on every recreation. So the memory will sum up to 9100K, 9700K and so on.

To what Agoln said, I have done a m_pWebBrowser2->Quit() before Release(), and my only reference is the pointer m_pWebBrowser2. But memory won't decrease.

That's the most importing thing the project requests me to do. To build a lightweight host which runs forever (that's _tWinMain(...)) and which loads und unloads the main window and webbrowser control dynamically. On unload I have to free up their memory! That's important. But I'm stuck.

Martin

Martin