# SDL_UpperBlit: passed a NULL surface

## Recommended Posts

Ok, so I have a function to apply surfaces to whatever but whenever it's called it's giving me the error "SDL_UpperBlit: passed a NULL surface". I don't see how it's being sent a NULL surface because I have checks after all the surfaces are updated to see if the surface equals NULL and it gets passed there perfectly fine; All surfaces are global.
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, clip, destination, &offset);
}

void Button::show()
{
apply_surface(box.x, box.y, buttonSheet, screen, image);
}

void Tile::show()
{
apply_surface(box.x, box.y, isometricSQ, screen, &tile_clips[type]);
}

##### Share on other sites
It could be a number of things. The code you posted is fine, so the error must be elsewhere.

When you update the surface, are you sure that you are updating the global surface and not a local copy by mistake?

Are you perhaps writing past the bounds of an array and into your surface?

You should put a check in the apply_surface function so you can see which surface is NULL and post the code where it is created.

##### Share on other sites
Run your code in a debugger. See what the stack trace says when you get that error.

##### Share on other sites
Use a debugger. A simple way would be to dereference each surface. This will cause an access violation, which when run in debugger will allow you to get a backtrace, which will make obvious which surface is NULL. Add a line like "offset.w = source->w * destination->h" to your apply_surface function (SDL_BlitSurface only cares about the source rectangle's x and y values IIRC).

You have posted the wrong code anyway. None of those functions directly modify the surface pointers, so they cannot be the cause*. The problem is more likely in your setup code. Perhaps you forgot to call it (happens the best of us [smile].

*If only this were true. In C++, one of those surfaces could accidentally be pointing at memory that isn't a surface. If you were to blit to that pointer, one could accidentally write over one of your global pointers with whatever bit pattern is null on your system. I would hope that the problem is much simpler.

##### Share on other sites
I ran in a debugger and it says the following: "An Access Violation (Segmentation Fault) raised in your program."

If it makes a difference, I'm using Dev-C++.

I put a check in apply_surface and it's saying that SDL_Surface* source is null.

//Declared as globals and set to NULL as globals.//Updated in "init()"screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );//Updated in "main(int argc, char* args[])"isometricSQ = load_image("Tile.png");buttonSheet = load_image("buttons.png");

SDL_Surface *load_image(std::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);    }    if(optimizedImage != NULL)    {        Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 0xff, 0, 0xff);        SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );    }    return optimizedImage;}

##### Share on other sites
You really should get a better debugger - the one that comes with Dev-C++ is pretty bad. The best is Visual Studio's. There is a free version of it somewhere. Other options I can vouch for are the one that comes with Code::Blocks, and DDD.

Quote:
 I ran in a debugger and it says the following: "An Access Violation (Segmentation Fault) raised in your program."

If this is your only interaction with the debugger, you need to learn to use it better. You should be able to step through your code and see exactly what value your surfaces have at each point of execution. This will tell you what's going wrong.

The code you posted is, again, fine looking on the surface.

##### Share on other sites
Ok, lets say IMG_Load returns NULL. What happens? Well, the first if block is skipped because the condition doesn't match. However, because the code in that function doesn't execute, optimisedImage remains null, so the next block is skipped. The function returns null. I cannot see any checks to guard against this from the little of the calling code you have posted.

Most of the time, we expect the images to load fine. So not finding the image is a pretty exceptional situation. We can use a C++ language feature, an exception, to handle this.

Simply put, if we find that the surface fails to load, we throw an exception. Should we choose to, we can catch the exception anywhere is the calling code.

#include <stdexcept>class ImageLoadingError : public std::runtime_error{public:     ImageLoadingError(const std::string &error)     :         std::runtime_error(error)     {     }}SDL_Surface *load_image(const std::string &filename){    SDL_Surface* surface = IMG_Load(filename.c_str());    if(!surface)    {        throw ImageLoadingError(IMG_GetError());    }        // if we fail to optimise a surface, it really isn't a critical error    // after all, we can still use the original surface    if( SDL_Surface* optimized = SDL_DisplayFormat(surface) )    {        SDL_FreeSurface(surface);        surface = optimized;    }    Uint32 colorkey = SDL_MapRGB(surface->format, 0xff, 0, 0xff);    SDL_SetColorKey(surface, SDL_SRCCOLORKEY, colorkey );        return surface;}

The above guarantees that we will either get a good surface, or an exception will be thrown.

The next step is to deal with the error in a sensible way. This is trickier. The simplest approach is to print an error message, or bring up an error message box explaining the error.

There are more complex solutions, such as loading a default image (the image could be procedurally generated), or notifying the user and asking them if they wish to use a different image and or switch the image directory.

In this example, we can simply wrap main in a try...catch block and print errors:
int main(int argc, char **argv){    try    {        init();        load();        run();    }    catch(const std::exception &e)    {        std::cerr << e.what() << '\n';        return 1;    }    return 0;}

To illustrate the filtering that we can do using exceptions:
void load(){     try     {         someSurface = load_image("some_surface.png");     }     catch( const ImageLoadingError &e )     {          bool b = notifyUser("Image loading failed! " + e.what() + "\nDo you wish to continue with default image?");          if(b)          {               someSurface = generateImage();          }          else          {               throw; // let the caller deal with the problem          }     }}

For the moment the most productive thing you could do would be to learn how to use a debugger. The debugger that Dev-C++ provides is almost useless, and is the main reason why it isn't recommended. A great debugger for C++ is included with the free versions of Microsoft's Visual C++ Studio. I recommend you switch.

##### Share on other sites
I don't know how to use debuggers, there's a bit that controls it, but I'm not sure what they do :S

I'll look into it.

Sorry to be a bother but I'm trying to debug in Visual C++ and it's saying: "LINK : fatal error LNK1561: entry point must be defined"

I have set up the libaries in project options.

Here's what it says;

Message box:
Quote:
 Unhandled exception at 0x689bfcac in Isometric.exe: 0xC0000005: Access violation reading location 0x00000000.

In the little bit at bottom:
Quote:

[Edited by - MrPickle on February 22, 2008 2:56:12 PM]

##### Share on other sites
Can someone please help, I don't know why it's doing this and I can't work on my project until this is fixed.

##### Share on other sites
The 0x00000000 indicates a null pointer. There's not much more help that can be given without more details from you about your code.

##### Share on other sites
I gave up on it and re-done it, hadn't done much anyway :3

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628284
• Total Posts
2981829

• 10
• 10
• 10
• 11
• 17