Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Memory problems - Garbage in variables - NEW operator may be? 2

Status
Not open for further replies.

jaimito

Programmer
Jul 16, 2003
4
IL
Hello all!
I'm new in this group! Please help me with this: I wrote a big application that run others applications. It also has somethin like 4 objects. The problem is that a variable used in a loop, suddenly get impossible numbers; let' say: for (i=0;i<12;i++), suddenly I get i=12 or i=-12334454.
I assume that may be is a memory problem. In all objects used, I used the NEW operator in the constructor. An the constructor is called once in the beggining of the main program. The loop uses methods of the 4 objects, but in this method there is no NEW operator at all. May be the problem is in another command? I use DB commands, and also I used the command &quot;CreateProcess&quot;. But also used CloseHandle. PLEASE, can anyone help me with this??? I'm getting crazy!!!
Thank you all!
 
Yes is sounds like you are having a memory corruption problem. One of the most difficult problems to solve. It is very unlikely that message based conversations will help you locate the badness in your application.

-pete
 
What development environment are you using? Try stepping through the loop and watching the i variable.

To do this in VC++ 6.0 run your program in the debugger and put a breakpoint at the top of your loop. Put the variable 'i' into the watch window. Then just step over (F10) each line and watch to see when i changes to the impossible number. When you've identified the location in the for loop, you can use step into (F11) to go deeper into the code to find the specific place that the memory changes. You will probably have to run your program several times because once the variable i changes it is hard to go back and do it again without starting from the beginning.

If you are still having trouble pinning down the actual code that is called when the variable changes, there are other tricks you can do. For example, if you put &i in the watch window it will show you the address of the int. You can set a breakpoint to go off whenever the data at that location changes. Go to Edit->Breakpoints and switch to the Data tab, then use the address of the variable as the expression and set the number in the array to 4 (as in an int is 4 bytes). You can then run the program, and anytime your variable changes value the debugger will notify you and you can look at the call stack to identify the bad code.
 
Hello uolj and thank you all the others for their help!
I'm working on VC++ 6.0 on Windows.
Listen, uolj, the place where the variable change to an impossible number is the beggining of the loop, I already debugged this. I have nested loops like:

for (a=0;a<5;a++)
for (b=a+1;b<6;b++)
for (c=b+1;c<7;c++)
...................
.....................
for (h=g+1;h<12;h++)
{
body
}

So, after 210 inner iterations (the body loop), the variable c,..,h gets -23533355, or may be h=12, and c=13.


I know that is possible to see if there is a bad management of memory using the &quot;Winodws Task Manager&quot;, and then &quot;Processes&quot; and &quot;Mem Usage&quot;. Si I see that for each iteration of the main body in the loop,the memory oscillates, but tends to grow slowly. I think that this means that I'm not deallocating memory. But it is strnage, since all the NEW operators are before the for's. And after the for's (ouside the loops) the objects get destroyed.

Another idea?????????????

Thank very much!!!!!
 
When you say you debugged it, were you able to pin down the exact spot that changes the data using the techniques I suggested? If so, then you should have the line of code that when executed changes the variables from good to bad. Just look at that line of code (or post it) until you figure out what is wrong with it.

I noticed in your example that after the 210th execution of the body, the variable &quot;b&quot; is incremented for the first time. Assuming this is just pseudo code, look at the actual incrementing of &quot;b&quot; and make sure it is correct (or post it).

Finally, if you weren't able to pinpoint the exact line of code that is the culprit, then it might help to ask questions about the techniques I mentioned earlier. They are really valuable in narrowing down a problem from &quot;somewhere in my code&quot; to &quot;there!&quot;. I admit it takes a lot of patience, but it just might be the best way.
 
Hi uolj!
First of all, thank you vey much! You really calm me down. I was very nervous about this, sice I spent a lot of time doing this application. I also found the technique that you showed me using the &quot;Edit->Breakpoint&quot;, I always look for something like this in the past and never knew how to do it.
I really appreciate all your help, it gives me a lot of spiritual support.
The good news are that I tracked the bug. Although, when using f11, it always bring me to the &quot;for&quot; command, I found that it has nothing to do with my huge application. It seems to be a stupid begginer bug.
How I konw that?
I wrote an very short and simple application in C, a small one with nested loops, and run it. And...surpise! The problem (bug???) also appears here. Now, may be I forgot the few that I knew about basic programming, but it seems that I am doing something wrong at the basic level.
This is the simple code that I wrote:

int main(int argc, char* argv[])
{
int a,b,c,d,e,f,g,h;
int x=0;

for (a=0;a<2;a++)
for (b=a+1;b<3;b++)
for (c=b+1;b<4;b++)
for (d=c+1;c<5;c++)
for (e=d+1;d<6;d++)
for (f=e+1;f<7;f++)
for(g=f+1;g<8;g++)
for (h=g+1;h<9;h++)
{ x=x+1; }
return 0;
}

The values of a,b,c,d,e,f,g,h in the 'x=x+1' line respectively are:

0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 8
0 1 2 3 4 5 7 8
0 1 2 3 4 6 7 8
0 1 2 3 5 6 7 8 *****
................
an so on.

Bit instead of the line with starts, i get
0 1 2 4 4 5 6 7
This is impossible, not???
And after that I get another not true series.
I still don't know why in my original application, I also got values as -1232344, but let's keep the problem simple.
Do you know why this happends???

Thank you very much!!!

and then, go out the program.
But instead of exiting, I get this output:
 
Ohhh excuse me! I have a mistake in the code that I showed!!
it should be:

int main(int argc, char* argv[])
{
int a,b,c,d,e,f,g,h;
int x=0;
for (a=0;a<2;a++)
for (b=a+1;b<3;b++)
for (c=b+1;c<4;c++)
for (d=c+1;d<5;d++)
for (e=d+1;e<6;e++)
for (f=e+1;f<7;f++)
for(g=f+1;g<8;g++)
for (h=g+1;h<9;h++)
{ x=x+1; }
return 0;
}

So, the loops works fine! But.. This means that still I have a memory allocation problem in my code?? Ohhh, my god!!!
Ok, I will check it again..but probably I many need your help. Thank you very much!!!
 
>the NEW operators are before the for's. And after the for's (ouside the loops) the objects get destroyed.

So do you really need new at all?

/Per
Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
Another way to get at this sort of thing is to look at the body piece of code where you have the fluctuating memory usage (which you find odd since there should be no memory allocation/deallocation during that time) and comment out chunks of it. So you can comment out the whole thing and see if your for-loops run OK. Then de-comment a bit, then a bit more etc. etc.
But you have my sympathy. This sort of error can be hard to track down.
 
You could use a breakpoint with a condition on it :

F9 - toggles the breakpoint on a line
Alt-F9 - allows you to add a condition for that breakpoint (press the condition button)
then type in the box : h < 0
then press ok, ok again and run your program.

the debugger will stop at the break point only when the expression evaluates to true.

hope this helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top