-2

The problem

I've been coding a game in Microsoft Visual Studio 2010 Professional using C++ and Allegro 5.0.10 for the past year, and it's getting to the point where there isn't much left to do before release. However, when I build the game now, I run into random errors. By random errors, I mean sometimes the game runs without issue, and sometimes it begins to do things I haven't written into the code at all. Examples of this include displaying entire sprite sheets at (0,0); not moving the character, but changing the direction he/she is facing; moving the character one direction but displaying them walking the opposite way (think moonwalk). I'm fairly certain it isn't actually an error with the coding, as these were some of the initial functions I wrote into the code, and they have always worked.

The question

What could be some causes of random erros (that are not detected by the program/IDE) such as what I've mentioned above in coding (whether local to C++ or Allegro)?

  • About a million things. We'd need to know an awful lot about the way your program works to even begin speculating. I think this question is too broad for here. But probably some form of Undefined Behaviour. – BoBTFish Jun 13 '14 at 13:13
  • Adding to BoBTFish's comment, you are probably incorrect in assuming that the problem isn't with your code. _"They have always worked"_ is not a strong test for correctness. Especially in the face of [Undefined Behavior](https://en.wikipedia.org/wiki/Undefined_behavior). – Drew Dormann Jun 13 '14 at 13:23
  • I haven't heard of _Undefined Behavior_ before, but from the wiki page @DrewDormann posted, it sounds like it is related to allocating a variable, and then failing to define it. I did go back and check the code for examples of this, but I was unable to find any. Am I understanding what _Undefined Behavior_ is incorrectly? – user3737701 Jun 13 '14 at 14:17

2 Answers2

2

Sounds like something is touching memory that it shouldn't be. Run you program using a memory debugger like Valgrind and look for problems like.

Invalid write of size 8

Any invalid writes can cause the behavior you are describing.

user3736255
  • 121
  • 2
  • I went with [Visual Leak Detector](http://vld.codeplex.com/), but didn't find any invalid writes. 12 memory leaks, which I'll look into though. Memory leaks should just slow down the program though, not cause undefined behavior, correct? – user3737701 Jun 13 '14 at 14:25
  • To my knowledge VLD only checks for memory leaks and will not identify invalid writes. If you cannot run you code on Linux with Valgrind, then you can try using Dr. Memory (http://www.drmemory.org/), this will find invalid writes and other memory issues. Unless you are on a memory limited system, the memory leaks should not slow anything down. They can however cause allocations to fail over time, so it is a good idea to fix them sooner rather than later. – user3736255 Jun 13 '14 at 14:47
0

From your comment, allocating a variable, and then failing to [assign a value to] it is one correct example of many possible UB situations. Here is a larger (but still incomplete) list.

The most important thing to understand about Undefined Behavior is that you can not make any assumptions about what the code will do. Even the same code in the same compiled binary. It might work, it might misbehave, it might crash.

If you are using C or C++, Undefined Behavior will invalidate the "they have always worked" approach for testing a function's correctness.

Community
  • 1
  • 1
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • One of the suggestions in that thread for what could cause undefined behavior is modifying a string literal. I do this using the += operator, so is it possible the fault lies there? The issues started appearing relatively recently, so I'm looking heavily into recent code. – user3737701 Jun 13 '14 at 14:35
  • It looks like you're on the right track, although we're travelling away from the question that you posted. I suggest that you make sure all your compiler's warnings are enabled and fix all warnings that are reported. Post a new, specific question about your code if you have one. Good luck! – Drew Dormann Jun 13 '14 at 14:39