   Very confusing bug (SDL in Code::Blocks)
  I'm getting a segmentation fault in a game I am writing but in trying to track it down I've gotten some very very confusing results.  Here is the code snippet which I believe to be relevent://part of a switch case block obviously, also part of an SDL event handling structurecase SDL_MOUSEBUTTONDOWN:{if(event.button.button == SDL_BUTTON_LEFT){    graphics.leftMouse(1, 2);  <------ This Line}break;}} // end switch} graphics.drawScreen();I receive the error when I include that particular line of code denoted above.  Here are the wierd things:  1) Courtesty of backtracing I have discovered that leftMouse returns correctly but that the error occurs when I attempt to draw the screen (specifically with the SDL_Blit function).2) leftMouse(int x, int y) does not do ANYTHING currently, it is completely empty.  The entirety of the function is this:void Graphics::leftMouse(int x, int y){}3) If I do not run leftMouse, I do not receive an error and the program runs as expected.4) If I modify leftMouse to this:void Graphics::leftMouse(int x);and call that function, the program executes as expected with no errors.  The same for a function that takes no parameters, but a function that takes 2 or more parameters causes an error.I am very confused, because the statement in question doesn't actually DO ANYTHING that should cause an error, and it certainly doesn't modify any of the info that the drawScreen method uses... So any help with what do to to solve this would be appreciated...   
  Further Weirdness:1)  I've discovered that I get the same error from calling any function at any point within the event handling structure if the function being called takes two or more parameters.  I made a simple dummy function defined as:void test(int q, int r);and moved it around to different places with regular integer inputs and wherever I put it, the next time the screen attempted to Draw I would get a segmentation error.2)  Here is the call stack I get when backtracing:#0 68110FDASDL_SetTimer() (C:\WINDOWS\system32\SDL.dll:??)#1 6812842ASDL_LowerBlit() (C:\WINDOWS\system32\SDL.dll:??)#2 681286A6SDL_UpperBlit() (C:\WINDOWS\system32\SDL.dll:??)#3 0040660AGraphics::applySurface(this=0x22fca0, x=0, y=0, source=0xca7098, destination=0x22fbe4, clip=0x0) (C:/.../Graphics2.h:200)#4 00405C7BGraphics::drawScreen(this=0x22fca0) (C:/.../Graphics2.h:88)#5 00406B25SDL_main(argc=1, argv=0xa13fe0) (C:/.../main.cpp:111)(I took out the last few lines of various windows functions that are irrelevent to this case)So, the wierdness continues... why exactly is SDL_SetTimer being called??  And even if the SDL_Blit function does in fact call SDL_SetTimer for some reason I don't understand, why is it only failing when I run an essentially functionless piece of code?I apologize for clogging up the forum with this, but I'm basically bashing my head against a wall at this point, lol.  I think my next option is to start a new program and copy and paste my code piece by piece while running that above function at every step of the way  =/   ;  Can you give us a complete, minimal source code example (one that we can test) that exhibits this behaviour?   ;  Make sure you do not return the address of an automatic ("stack") variable somewhere in your program.  It could be anywhere.   ;  Try this (I threw this together and am not 100% sure I got rid of every single dependency, if there is something I missed let me know):Here is main.cpp:#ifdef __cplusplus    #include <cstdlib>#else    #include <stdlib.h>#endif#ifdef __APPLE__#include <SDL/SDL.h>#else#include <SDL.h>#endif#include <vector>#include "Graphics2.h"#include "SDL/SDL_image.h"using namespace std;int main ( int argc, char** argv ){    // initialize SDL video    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )    {   printf( "Unable to init SDL: %s\n", SDL_GetError() );   return 1;    }    atexit(SDL_Quit);    SDL_Surface* screen = SDL_SetVideoMode(960, 600, 16,   SDL_HWSURFACE|SDL_DOUBLEBUF);    if ( !screen )    {   printf("Unable to set 640x480 video: %s\n", SDL_GetError());   return 1;    }    Graphics graphics(*screen);    // program main loop    bool done = false;    while (!done)    {   // message processing loop   SDL_Event event;   while (SDL_PollEvent(&event))   {  // check for messages  switch (event.type)  { // exit if the window is closed  case SDL_QUIT: done = true; break;  case SDL_MOUSEBUTTONDOWN: {if(event.button.button == SDL_BUTTON_LEFT){    //graphics.leftMouse(1, 2);}else if (event.button.button == SDL_BUTTON_RIGHT){    graphics.rightMouseClick();}break; }  } // end switch   } // end of message processing   // finally, update the screen :)   graphics.drawScreen();   SDL_Flip(screen);    } // end main loop    printf("Exited cleanly\n");    return 0;}and here is the minimal version of graphics2 that still produces the error:#include <SDL/SDL.h>#include "SDL/SDL_image.h"class Graphics{    private:    SDL_Surface *mainScreen;    SDL_Surface *tempScreen;    int cursorOffset;    public:    Graphics(SDL_Surface screen);    void drawScreen();    void leftMouse(int mouseX, int mouseY);    void leftMouse();    void applySurface( int x, int y, SDL_Surface* source, SDL_Surface* destination,   SDL_Rect *clip);};Graphics::Graphics(SDL_Surface screen){    mainScreen = &screen;    tempScreen = loadImage("backgroundMask.png");}void Graphics::drawScreen(){    applySurface(0, 0, tempScreen, mainScreen, NULL);}SDL_Surface &Graphics::loadImage(string filename){    SDL_Surface *loadedImage = NULL;    SDL_Surface *optimizedImage = NULL;    loadedImage = IMG_Load(filename.c_str());    if (loadedImage != NULL)    {   optimizedImage = SDL_DisplayFormat(loadedImage);   SDL_FreeSurface(loadedImage);   Uint32 colorkey = SDL_MapRGB(optimizedImage ->format, 255, 255, 255);   SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorkey);    }    return optimizedImage;}void Graphics::applySurface( int x, int y, SDL_Surface* source, SDL_Surface* destination,   SDL_Rect *clip){    //Make a temporary rectangle to hold the offsets    SDL_Rect offset;    //Give the offsets to the rectangle    offset.x = x;    offset.y = y;    //Blit the surface    SDL_BlitSurface( source, clip, destination, &offset );}void Graphics::rightMouseClick(){}void Graphics::leftMouse(int mouseX, int mouseY){}That should do it.  I've commented out everything but that in my code and I'm still recieving the error.  Everything should work correctly until you left click on the screen, then, if its the same as mine it will crash.  If it doesn't crash then at least I know there is something screwed up in my configuration.;  Well I haven't finished going through your code, but you do some fishy stuff with your screen surface in the Graphics constructor. SDL_SetVideoMode returns a pointer to the main screen surface. You dereference this and pass that to the Graphics constructor making a copy of the surface. You then take the address of this copy which is different from the original address you were given and use that as your screen surface. When the constructor returns the local copy is destroyed and you are pointing to a random chunk of memory. Change your constructor to this:Graphics(SDL_Surface * screen){    // perform check here for valid surface if necessary    mainScreen = screen;    // ...}And create a Graphics object like this:    SDL_Surface* screen = SDL_SetVideoMode(960, 600, 16,   SDL_HWSURFACE|SDL_DOUBLEBUF);    if ( !screen )    {   printf("Unable to set 640x480 video: %s\n", SDL_GetError());   return 1;    }    Graphics graphics(screen);There are possibly other issues but that was the big one I noticed.   ;Quote:Everything should work correctly until you left click on the screen, then, if its the same as mine it will crash.After fixing a few problems with undeclared methods, it worked. And I had to change the code in these lines:SDL_Surface *tempScreen;tempScreen = loadImage("backgroundMask.png");SDL_Surface& Graphics::loadImage(string filename){...}How did you do that? Returning a reference and assigning it to a pointer? Did that even compile?   ;  Why not have the graphics class initialise the screen?Why is the graphics class responding to input?   ;Quote:Original post by rip-offWhy not have the graphics class initialise the screen?Why is the graphics class responding to input?;Quote:Original post by fcoelho*** Source Snippet Removed ***How did you do that? Returning a reference and assigning it to a pointer? Did that even compile?That was definitely an error in transferring, not sure how it popped up but that is definitely supposed to return a pointer (that method is directly from the lazy foo tutorials, so yeah).Quote:Original post by nobodynewsWell I haven't finished going through your code, but you do some fishy stuff with your screen surface in the Graphics constructor. SDL_SetVideoMode returns a pointer to the main screen surface. You dereference this and pass that to the Graphics constructor making a copy of the surface. You then take the address of this copy which is different from the original address you were given and use that as your screen surface. When the constructor returns the local copy is destroyed and you are pointing to a random chunk of memory. This fixed the immediate problem, it seems so obvious having it pointed out, lol.  Just love the fun ways errors like that decide to express themselves.I do have a question about the input handling.  For the most part my graphics class doesn't handle input, I fixed it up to do that little thing for the purposes of this post without having to include another class.  But that said, I've never been quite sure on where input handling fits into things ideally.  This game is a turn based strategy game with a largely menu driven interface, so for the most part the keyboard and mouse input feeds directly into the menu class and/or my game_management class, depending on the situation (I do have a cursor object, which is basically just a point on the map which I take care of directly in the graphics class, but that's a special case).So I guess my question is, is that an acceptable setup?  If not, who should the various inputs be handled?Aside from that, thank you all very much for the help!  :)Anyway though, thanks all for the help   