Sign in to follow this  
Lunatix

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

Recommended Posts

Hey =)

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

[code]void cVmSidebar::onUpdate(){

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

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

}[/code]

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

[code]void cVmSidebar::onUpdate(){

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

lpMemLabel->bSetWindowTextA(buffer);

}[/code]

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

[code]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;
}[/code]

Is this a memory leak? And... why is this happening?

Share this post


Link to post
Share on other sites
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 [url="http://connect.microsoft.com/VisualStudio/feedback/details/98861/2005-crt-memory-leaks-std-basic-iostream-affects-std-stringstream-std-fstream-probably-others"]this[/url] is relevant?

Share this post


Link to post
Share on other sites
The OP said: [b]Doesn't matter if i type "static wstringstream wInfo" or "wstringstream wInfo".[/b]. It is unclear if this means promoting it to be a global or making it a non-static local. I hope the latter.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 [url="http://msdn.microsoft.com/en-us/library/974tc9t1%28v=vs.80%29.aspx"]Debug Heap[/url] to look for leaks instead.

Share this post


Link to post
Share on other sites
[quote=rip-off]The OP said: [b]Doesn't matter if i type "static wstringstream wInfo" or "wstringstream wInfo".[/b]. 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:
[code]void xyz(){

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

}

void xyz_w(){

wstringstream wInfo;
wInfo<<"Test"

}[/code]

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 [url="http://msdn.microsoft.com/en-us/library/974tc9t1%28v=vs.80%29.aspx"]Debug Heap[/url] 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...

Share this post


Link to post
Share on other sites
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"?! [i]*shudders*[/i] 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.

Share this post


Link to post
Share on other sites
Okay, i downloaded it - thanks for the tip =)
But a same as taskmanager, the memory usage increases... i think i try an other way to update the text in that label...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this