-
found the function that fills the vector and it seems to be causing the problems. I use almost the same code as in this function within the program and that vector can be cleared without a problem. I've been looking at it for a while but can't pin point the problem. Could someone take a look at it, I must be missing something:
Code:
void ReadDataSec(FILE * inp, FSAD &Section , int nExpected)
{
char line[110];
int check;
int newline =0, endcount=0;
DataLine dl;
int size = nExpected*2 +1;
while ( fgets(line, sizeof line, inp) != NULL) // reads in a line of text from file
{
dl.Param = dl.Label=dl.SFactor=dl.SigBits=dl.Bit30=dl.Bit31=dl.Max=dl.Min = '\0';
newline =0;
if (strncmp(line, "\n", 1) == 0)
newline =1;
check = (sscanf(line, "%s %d %f %d %d %d %f %f", dl.Param, &dl.Label,
&dl.SFactor, &dl.SigBits, &dl.Bit30, &dl.Bit31, &dl.Max, &dl.Min));
if ( strncmp(dl.Param, "ENDL", 4) == 0 && newline ==0) // Exit if at the end of a section
{
dl.Label=dl.SFactor=dl.SigBits=dl.Bit30=dl.Bit31=dl.Max=dl.Min='\0';
Section.vectDL.push_back(dl);
break;
}
if ( strcmp(dl.Param, "END") == 0 && newline == 0) // set all fields to 0
at the end of subsection marker
{
dl.Label=dl.SFactor=dl.SigBits=dl.Bit30=dl.Bit31=dl.Max=dl.Min='\0';
Section.vectDL.push_back(dl);
endcount++;
}
if (strncmp(dl.Param, "END", 3) != 0 && newline ==0) // If the line is not
the end of subsection marker, copy the line into global vector
{
if (check == 8)
{
Section.vectDL.push_back(dl);
}
else if (check == 6)
{
dl.Max=dl.Min='\0';
Section.vectDL.push_back(dl);
}
}
}
if (endcount != nExpected)
EXIT_FAILURE;
}
-
The orange line in the above code was removed, and the error does not occure anymore. However, the file is not parsed properly now. I hope it won't take me another million hours to fix that.
I just don't understand why would resettign the structure variable would cause a amemory leak?
-
> dl.Param = dl.Label=dl.SFactor=dl.SigBits=dl.Bit30=dl.Bit31=d l.Max=dl.Min = '\0';
You should not be setting dl.Param to '\0'. Since this is a char array (assumption based on your sscanf()), you would lose the memory address. To clear dl.Param, use:
Code:
dl.Param[0] = '\0';
-
And since dl.Label and the rest are numbers, you should zero them with:
instead of:The two statements are equivalent, but the second is usually used for strings.
-
dl.Param is a CString.
and I'm using MS VC++ 6.0
-
I've amde the change from '\0' to 0, but I kept getting the demage error. So I figured that if I can't fix this problem I'll go around it. So I used an array of dl's instead of reusing and resetting the same one. This way I only use the data I just read into that cell and there is no demage error further on. That array is destryed automatically on the stack, so I don't have to worry about it.
I have to admit I really don't understand how clearing that variable would cause a demage problem with the vector being filled later on.
-
I don't believe it's a good idea to pass a CString directly to sscanf(). I would try something like:
Code:
check = (sscanf(line, "%s %d %f %d %d %d %f %f", dl.Param.GetBufferSetLength(sizeof(line)), &dl.Label,
&dl.SFactor, &dl.SigBits, &dl.Bit30, &dl.Bit31, &dl.Max, &dl.Min));
And then just leave out dl.Param in your clearing line:
Code:
dl.Label=dl.SFactor=dl.SigBits=dl.Bit30=dl.Bit31=dl.Max=dl.Min = 0;
-
Then I cannot call any other cString function until I call ReleaseBuffer(). and I'd like to work with it before I release it.
-
>Then I cannot call any other cString function until I call ReleaseBuffer()
I don't think that's true with GetBufferSetLength(), only with GetBuffer().
But irregardless, CString is a class, and cannot be passed directly to sscanf(). You can read a CString like a char array, but not write to one as a char array.
-
OK. I see. that makes a lot of sense, I always froget about it being a class in these cases.
Thanks.