   Crash in Debug mode only
  Hi!I'm developing a game engine in c++ and this weird bug appeared. In Visual studio 2008, release mode, everything runs fine. But in debug mode it crashes.Settings settings;settings.init();In settings.init i read values from a file and assign them to private variables. Values like width, height and such. While the function is running these values are correct and equal to what is in the file. But when init returns those values become garbage like 809454854. When i try to use width and height like that, it crashes, of course.And this only happens in debug mode. In release, init returns and the values are still there, as expected.I would appreciate any help. Thanks!   
  Heya Pombal,It sounds like you have some code in there that's corrupting memory.Some common causes of that are...1) Writing out of bounds in an array (ie if it's only 5 elements big and you write to element 10)2) You forget to initialize a pointer to null, and then start writing data to it anyways3) You are casting a pointer to a type that it isn't, and then using it as if it wasIf this doesn't help, one thing you could do is step through your init() function and see at what point inside of there the values go crazy.  It might not til you exit the function, but if you can find the culprit that way that should be helpful.If you still can't figure it out, you could post your code and maybe people could help you more (:By the way, in debug mode there is a lot more data in the executable than in release.  This is for debug code type stuff, as well as because in debug mode you don't have optimizations turned on.Because of this, if you write to the 10th element of an array that's only 5 elements big, in release you might over-write a different variable than you do in debug which results in different behaviors - IE crash or no crash.Also in debug mode, the compiler does stuff like reserve extra memory and write some known numbers into that memory.  Then, if that memory is every something other than the number it put in there, it knows you are doing something that is corrupting memory and will throw an error (that looks like a crash) letting you know that there is a problem.   ;  I'll take "What does the source of init() look like?" for $200.   ;  Atrix256, shouldn't those errors appear in release mode too?here is the code for init, i've been messing around with for some time to catch the bug so the code is now a horrible mess, i'm sorry:void Settings::init(){std::string line;std::ifstream myfile ("config.ini");getline (myfile,line);getline (myfile,line); std::string s = line.substr(13);if(s.compare("BASS") == 0)audioLib = BASS; if(s.compare("AUDIERE") == 0)audioLib = AUDIERE;getline (myfile,line);std::string s1 = line.substr(6);std::istringstream stream(s1);int width = 0;stream >> width;getline (myfile,line); std::string s2 = line.substr(7);std::istringstream stream1(s2);int height = 0;stream1 >> height;myfile.close();}config.ini:[EngineSettings]AudioLibrary=BASSWidth=1024Height=768Thanks   ;  the issue of debug / release is ...1) Debug code to detect memory corruption etc isn't present in release mode.  It's only in debug mode because it slows down your code and makes it use more memory.  Release mode is there to cut out all that stuff so that you are left with a high speed, trim application.  It assumes you thoroughly tested your program and all (most) of the bugs are worked out so that it doesnt need to do the checks.2) the memory layout of your program is different in release and debug mode.  This means that when you write to Blah[10] when it's only 5 elements big, what gets over-written is different in release mode than in debug model.  In release mode it may just be over-writing a variable that you use as a loop counter in a loop that's already done and is harmless.  in debug mode it may be over-writing something more important, such as removing the null character off the end of a string you care about hehe.you are lucky to have hit a debug only bug... in my experiences, it's been much more common to find release only bugs (which are difficult to debug for obvious reasons)So no, a bug or a crash in one mode doesn't mean that it should necessarily happen in the other mode, and it's common to find problems that don't act the same in both flavors :P   ;  I noticed you said "width and height" are messed up upon returning from the function. That's not really surprising if this is your actual code.You never assign any width or height member variables.If you actually have member variables in the Settings class that is named "width" and "height", you are still not assigning anything to them since you redefine them locally just before writing to themSince you have redefined them they will be trashed when the function goes out of scope.class Settings{ public:  int width; int height;};void Settings::Init(){  int width = 0;  width = 128;}The variable width will only be 128 during the scope of the function, when the function returns the locally defined "width" will seize to exist. And I believe it's only pure luck that makes it work in release mode. In debug all allocated data is initialized to 0xcdcdcdcd or such. In release they only take the memory that happens to be at that location. In your case that memory probably contains something that's valid width/heights.The code:int width = 0;stream >> width;will probably be optimized away in release mode all together since it serves no purpose.[Edited by - panic on June 2, 2010 3:33:38 AM];  What a stupid error to make. Late hours in the night and this is what happens...I was misled by the functional release mode and thought it was a severe bug.Thanks for all your answers.   