Program recieved signal: EXC_BAD_ACCESS

Discussion in 'Mac Programming' started by TheLutariFan, Nov 25, 2011.

  1. TheLutariFan, Nov 25, 2011
    Last edited: Dec 1, 2011

    macrumors newbie

    Joined:
    Nov 25, 2011
    #1
    Hello everyone.

    I am pretty new to programming and I have started to use c++ with SDL on Xcode.

    I used the SDL application Template, and added the SDL.ttf framework to add text to my program. I used code from some tutorials as a start.

    Here is my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <SDL/SDL.H>
    #include <SDL_ttf/SDL_ttf.h>
    
    void DrawPixel(SDL_Surface *screen, int x, int y,
    			   Uint8 R, Uint8 G, Uint8 B)
    {
    	Uint32 color = SDL_MapRGB(screen->format, R, G, B);
    	switch (screen->format->BytesPerPixel)
    	{
    		case 1: // Assuming 8-bpp
    		{
    			Uint8 *bufp;
    			bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
    			*bufp = color;
    		}
    			break;
    		case 2: // Probably 15-bpp or 16-bpp
    		{
    			Uint16 *bufp;
    			bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
    			*bufp = color;
    		}
    			break;
    		case 3: // Slow 24-bpp mode, usually not used
    		{
    			Uint8 *bufp;
    			bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
    			if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
    			{
    				bufp[0] = color;
    				bufp[1] = color >> 8;
    				bufp[2] = color >> 16;
    			} else {
    				bufp[2] = color;
    				bufp[1] = color >> 8;
    				bufp[0] = color >> 16;
    			}
    		}
    			break;
    		case 4: // Probably 32-bpp
    		{
    			Uint32 *bufp;
    			bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
    			*bufp = color;
    		}
    			break;
    	}
    }
    
    void Slock(SDL_Surface *screen)
    {
    	if (SDL_MUSTLOCK(screen))
    	{
    		if (SDL_LockSurface(screen) <0)
    		{
    			return;
    		}
    	}
    }
    
    void Sulock(SDL_Surface *screen)
    {
    	if (SDL_MUSTLOCK(screen))
    	{
    		SDL_UnlockSurface(screen);
    	}
    }
    
    void DrawScene(SDL_Surface *screen)
    {
    	Slock(screen);
    	for(int x=0;x<640;x++)
    	{
    		for(int y=0;y<480;y++)
    		{
    			DrawPixel(screen, x,y,y/2,y/2,x/3);
    		}
    	}
    	Sulock(screen);
    	SDL_Flip(screen);
    }
    
    
    int main(int argc, char *argv[])
    {
    	using namespace std;
    	if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) <0)
    	{
    		printf("Unable to init SDL: %s\n", SDL_GetError());
    		return 1;
    	}
    	
    	atexit(SDL_Quit);
    	
    	SDL_Surface *screen;
    	
    	screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
    	
    	if (screen == NULL)
    	{
    		printf("Unable to set 640x480 video: %s\n", SDL_GetError());
    	}
    	
    	TTF_Init();
    	atexit(TTF_Quit);
    	TTF_Font *fntCourier = TTF_OpenFont("Fonts\\Courier New.ttf", 12);
    	SDL_Color clrFg = {0,0,255,0};
    	SDL_Surface *sText = TTF_RenderText_Solid(fntCourier, "Courier 12", clrFg);
    	SDL_Rect rcDest = {0,0,0,0};
    	SDL_BlitSurface(sText,NULL, screen,&rcDest);
    	
    		
    	SDL_FreeSurface(sText);
    	TTF_CloseFont( fntCourier );
    	int done=0;
    	
    	while(done == 0)
    	{
    		SDL_Event event;
    		
    		while ( SDL_PollEvent(&event) )
    		{
    			if ( event.type == SDL_QUIT )  {  done = 1;  }
    			
    			if ( event.type == SDL_KEYDOWN )
    			{
    				if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
    			}
    		}
    		
    		DrawScene(screen);
    	}
    	
    	return 0;
    }
    
    This program should print "Courier 12" to the screen, and wait for keypress to exit program. I used code from: http://www.gamedev.net/page/resources/_/technical/game-programming/sdl--fonts-r1953
    So I don´t think the code itself is wrong, i maybe typed something weird.

    It compiles fine, and then opens a window. However, it is blank, and it freezes.

    GDB says:

    Program received signal: “EXC_BAD_ACCESS”.
    sharedlibrary apply-load-rules all

    I´ve got no idea, please help me! :(
     
  2. macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #2
    I'm not a c++ programmer so it's kind of hard for me to help you with your code, but in Xcode, when you receive a EXC_Bad_ACCESS message it usually means there's a memory leak. Maybe you're trying to access something that is no longer alive.
     
  3. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    Time to learn some basic debugging. Set a breakpoint on the first line of main, run the program, step through it line by line, look at all the variables after each line to see if there is anything you don't expect.

    My guess is that fntCourier == NULL.
     
  4. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #4
    That's the opposite of a memory leak, actually. Memory leaks cannot (in general) cause crashes.
     
  5. BlkBear, Nov 30, 2011
    Last edited: Nov 30, 2011

    macrumors member

    Joined:
    Jul 13, 2004
    #5
    A memory leak is when you forget to free memory but no longer have a pointer to said memory (the memory is essentially lost).

    the bad access signal is like a segmentation fault. The program is accessing memory that is not assigned to the program. Either it is accessing memory that has been freed or it is using an uninitialized pointer or some other invalid address (pointer arithmetic error).

    To debug this the original poster should start by running the program in gdb and look at the stack trace when the signal is thrown and work from there. They should be able to find out which pointer is invalid and go from there. may need to setup breakpoints to watch the pointer, or it might be obvious once they find out where the error is occurring.

    Figuring this out with the tools available will be a good exercise.
     
  6. jared_kipe, Nov 30, 2011
    Last edited: Nov 30, 2011

    macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #6
    EDIT: nvm I don't think that pointer arithmetic is actually wrong..
     
  7. TheLutariFan, Dec 1, 2011
    Last edited: Dec 1, 2011

    thread starter macrumors newbie

    Joined:
    Nov 25, 2011
    #7
    EDIT: Hmm, not fixed yet...

    I might have something going on though:

    I stepped through it line by line, worked fine until this line:

    Where it received the EXC_BAD_ACCESS.

    So yes, you might have a point with that fntCourier == NULL.

    Now the problem is, how do I fix this?
     
  8. macrumors 6502

    Joined:
    Apr 24, 2008
    #8
    The obvious thing to check is: Do you really have a "Courier New.ttf" file in the "Fonts" directory, which should be a subdirectory of the cwd?

    Another thing is that I'm not sure the backslash as a path separator works; if you're sure the file is there (and "Fonts" is a subdir of where you're running your app from), check whether it works if you substitute the \\ with a single /.
     
  9. thread starter macrumors newbie

    Joined:
    Nov 25, 2011
    #9
    Ok, fixed that, mistake -.-

    Yup i didnt have the file there.


    Uhhhh... now the problem is...

    I´m getting a blue/yellow/black window which works perfectly, but it doesnt do what it should; it doesnt print "Courier 12" to the screen. :(
     
  10. macrumors 603

    Joined:
    Aug 9, 2009
    #10
    Apply basic debugging skills: draw something other than text to the window. I suggest drawing the simplest thing that could possibly work.
     
  11. thread starter macrumors newbie

    Joined:
    Nov 25, 2011
    #11
    So I abandoned that for a while, but now I'm back with it. I created a program which draws a background image and two sprites, then tried to apply text to the screen, but it's still not working...
     
  12. thread starter macrumors newbie

    Joined:
    Nov 25, 2011
    #12
    Never mind, I fixed it.
     

Share This Page