Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Randomly changing variable

Credits go to BAF, CGamesPlay, GullRaDriel, Kitty Cat, Kris Asick, miran, nonnus29, Onewing, Richard Phipps, Sirocco, and Thomas Fjellstrom for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
Randomly changing variable
23yrold3yrold
Member #1,134
March 2001
avatar

Can we please brainstorm for a minute why an integer variable might spontaneously reset to zero? This thing gets explicitly assigned nowhere but in a SetState() function, which does not allow 0 to be assigned to it. Elsewhere, I have code to check if the value is ever equal to 0, and it occasionally is. The only reason I can think of that that might happen is if there's sort of buffer overrun out of an array, but this variable is cushioned by a large number of other single variables (ie: it's nowhere near an array in memory), and this happens in multiple objects. So I guess it's unlikely, but I can't think of what else might do that. Any other ideas?

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Onewing
Member #6,152
August 2005
avatar

Quote:

So I guess it's unlikely, but I can't think of what else might do that.

Neither can I. What all debugging process have you tried?

------------
Solo-Games.org | My Tech Blog: The Digital Helm

nonnus29
Member #2,606
August 2002
avatar

Sleep on it. Repeat if necessary. :P

BAF
Member #2,981
December 2002
avatar

Optimizations? Did you try disabling optimization or declaring it volatile?

GullRaDriel
Member #3,861
September 2003
avatar

Sometimes a buffer overflow fill variables with some strange values. The last pointer problem I had was caused by such a thing. ptr = NULL ; if( ptr == NULL ) do nothing ; but ptr wasn't NULL as a for() loop with a char toto[it] overwritten it.

Launch GDB, set a breakpoint at main, and go step by step, check the variable value.

Try launching your code trough valgrind.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Kris Asick
Member #1,424
July 2001

The first thing that comes to mind is you've done something like this:

void SetVarible (int var)
{
  var = 1;
}

void main (void)
{
  int v;
  SetVariable(v);
}

When you pass a pointer or variable to a function, it is copied into the function. If you want to change the value of a variable passed to a function, you need to make it a reference like this:

void SetVarible (int &var)

This counts for pointers as well. If you want to change where a pointer passed to a function is pointing to it needs to be a reference to a pointer.

If you don't make it a reference, any changes you make to the variable/pointer will only affect said function.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Sirocco
Member #88
April 2000
avatar

My vote goes for a buffer over/under run, or a boofed pointer. If all else fails look for uninitialized variables.

-->
Graphic file formats used to fascinate me, but now I find them rather satanic.

CGamesPlay
Member #2,559
July 2002
avatar

Valgrind would spot this in a few seconds ;)

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

miran
Member #2,407
June 2002

Quote:

Elsewhere, I have code to check if the value is ever equal to 0, and it occasionally is.

Does that code look like this:

if (evil_variable = 0) {
   printf("OMFG!");
}

?

--
sig used to be here

BAF
Member #2,981
December 2002
avatar

Well that wouldn't work. blah=0 always returns 0, which is false.

miran
Member #2,407
June 2002

Oh right... :-[

EDIT:

What about this:

if (evil_variable = 0) {
   printf("OMFG!");
}

// then somewhere else

if (evil_variable == 0) {
   printf("OMFG!");
}

:D

--
sig used to be here

BAF
Member #2,981
December 2002
avatar

That would work. :D

Kitty Cat
Member #2,815
October 2002
avatar

GDB has a way to watch memory addresses, so you can set a watchpoint on the variable's location, run the program, and it'll break whenever the memory is changed.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Richard Phipps
Member #1,632
November 2001
avatar

My vote goes for a bad pointer too!

KC's suggestion to find the problem sounds good too.. :)

Thomas Fjellstrom
Member #476
June 2000
avatar

My vote goes for bad pointer plus memory-corruption/buffer-overflow.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

Did you try disabling optimization or declaring it volatile?

It's not volatile. No special optimizations.

Quote:

When you pass a pointer or variable to a function, it is copied into the function.

Nah, I ain't that amateur. ;) The value is a private member that gets set nowhere except in its own private Set() function (one that checks for a value of 0, too). I assign to it nowhere else, but I reference it every loop and occasionally discover it's 0.

Quote:

If all else fails look for uninitialized variables.

Not that. I'm picky about always initializing.

Quote:

Does that code look like this:

Actually, I just output the variable to file. Says 0.

I'm still cleaning up the code and making sure there's no dirty hacks that might be doing this, so maybe it'll disappear on its own. To my shame, I'm not much of a debugger, I could never get stuff like GDB to work, so I don't know if I'll have to resort to that. Main thing that concerned me was if there was something else I hadn't though of, so I'll have to check all my array crap.

Quote:

Try launching your code trough valgrind.
....
Valgrind would spot this in a few seconds ;)

You Linux people can kiss my butt. ;)

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Richard Phipps
Member #1,632
November 2001
avatar

You could try Fortify. It's free and you can use it with GCC or MingW on Windows. I managed to get it working before, so you should have no problem.. ;)

Onewing
Member #6,152
August 2005
avatar

Most debuggers have a way to watch variables, even break once the variable changes. You can't just look through X amount of code and think that you've solved the problem manually.

What IDE are you using?

------------
Solo-Games.org | My Tech Blog: The Digital Helm

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

You can't just look through X amount of code and think that you've solved the problem manually.

If the variable stops reporting 0 after the code is finalized, I can be reasonably certain. :)

Quote:

What IDE are you using?

MinGW and SciTE.

I'll look into Fortify if I find the need.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Michael Jensen
Member #2,870
October 2002
avatar

This will totally fix it:

if (myvar==0)
{myvar=1;}

Obviously, you didn't read Worse Than Failure today...

:D

23yrold3yrold
Member #1,134
March 2001
avatar

That's basically the current remedy and it works fine, but I prefer knowing how it happened in the first place ...

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Michael Jensen
Member #2,870
October 2002
avatar

Well, at least I made myself laugh -- I guess that's all that matters. 8-)

Kris Asick
Member #1,424
July 2001

Well, depending on the amount of code between when you set it to non-zero and when it gets mysteriously reset to 0, you could always do something like this to try and spot where exactly it changes:

void DoStuff (void)
{
  allegro_message("A: Value = %d",myVar);
  myVar = 1;
  allegro_message("B: Value = %d",myVar);
  // next command goes here
  allegro_message("C: Value = %d",myVar);
  // next command goes here
  allegro_message("D: Value = %d",myVar);
  // next command goes here
  allegro_message("E: Value = %d",myVar);
  // next command goes here
  allegro_message("F: Value = %d",myVar);
  // next command goes here
}

It would be nice to see some code. Specifically, I want to see the code for where the variable is created, because my next suspicion is out-of-bound array access.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Richard Phipps
Member #1,632
November 2001
avatar

If this is a game, maybe display the contents of the variable every frame and see if any parts the game logic chnage it. If it changes straight away then it's probably something in your inner loop.

james_lohr
Member #1,947
February 2002

Are you using any sort of multi-threading?

Otherwise make a backup of your project, and then start ripping out chunks of it until the problem goes away. It's probably wise to find out what it is while it's occuring regularly. When you have the same problem, but it only occurs once a week - that is when you start getting worried.

 1   2 


Go to: