# [C++] Two memory leaks I don't understand... at all

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

## Recommended Posts

Exhibit A: void clearBestScoreFile(std::string filename) { std::fstream t_File(filename); if(t_File.is_open()) { t_File.write("", 0); } t_File.close(); } "std::fstream t_File(filename)" apparently causes a memory leak. I can't get a file to open without causing a memory leak; I've tried using a straight string ("text.txt"), a c style string (char t_Buf[16]) and using open() instead of opening in the constructor, but it always leaks... :/ Exhibit B: float t_Minutes = 0; std::cout << "Init done. "; while(t_Minutes <= 0) { std::cout << "How many minutes should I evolve for?\n\n"; if(!(std::cin >> t_Minutes)) //if it failed { std::cin.clear(); std::cin.ignore((std::numeric_limits<std::streamsize>::max()), '\n'); std::cout << "\nInvalid input. Minutes must be a valid number (eg 5, or 4.3)\n\n"; } else if(t_Minutes <= 0) { std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cout << "\nInvalid input. Minutes must be greater than 0.\n\n"; } } if(!(std::cin >> t_Minutes)) leaks memory. I've also tried moving std::cin >> t_Minutes to another line, and just doing if(!(std::sin)), as well as if(std::cin.fail())... can't get rid of this one either. It's annoying the crap out of me because they seem like such simple things to use. Anyone have any ideas?

##### Share on other sites
What compiler are you using, and what are you using to detect memory leaks?

##### Share on other sites
How do you know it's causing a memory leak in the first place?

##### Share on other sites
Oops, I totally forgot to give that info!

I'm using Microsoft Visual C++ 2008 SP1, and to detect the leaks I've used Deleaker, as well as _CrtDumpMemoryLeaks(). Both seem to report the same leaks (at least _CrtDumpMemoryLeaks() seems to report that there are 2 leaks, same as Deleaker - it doesn't provde me with source files or line numbers)

As an aside though, Deleaker also reports memory leaks in another of my applications to do with CreateWindowEx and DirectX 9's CreateDevice functions, and I'm 100% sure I'm releasing them properly. If it wasn't for _CrtDumpMemoryLeaks() also reporting leaks, I would maybe consider that Deleaker is simply wrong...

##### Share on other sites
It takes a bit more than _CrtSetDbgFlag to enable VC++ memory detection:
// Include before any headers are included in every translation unit// (or in the precompiled header):#define _CRTDBG_MAP_ALLOC#include <stdlib.h>#include <crtdbg.h>#define new DEBUG_NEW#define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )// And post this as the first line of your entry point function:// _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

Post the full source for a minimal program sample that demonstrates these memory leaks.

##### Share on other sites
Where are you performing the leak check? Don't forget that some memory (including static allocations etc.) is not released until after main() exits.

##### Share on other sites
In addition to what _fastcall suggested, if you really want to find the true line of a memory leak, use _CrtMemCheckpoint or something like that. I don't remember the exact function name. But it allows you to compare the heaps at two different points of time. Chances are there is static allocation going on in your program.

For example, put all the code that you think is leaking into a loop. Does the memory of your program grow without bound, or does it "leak" by the same amount as if you had just executed the offending lines once?

##### Share on other sites
Sorry, I don't know how to do a code box on this forum. I didn't know that about the CRT memory debugging, thanks for the info.

As far as I know, Deleaker hooks in after the end of main, though I'm not 100% sure.

Here is full source code that will produce the leaks:

#include <string>

#include <iostream>
#include <fstream>
#include <limits>

void clearBestScoreFile(std::string filename)
{
std::fstream t_File(filename.c_str());

if(t_File.is_open())
{
t_File.write("", 0);
}

t_File.close();
}

int main(int argc, char* argv[])
{
clearBestScoreFile("test.txt");

float t_Minutes = 0;
std::cout << "Init done. ";

while(t_Minutes <= 0)
{
std::cout << "How many minutes should I evolve for?\n\n";
if(!(std::cin >> t_Minutes)) //if it failed
{
std::cin.clear();
std::cin.ignore((std::numeric_limits<std::streamsize>::max()), '\n');

std::cout << "\nInvalid input. Minutes must be a valid number (eg 5, or 4.3)\n\n";
}
else if(t_Minutes <= 0)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

std::cout << "\nInvalid input. Minutes must be greater than 0.\n\n";
}
}

system("pause");
}

Thanks

##### Share on other sites
What happens if you open two files (one after the other)? My guess (and it's only a guess, mind you) is there may be some kind of pooling going on in the backend of the stream class (perhaps for the stream buffer).

This is one of the pitfalls of memory leak detection - there are often a lot of false-positives. I would suggest that in general*, it's not worth stressing about one or two leaked buffers. It's only when you're leaking kilobytes per second or more that you'll actually bother about it.

* A server that's supposed to be up 24x7 is different to a game that you'll only be playing for a few hours at a time.

##### Share on other sites
I agree, it's not really much to worry about (the leaks are of about 128 bytes in total), but it's more academic than anything else. It frustrates me not knowing why this is leaking - I'm not really fussed about it in this instance, it's more that I'm worried it's a problem I'll run into time and time again.

As for opening two files, one after the other, I get 2 memory leaks. Oddly, if I open 3, I still only get 2 memory leaks. Is that confirmation that it's probably a false positive?

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 13
• 9
• 9
• 15
• ### Forum Statistics

• Total Topics
634072
• Total Posts
3015341
×