Uninitialized variable destroyed program

So ive been creating a simple game on SDL and had a few problems here and there but nothing serious and ive solved most of them.
Anyway i added a bool variable to one of my classes just to check a condition but i forgot to initialize it.
So i ran the program and it crashed which i realised it would right after clicked build and run.
So i just closed the exe and then initialised the variable.
It crashed again.
I deleted the variable.
And it worked.
Added it again and it crashed.
Deleted it and added
 
bool test = 0;

at the start of main and it crashed again.
So i was just wondering if its accessed some memory its not supposed to have and corrupted something.
Because i literally cannot change one line of my code now without it crashing even though it should work.
Also i can post my code if needed but there is like maybe 700 lines of code.
It is a magic variable!:) Be caution with it!
Does anyone maybe have a solution ?
> Also i can post my code if needed but there is like maybe 700 lines of code.
The code would probably be the cause.
Also, you need to modularize and test them properly.

Run through a debugger and see the exact line of the crash.
Okay so it only does it if i add a variable to a certain class

WORKS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef GRAPHICS_H_INCLUDED
#define GRAPHICS_H_INCLUDED
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>


class Graphics
{
private:
    static SDL_Surface* Screen;
    static const int BPP                = 32;
    static const int ScreenHeight  = 600;
    static const int ScreenWidth   = 800;
//=========================================
    SDL_Surface* Image;
//Animation Variables
    int FrameHeight, FrameWidth, NumFrames, TimePerFrame , CurrentFrame;
//

public:
    SDL_Rect* Frame;
    Graphics();
     static void InitGraphics();
     static void UpdateScreen();
     void LoadImage(std::string Filename);
     void ApplySurface( int x, int y, SDL_Rect* clip  = NULL );
     //Animation Functions
     void SetFrames(int Num,int Height,int Width,int Time);
     bool Animate(int x);
    void AnimateOff();
     void AnimateOn();

};

DOESNT WORK
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef GRAPHICS_H_INCLUDED
#define GRAPHICS_H_INCLUDED
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>


class Graphics
{
private:
    static SDL_Surface* Screen;
    static const int BPP                = 32;
    static const int ScreenHeight  = 600;
    static const int ScreenWidth   = 800;
//=========================================
    SDL_Surface* Image;
//Animation Variables
    int FrameHeight, FrameWidth, NumFrames, TimePerFrame , CurrentFrame, test;//New Variable

public:
    SDL_Rect* Frame;
    Graphics();
     static void InitGraphics();
     static void UpdateScreen();
     void LoadImage(std::string Filename);
     void ApplySurface( int x, int y, SDL_Rect* clip  = NULL );
     //Animation Functions
     void SetFrames(int Num,int Height,int Width,int Time);
     bool Animate(int x);
    void AnimateOff();
     void AnimateOn();

};


Also I know this is probably stupid but i dont know how to use the debugger properly.
I use codeblocks but it doesn't bring up the normal window when i debug so i cant click the anything and see why it crashes.
Does Anybody have any useful suggestions on what to do or any idea why this has happened ?
Last edited on
What you showed has nothing common with your problem.

Check whether a macro with name test is defined.
Last edited on
It does though.
and i don't mean that in a nasty way.

Its just that i am able to change code in my other files but not this one.
Even something as trivial as adding a new variable causes it to crash.
I am not doing anything that disobeys the syntax of C++.
Nor am i calling the variable i declared in my second example.
Therefore the program should not crash as there is no difference in the code that is run except one more variable is declared.
As it only happened after i tried to call an uninitialized bool i was guessing that was the problem.


So i debugged it
With the extra variable that causes the crash
it ran through perfectly but then got a Segmentation error when i exited.
Without the extra variable that causes the crash
It crashes when reaching the graphics cpp which it doesnt do if i build and run it which is odd.

Also after looking around the internet for the problem many people were saying it was due to overflow of arrays or something
i dont have any that im aware of but im not fully sure how the SDL_Variables as implemented.

Im sorry for being a nuisance :(
backtrace, at least.

> im not fully sure how the SDL_Variables as implemented
Why do you look at the speck of sawdust in your brother’s eye and pay no attention to the plank in your own eye?
Ive backtraced a lot.
But ive solved the problem.
Well ive found a solution anyway.
i dont think its the most practical though :(.

After captioning out pieces of code to find the problem for the last hour, i found that the code causing the problem was.
1
2
3
4
Frame->h = FrameHeight;
Frame->w = FrameWidth;
Frame->y = 0;
Frame->x = (CurrentFrame * FrameWidth) 

and changing it to
1
2
3
4
5
6
  
Clips.h = FrameHeight;
Clips.w = FrameWidth;
Clips.y = 0;
Clips.x = (CurrentFrame * FrameWidth) ;
 Frame = &Clips;

,after creating another Rect called Clips inside my class,solved it.
So im guessing it was something to do with the pointer.
I have no idea why it only crashed when i added a new variable to the class though.

If anyone could shed some light it would be helpful so i dont make the same mistake again in the future :)
¿how did you initialize `Frame'?
So `Clips' is a SDL_Rect object, ¿why is `Frame' a pointer?

PS: backtrace http://ftp.gnu.org/old-gnu/Manuals/gdb-5.1.1/html_node/gdb_42.html
Last edited on
Sorry i didnt know it was a technical term i thought you just meant look over haha.
I only started to learn C++ like a month ago so i dont know quite a few things
I also dont know how to get the call stack up either.
and I havent really done much debugging.

Also it was a pointer because as i said before i dont actually know how the SDL variables are implemented and after following LazyFoos Tutorials i had already made functions to take a pointer and they worked.
Obviously im going to have to rethink this approach though if i encounter more problems.
Will it make no difference to my program if i change all the rects to regular variables ?
Does that also mean i dont have to have pointers to SDL_Surfaces aswell ?
Also are you spanish :)?
1
2
3
4
5
void foo(int *); //it takes a pointer
int main(){
   int n=42;
   foo(&n); //we pass a pointer
}
As you see, the function expects a pointer and that's what we pass it to it.

I don't know about SDL, but if it let you create objects, do it. I suspect that your issues were because you didn't initialize the pointers, or make them point to local variables (also if you allocate dynamically you must clean up)
There are cases like SDL_GetVideoSurface() that returns a pointer, as you want to work with that surface and not a copy.


PS: hablo castellano, no soy gallego.
Topic archived. No new replies allowed.