Jump to content
  • Advertisement
Sign in to follow this  
Lunatix

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

This topic is 2567 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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:

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?

Share this post


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

Share this post


Link to post
Share on other sites
What result do you get if you make the wstringstream variable a regular stack-based variable instead of being static?

Share this post


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

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
What is lpMemLabel->bSetWindowText function doing?
Does it make copy of string passed to it? Do you need to free it manually?

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 Debug Heap to look for leaks instead.

Share this post


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

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"?! *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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!