wierd c++ problem
I program in C++ and openGL and have an annoying bug. Once when I ran the program it crashed and I had to restat the computer, when I restarted the computer one of my header files had been turned into the assembly code and the screen looked like a hex editor. Any way to change this back and how did I cause it?
I then proceeded to load in my recent backup but the first time I built the program I had a 'non incremental link' warning, without doing anything I rebuilt the program and it came up fine. I have cleaned the program, re-built in differant ways but I can't replicate the warning.
Now, about every third time I run it the program crahes when executing the line
glwInstance->glwFinished = TRUE; //quits program
Yet this line is part of the program that has not been edited for months (when I started).
Someone e-mailed me and explained I should compile this program in 'release' mode. I did this and I have no more crashes, can anyone explain to me why?
> can anyone explain to me why?
You've moved the bug to somewhere else.
It's like a minefield - sometimes you can walk across it and nothing happens.
Release mode builds just move the mines around - it can't remove them.
> Yet this line is part of the program that has not been edited for months
This is a sure sign of trouble.
It usually means that you have some kind of memory access problem, typically resulting from
1. using uninitialised pointers
2. any kind of array overrun - either normal arrays, or arrays of dynamic memory
3. not releasing allocated memory at the correct time
4. using memory which has already been freed.
But you have a bug for sure - and you can be pretty sure it will surface again at some point in the future - the only difference is, you'll have more code to look through.
I wish you were wrong but my inner voice tells me your right :( I think I might go through the code with an instructor and see what he can find.
I have had a similar problem at work last week. I have some code that was in production for several months. When we modified the main header file most of our software uses, I had to recompile the program with the new .h. No major changes were done to the structures in the header file, but despite that my server stopped working, it would run once and after doing everything it should have done, would get a segmentation fault error.
After two days of debugging I found out that the lines that were causing the seg. fault, were actually the ones that were working for months before this.
What happened is this: by incorrectly using some of my pointers, every pass through a certain function would damage the memory heap of my program in such a way that all the pointers in the heap were destroyed.
The problem you are describing sounds very similar. What happenes in the release mode, is that the compiler packs your executable a bit differently than in debug mode, and because of that the conditions to recreate the previous error change. But if you add something to the program or remove something from it, the compiler will repack the executable again and the error might appear again.
Other than Pointers to arrays the only other pointer I know of in my program is
glwInstance->glwFinished = TRUE;
which is the quit screen, and this is where the program crashes.
If I saw more of the code using this pointer and also if I really wanted to debug another problem such as this in less than a week, I would definitely help you.
I wouldn't ask anyone on this board to help me debug a major problem like that because it would take up a lot of their time (which I don't think is fair). I have paid good money to do a course though so I'll get them to help me :)