UpdateWindowSurface() causes error "Window surface is invalid, please call SDL_GetWindowSurface() to get a new surface"

I’ve been trying to develop a quick game with SDL2, and since I’ve done this exact thing with SDL2 (2.26.2). I figured this would be a walk in the park except one error has been driving me completely mad and insane.

Since I had a simple code base using SDL2 and I didn’t want re-write everything I just mostly write the exact same thing. I used to use Windows but then transitioned to Linux (PS I’ve tried this with MinGW on Windows and I get the exact same error).

So basically I noticed that my SDL Image wasn’t loading. To be clear I’ve made sure about everything, on Linux I’ve recompiled it from scratch with no errors and also used the apt-get (no difference), on Windows I’ve made sure to put in the libpng16 and zlib dlls, etc.
But it would error out with the error in the title.

Eventually I traced every thing down to the start of my file.
Here is a quick example.

int main(int argc, char** argv){
    sW = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
    SDL_UpdateWindowSurface(sW);
    // this returns the error in the title.
    printf("%s\n", SDL_GetError());

    // ... Other code for SDL renderer and stuff
}

I had an issue on github and I was told not to call SDL_UpdateWindowSurface which caused my program to segfault when I used SDL_GetWindowSurface. And whether or not I should call it, it didn’t fix the problem, and as I said: that was in my other game that ran just fine.

I will be willing to give more details about compiler commands and files if needed, and any help would be appreciated.

The only thing that has really changed between then and now is I’m using MinGW-w64 instead of the base MinGW (and also SDL version). I can’t really test my old game code right now as it needs all the new libraries and stuff to recompile it using MinGW-w64, though if I have to then I will try to.
This error has been driving me insane and no other fixes for similar issues have worked here.

Also sorry if my formatting is terrible, I just want this fixed.

In your code snippet you have “... Other code for SDL renderer and stuff” which suggests that your program is later calling SDL_CreateRenderer(). In that case you should not be referencing a window surface anywhere, neither SDL_UpdateWindowSurface() nor SDL_GetWindowSurface() should be called if you’re using the renderer API.

Thanks for that, I have removed that SDL_UpdateWindowSurface() but that did not fix the original problem.
The image is still blank, I’ve added error checking and it seems to be “fine”.

Also if so, how on Earth did my original project not have this problem?
I know I probably have something messed up somewhere but I’m trying to figure out where it is. Which btw using a BMP and SDL_LoadBMP also did not fix this problem.

Shouldn’t you call SDL_Init before you start using SDL, or is that not necessary anymore?

If SDL_LoadBMP fails make sure the path is correct. Test with an absolute path and see if that works.
Note that relative paths looks in the “working directory” of the process which is not necessarily the same directory as where your executable file is located (it depends on how your program was started).

It seems that SDL_Init is not necessary anymore as it made no change (and as I keep saying my older code worked just fine without it).

For some reason I tracked down my problem to either my SDL_RenderCopy or SDL_RenderPresent. I can actually see my image for 1 singular frame before disappearing. But no matter what I try it will not come back. If I manually change it so SDL_RenderPresent isn’t called every frame then the image stays, except obviously the game won’t update like that.

I guess my original problem in the title is solved, but this is some different issue I’m not sure about.
Again I’m still working off of my original code and the update looked something like this

void update(){
    SDL_RenderClear(renderer);
    SDL_RenderCopy(renderer, Sprite->texture, 0, &Sprite->rect);
    SDL_RenderPresent(renderer);
    SDL_Delay(1000 / 60);
}

From what I’ve seen this should be entirely correct? But as I said, just disappears after 1 frame.

I don’t see anything wrong with that function but there is a lot of code missing that you haven’t shown us.

Perhaps you could post a real example that can be compiled and executed to reproduce the problem?

Correct code should be guaranteed to work, but incorrect code isn’t guaranteed to fail! There’s nothing surprising here.

Well I found out the problem.

Turns out in my actual code I was passing a local variable into my rendering code.
I’m super sorry for that.

Oh well at least I learned more about SDL2 so thanks for everyone’s help!
I was honestly just more confused that my old code worked fine but this time it throws errors.

1 Like