Memory leak or no Memoryleak... this is the question

Started by
9 comments, last by Lunatix 12 years, 10 months ago
Hey =)

I have a sidebar in my modeleditor, and there's a label "Memory usage". In the update function i do something like:

void cVmSidebar::onUpdate(){

static wstringstream wInfo;
wInfo.str("");
wInfo.clear();

wInfo<<(cCore::GetAllocatedMemory() / 1024)<<"Kb";
lpMemLabel->bSetWindowText(wInfo.str().c_str());

}


If i do so, allocated memory runs up and up and up... if i use something like:

void cVmSidebar::onUpdate(){

char buffer[10];
_itoa_s((cCore::GetAllocatedMemory()/1024), buffer, 10);

lpMemLabel->bSetWindowTextA(buffer);

}


it runs good, memory don't increases. Doesn't matter if i type "static wstringstream wInfo" or "wstringstream wInfo".
cCore::GetAllocatedMemory:

size_t cCore::GetAllocatedMemory(){

HANDLE process = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS meminfo;

if (GetProcessMemoryInfo(process, &meminfo, sizeof(PROCESS_MEMORY_COUNTERS))){
return meminfo.WorkingSetSize;//PagefileUsage;
}

return 0;
}


Is this a memory leak? And... why is this happening?
Advertisement
What compiler version are you using? I remember something about a leak in the stringstream classes being fixed in a particular version of Visual Studio. Perhaps this is relevant?
Visual Studio 2010 Express, Version 10.30319.1 RTMRel (Latest)
What result do you get if you make the wstringstream variable a regular stack-based variable instead of being static?
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
The OP said: Doesn't matter if i type "static wstringstream wInfo" or "wstringstream wInfo".. It is unclear if this means promoting it to be a global or making it a non-static local. I hope the latter.
Though it probably won't help in your case, using boost::lexical_cast rather than manually manipulating a stream can avoid memory leaks in some implementations; the boost library includes workarounds for things like the MSVC 2005 stream bug.
What is lpMemLabel->bSetWindowText function doing?
Does it make copy of string passed to it? Do you need to free it manually?
Looking at the working set isn't a very good way of detecting leaks. The working set is "a collection of those pages in its virtual address space that have been recently referenced". See http://msdn.microsoft.com/en-us/library/ms684891%28v=vs.85%29.aspx

You probably want to use the Debug Heap to look for leaks instead.
[quote=rip-off]The OP said: Doesn't matter if i type "static wstringstream wInfo" or "wstringstream wInfo".. It is unclear if this means promoting it to be a global or making it a non-static local. I hope the latter.[/quote]

>>I meant the following:
void xyz(){

static wstringstream wInfo;
wInfo.str("");
wInfo<<"Test"

}

void xyz_w(){

wstringstream wInfo;
wInfo<<"Test"

}


I'm not realy shure, that the "static" Keyword is good or not for performance, it was "test-wise".

[quote=MartinsM]What is lpMemLabel->bSetWindowText function doing?
Does it make copy of string passed to it? Do you need to free it manually? [/quote]

This is a function in the User-Interface part of my engine. It simply calls the MSDN function "SetWindowText" with the widgets HWND handle.

[quote=Adam_42]Looking at the working set isn't a very good way of detecting leaks. [...] You probably want to use the Debug Heap to look for leaks instead. [/quote]

It was not my goal to display the debug heap or else, i simply like to show the current size of allocated memory by my process, like in the taskmanager. I searched google for this and found some posts, describing the usage of PROCESS_MEMORY_COUNTERS. Is there a better way to display the allocated memory ?

If i look in the taskmanager, the used memory for my process increases too. Only if i comment "wstringstream wInfo" out, the memory usage stays constant...
Thanks for clarifying. Maybe I did miss that part in the original post, but even reading it now I would not have been certain as to that actual meaning. I was meaning when the .str and clear were removed also.

"Task Manager"?! *shudders* That awful thing?
No Windows developer should ever be without "Process Explorer" from SysInternals. Heck it's the first thing that goes on any newly installed system of mine, right after the OS. The "Private Bytes" column of that should give you a much better insight as to how much memory is allocated by the application.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement