Reputation: 75
So this is my code:
#include <windows.h>
#include <iostream>
using namespace std;
int main(void) {
cin.get(); //MEMORY SNAP
HWND win = CreateWindow(
"static",
NULL,
WS_CHILD | WS_VISIBLE | SS_BITMAP | WS_DISABLED,
NULL,
NULL,
NULL,
NULL,
GetConsoleWindow(),
(HMENU)0,
GetModuleHandle(0),
NULL
);
cin.get(); //MEMORY SNAP
DestroyWindow(win);
cin.get(); //MEMORY SNAP
}
basically I'm only creating a window and then destroying it. So all the memory allocated by the window should be freed, but my memory looks like this -> http://oi67.tinypic.com/2rwrsxc.jpg it won't get fully deleted and i don't know why.
I'd be really happy if you could help me with this one.
Image from link:
Upvotes: 0
Views: 493
Reputation: 941505
While memory usage is not necessarily a good measure, the odds that a single release frees up enough contiguous VM space to make it worthwhile to return it to the OS are never good, the displayed allocation count should be trustworthy. The implementation detail that matters here is that windows are allocated in the desktop heap. A shared heap that is used by all processes that run on that desktop. It is not tracked by the memory profiler.
The allocation count increase you see is related to other things that happen when you create a window. Like the message queue that is needed to supply it with messages, it is dynamically allocated. It doesn't get released until the owner thread ends. Beware that the notion of a "queue" is just a mental model, the actual implementation is undocumented and much more involved. So much more than just one allocation.
Never fret about memory usage that is beyond your control. The OS can be trusted to not suffer from leaks.
Upvotes: 1
Reputation: 179819
You're looking at two different numbers. DestroyWindow
releases memory, but not directly back to the OS. It remains available to your program. Practically speaking, that means that the next CreateWindow
call does not increase memory consumption, as it recycles memory.
Upvotes: 5