Thread: It seems GCC likes my code better than VS 2010

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    14

    Unhappy It seems GCC likes my code better than VS 2010

    Basically, my code is just meant to load a BMP file, just to test SDL in Visual Studio. It works in Code::Blocks with MingW, and it worked in VS up until I added
    Code:
    Uint32 colorkey = SDL_MapRGB(img->format, 0xFF, 0, 0xFF); //declare the key
    SDL_SetColorKey(img, SDL_SRCCOLORKEY, colorkey); // set the key
    VS is pointing towards the key's declaration line, and saying "Unhandled exception at 0x00d9158a in SDL_Tutorial.exe: 0xC0000005: Access violation reading location 0x00000004." My entire program:
    Code:
    #include <SDL.h>
    
    
    int main( int argc, char* args[] )
    {
    	//Start SDL
        SDL_Init( SDL_INIT_EVERYTHING );
    	//vars
    	SDL_Surface *img;
    	SDL_Surface *buffer;
    	SDL_Rect crop;
    	SDL_Rect loc;
    	crop.x = 0;
    	crop.y = 0;
    	crop.w = 32;
    	crop.h = 32;
    	loc.x = 280;
    	loc.y = 280;
    
    	
    
       
        
    	//load image onto screen
    	img = SDL_LoadBMP("snakey.bmp");
    	Uint32 colorkey = SDL_MapRGB(img->format, 0xFF, 0, 0xFF);
    
    	SDL_SetColorKey(img, SDL_SRCCOLORKEY, colorkey); //make magneta invisible
    	buffer = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
    	SDL_BlitSurface(img, &crop, buffer, &loc);
    	SDL_Flip(buffer);
    	SDL_Delay(2000);
    	//Quit SDL
    	SDL_FreeSurface(buffer);
    	SDL_FreeSurface(img);
        SDL_Quit();
        
        return 0;    
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Perhaps SDL_LoadBMP simply fails? Check return values.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    14

    Question Well, I must've done something

    Well, I edited it a little, to this:

    Code:
    #include <SDL.h>
    
    
    int main( int argc, char* args[] )
    {
    	//Start SDL
        SDL_Init( SDL_INIT_EVERYTHING );
    	//vars
    	SDL_Surface *img;
    	SDL_Surface *buffer;
    	SDL_Rect crop;
    	SDL_Rect loc;
    	crop.x = 0;
    	crop.y = 0;
    	crop.w = 32;
    	crop.h = 32;
    	loc.x = 280;
    	loc.y = 280;
    
    	
    
       
        
    	//load image onto screen
    	img = SDL_LoadBMP("snakey.bmp");
    	Uint32 colorkey = SDL_MapRGB(img->format, 0xFF, 0, 0xFF);
    
    	SDL_SetColorKey(img, SDL_SRCCOLORKEY, colorkey); //make magneta invisible
    	buffer = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
    	SDL_BlitSurface(img, &crop, buffer, &loc);
    	SDL_Flip(buffer);
    	SDL_Delay(2000);
    	//Quit SDL
    	SDL_FreeSurface(buffer);
    	SDL_FreeSurface(img);
        SDL_Quit();
        
        return 0;    
    }
    but this only runs if I run the executable directly. Is that normal?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    By checking return values I meant something like this:

    Code:
    img = SDL_LoadBMP("snakey.bmp");
    if (!img) {
        //output error message and quit
    }
    It looks from the exception that your program is dereferencing a NULL pointer (or rather an offset of 4 bytes from NULL), presumably img being NULL.

    The cause of the problem might be that different IDEs may set a different working directory when you run a program from the IDE, and the file doesn't happen to be in that directory. See if giving a full path to the image file helps and see if the IDE lets you set that path to wherever the executable (and the image) is.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    Sorry, I replied right after I posted without checking for other posts, so...latepost
    but yeah, ill try that
    Last edited by rocko384; 02-09-2011 at 05:38 PM. Reason: adding something I forgot

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    I did that, and I have it exiting if it didn't load (doesn't do much, but atleast it tells me what's going on). I tried adding the file - snakey.bmp - as a Resource file, but that didn't work either. It only works if i run the EXE without VS, from the debug folder.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    Fixed it

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It means that your app is being launched with a different working directory when being run through the debugger. This is very common.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling GNU MP
    By mattnp12 in forum C Programming
    Replies: 3
    Last Post: 06-23-2011, 03:58 PM
  2. Compile Linux Kernel and GCC Version!
    By galactus51 in forum Linux Programming
    Replies: 0
    Last Post: 01-06-2011, 09:02 PM
  3. Understading assembly code created by gcc
    By Nazgulled in forum C Programming
    Replies: 3
    Last Post: 04-30-2006, 09:58 AM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

Tags for this Thread