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.
> 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.
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.
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 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?
,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 :)
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 :)?
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.