[C++] memory leak in std::string substr method?

Status
Not open for further replies.

Dave88

Ars Praefectus
3,583
Subscriptor
This one has me stumped. I have a small memory leak, but using the CRT alloc breaks, I get pointed to this method in my code:
Code:
void TrimStringLeft(string& str)
{
	int i = 0;
	int e = (int)str.size();
	for (i; i!=e; ++i)
	{
		if (!isspace(str[i]))
			break;
	}
	if (i!=0 && i<e)
		str = str.substr(i);
}
Specifically, the str = str.substr(i); line. Here's the rub - the callstack/alloc break ID is always different - every time I run the program, the leak warning will point to a different part of the program, but always in the TrimStringLeft method. So the program might be calling TrimStringLeft from a hundred different places, and it's seemingly random which one will cause the memory leak, but every time it's inside the substr method.

Occasionally it will throw up a different place for the alloc break:
Code:
p = str.find(",");
if (p==string::npos)
	break;
str = str.substr(p+1);
This is in a different, unrelated method, but once again it's the substr method that's causing the alloc break.

Now, I'm 99.9% sure there's no problem with the substr method - it's surely a far too commonly used method to cause memory leaks without someone noticing. Also, I've been working on this app for months, and the problem only showed up a couple of days ago. So something must have changed in my code that's somehow showing up as a leak in substr. I went over my recent code changes with a fine-toothed comb, trying to figure out what that might be, but every memory allocation I added has a matching deletion.

So now I'm all out of ideas - something, somewhere, is not getting freed or is causing corruption, but I can't for the life of me find it. I cold really use some clever ideas to track this problem down. Anyone?
 

levine

Ars Tribunus Militum
1,986
Code:
void TrimStringLeft(string& str)
Since it's being passed as a reference, is someone (another thread) fucking with your str underneath you? If feasible, you can (should) change the method signature to be
Code:
std::string TrimStringLeft(const std::string& str)
and return a copy. Your algorithm is also kind of long-winded...
Code:
std::string TrimStringLeft(const std::string& str)
{
    size_t cut(0);
    for (std::string::const_iterator it = str.begin(); it != str.end() && isspace(*it); ++cut, ++it);
    return str.substr(cut);
}
Complete example at pastebin.
(edit: and even with the extra allocation, my version has the benefit of being between 33% (no optimization) and 50% (-O3) faster; bonus!)
 

Dave88

Ars Praefectus
3,583
Subscriptor
I wasn't worried about optimizing that method, it's only used during startup.

It's not a threading issue (I think) as there are no threads running until after the CRT alloc break fires.

However, changing to using a copy of the string instead of reference has moved the goalposts somewhat. Now the CRT alloc break only breaks inside TrimStringLeft occasionally, and the rest of the time it's seemingly random where the break occurs. Which means this leak is going to be hell to find.
 

Dave88

Ars Praefectus
3,583
Subscriptor
Heisenbug, I like that, although at the same time I hate it immensely.

Fortunately I haven't added too much new code since the leak appeared, so maybe I will still have some hair left by the time I strip it all out, and slowly add it all back again.

edit: found the little bugger!

I came across a program called Memory Validator - it worked like a charm. Ran the program, and it listed the leak right at the top of the stats display, with a source file, line, and even a few lines of the source code to show me exactly what the problem was. Fantastic program, now I just need to save up and buy it (I used the free trial before anyone accuses me of pirating it).

If anyone cares, I was a proper idiot and forgot I had changed the size of a buffer without changing the buffer size limit checks. D'oh!
 
Status
Not open for further replies.