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:
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:
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?
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);
}
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);
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?