EXC_BAD_ACCESS on SDL_ConvertSurface Mac OS X

I am trying to load a bitmap image into a program with SDL. When I run the
following short program or whenever the function SDL_ConvertSurface() is
called the program stops responding. Through Xcode’s debugger I have found
that it is giving the error: EXC_BAD_ACCESS, which I have read has something
to do with memory, but I do not know how I can change how
SDL_ConvertSurface() deals with memory.

Here is my code:

#include “SDL/SDL.h”

int main( int argc, char *argv[] ){
// Screen surface
SDL_Surface *gScreen, *gTiles;

// Initialize SDL's subsystems
if (SDL_Init(SDL_INIT_VIDEO) < 0) 
{
	fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
	exit(1);
}

// Attempt to create a 320x240 window with 32bit pixels.
gScreen = SDL_SetVideoMode(320, 240, 32, SDL_SWSURFACE);

// Load tiles.bmp into the temp surface, then convert it to the format of

gScreen and save it to gTiles, then free the temp surface
SDL_Surface *temp = SDL_LoadBMP(“tiles.bmp”);
gTiles = SDL_ConvertSurface(temp, gScreen->format, SDL_SWSURFACE);
SDL_FreeSurface(temp);

return 0;

}

All this does is load a .bmp, convert it, save it to a new surface, then
free the temp surface and exit. Unfortunately when I run this all it does
is hang. When I step through it in the debugger I then get the
EXC_BAD_ACCESS error.

When looking up things to make sure I wasn’t asking a common question I
learned that I needed to add the SDL_Init() line before calling
SDL_ConvertSurface(), but that did not change my results.

I am using SDL 1.2.13 for Mac OS X Leopard 10.5.2 and Xcode 3.0. When
running programs with this same section of code in Windows I do not get this
error.

Thank you for your help,
-rob–
View this message in context: http://www.nabble.com/EXC_BAD_ACCESS-on-SDL_ConvertSurface-Mac-OS-X-tp15753087p15753087.html
Sent from the SDL mailing list archive at Nabble.com.

    // Load tiles.bmp into the temp surface, then convert it to

the format of
gScreen and save it to gTiles, then free the temp surface
SDL_Surface *temp = SDL_LoadBMP(“tiles.bmp”);
gTiles = SDL_ConvertSurface(temp, gScreen->format,
SDL_SWSURFACE);
SDL_FreeSurface(temp);

    return 0;

}

All this does is load a .bmp, convert it, save it to a new surface,
then
free the temp surface and exit. Unfortunately when I run this all it
does
is hang. When I step through it in the debugger I then get the
EXC_BAD_ACCESS error.

At which line do you get this error?

Is temp != NULL after SDL_LoadBMP ?

Is gTiles != NULL after SDL_ConvertSurface ?Am Samstag, den 08.03.2008, 11:12 -0800 schrieb thecommodore6:

Albert Zeyer wrote:

    // Load tiles.bmp into the temp surface, then convert it to

the format of
gScreen and save it to gTiles, then free the temp surface
SDL_Surface *temp = SDL_LoadBMP(“tiles.bmp”);
gTiles = SDL_ConvertSurface(temp, gScreen->format,
SDL_SWSURFACE);
SDL_FreeSurface(temp);

At which line do you get this error?

Is temp != NULL after SDL_LoadBMP ?

Is gTiles != NULL after SDL_ConvertSurface ?

This error arises on the line gTiles = SDL_ConvertSurface(…); when I try
to step over it in the debugger.

After SDL_LoadBMP, temp == NULL, which to me seems like it could be an
issue, I just don’t know how to change that.

I cannot check what gTiles is after SDL_ConvertSurface because the program
freezes at that line and will not continue.

Is the fact that temp == NULL the issue here? If so, how can I change that?

Thank you,
-rob–
View this message in context: http://www.nabble.com/EXC_BAD_ACCESS-on-SDL_ConvertSurface-Mac-OS-X-tp15753087p15921887.html
Sent from the SDL mailing list archive at Nabble.com.

If temp is null, doesn’t that mean that your tiles.bmp didn’t load? So, that’s
probably why things are running a muck.

hat are you using for a debugger? Make sure your program is run in the same
directory that contains tiles.bmp. Most debuggers have an option to se t the
working directory. Your other option would be to put the absolute path for
tiles.bmp. If temp is still null with the full path, than SDL_LoadBMP() might
not like your file (??).

AlvinOn Saturday 08 March 2008 19:23:26 thecommodore6 wrote:

Albert Zeyer wrote:

    // Load tiles.bmp into the temp surface, then convert it to

the format of
gScreen and save it to gTiles, then free the temp surface
SDL_Surface *temp = SDL_LoadBMP(“tiles.bmp”);
gTiles = SDL_ConvertSurface(temp, gScreen->format,
SDL_SWSURFACE);
SDL_FreeSurface(temp);

At which line do you get this error?

Is temp != NULL after SDL_LoadBMP ?

Is gTiles != NULL after SDL_ConvertSurface ?

This error arises on the line gTiles = SDL_ConvertSurface(…); when I try
to step over it in the debugger.

After SDL_LoadBMP, temp == NULL, which to me seems like it could be an
issue, I just don’t know how to change that.

I cannot check what gTiles is after SDL_ConvertSurface because the program
freezes at that line and will not continue.

Is the fact that temp == NULL the issue here? If so, how can I change
that?

Thank you,
-rob

Albert Zeyer wrote:

    // Load tiles.bmp into the temp surface, then convert it to

the format of
gScreen and save it to gTiles, then free the temp surface
SDL_Surface *temp = SDL_LoadBMP(“tiles.bmp”);
gTiles = SDL_ConvertSurface(temp, gScreen->format,
SDL_SWSURFACE);
SDL_FreeSurface(temp);

At which line do you get this error?

Is temp != NULL after SDL_LoadBMP ?

Is gTiles != NULL after SDL_ConvertSurface ?

This error arises on the line gTiles = SDL_ConvertSurface(…); when I try
to step over it in the debugger.

After SDL_LoadBMP, temp == NULL, which to me seems like it could be an
issue, I just don’t know how to change that.

I cannot check what gTiles is after SDL_ConvertSurface because the program
freezes at that line and will not continue.

Is the fact that temp == NULL the issue here? If so, how can I change that?

Of course that is an issue. It meens that there is no surface. Of course
SDL_ConvertSurface cannot convert nothing (NULL).

So SDL_LoadBMP cannot load the bmp. Returning NULL means that nothing
was loaded.

Probably SDL_LoadBMP doesn’t find the file. Ensure that the bmp is
really in the same directory from where you are starting the
application.Am Samstag, den 08.03.2008, 15:23 -0800 schrieb thecommodore6:

Alvin Beach-2 wrote:

If temp is null, doesn’t that mean that your tiles.bmp didn’t load? So,
that’s
probably why things are running a muck.

hat are you using for a debugger? Make sure your program is run in the
same
directory that contains tiles.bmp. Most debuggers have an option to se t
the
working directory. Your other option would be to put the absolute path for
tiles.bmp. If temp is still null with the full path, than SDL_LoadBMP()
might
not like your file (??).

Alvin

That was the issue. When I put in the full path to the file or when I put
the file in the same folder as the .app it worked. I guess I just need to
figure out how to reference the file where it’s saved within the .app
structure, which is an Xcode/Apple question and not in the scope of this
list. (unless you happen to know)

Thank you,
-rob–
View this message in context: http://www.nabble.com/EXC_BAD_ACCESS-on-SDL_ConvertSurface-Mac-OS-X-tp15753087p15922039.html
Sent from the SDL mailing list archive at Nabble.com.