The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL_TEXTUREACCESS_STREAMING crashes when window size changes
AntTheAlchemist


Joined: 13 Feb 2015
Posts: 60
Location: UK
Before I submit this to bugzilla, can anyone see anything I've done wrong here? Basically, it crashes when the window is resized if a streaming texture is created. Notice I haven't even used the texture, but I am in a larger project and it all works until the window size changes. I can sort of fix the bug by destroying and recreating the streaming texture when I get a window size event, but it still crashes sometimes when the window loses focus and regains focus. This is all caused by the streaming texture attached to the renderer when a call to SDL_RenderPresent() is made (it causes a segfault). Only able to test this on Windows 10 and Windows 7 at present.

Code:
#include <SDL.h>

int main(int argc, char *argv[]) {
   if(SDL_Init(SDL_INIT_EVERYTHING))
      SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Init fail",  SDL_GetError(), nullptr);
   SDL_DisplayMode desktopMode;
   if(SDL_GetDesktopDisplayMode(0, &desktopMode))
      SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Init fail",  SDL_GetError(), nullptr);
   SDL_Window *screenWindow = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
         desktopMode.w / 2, desktopMode.h / 2, SDL_WINDOW_RESIZABLE);
   if(!screenWindow)
      SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Init fail",  SDL_GetError(), nullptr);
   SDL_Renderer *renderer = SDL_CreateRenderer(screenWindow, -1, SDL_RENDERER_PRESENTVSYNC);
   if(!renderer)
      SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Init fail",  SDL_GetError(), nullptr);
   SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_BGR888, SDL_TEXTUREACCESS_STREAMING, 64, 64);
   if(!texture)
      SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Init fail",  SDL_GetError(), nullptr);
   SDL_Event event;
   bool running = true;
   bool fullscreen = false;
   while(running) {
      while(SDL_PollEvent(&event)) {
         switch (event.type) {
            case SDL_QUIT:
               running = false;
               break;
            case SDL_KEYDOWN:
               switch(event.key.keysym.sym) {
                  case SDLK_ESCAPE:
                     running = false;
                     break;
                  case SDLK_SPACE:
                     fullscreen = !fullscreen;
                     SDL_SetWindowFullscreen(screenWindow, fullscreen?SDL_WINDOW_FULLSCREEN_DESKTOP:0);
                     break;
               }
               break;
         }
      }
      if(SDL_SetRenderDrawColor(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
         SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error",  SDL_GetError(), nullptr);
      if(SDL_RenderClear(renderer))
         SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error",  SDL_GetError(), nullptr);
      SDL_RenderPresent(renderer);
   }
   SDL_DestroyTexture(texture);
   SDL_DestroyRenderer(renderer);
   SDL_DestroyWindow(screenWindow);
   SDL_Quit();
   return 0;
}
ChliHug


Joined: 05 May 2016
Posts: 7
This could be related to Bug 3147.

Check if it still happens when you change the renderer driver to OpenGL. You can do that by setting the SDL_RENDER_DRIVER environment variable to opengl.

There was already a patch proposed in that bug report. If you want to test it and don't want to build SDL2 yourself, I made some binaries with the patch applied to 5b61e12c0a30. Download them here (These binaries require the Visual C++ 2015 runtime library. Also note that SDL2 behaves slightly different when built with libc on Windows. For example, SDL_Log prints the message twice.)
AntTheAlchemist


Joined: 13 Feb 2015
Posts: 60
Location: UK
ChliHug wrote:
This could be related to Bug 3147.


You're right, it looks like it's the same bug, thanks. I tested using various renderers and it only happens with direct3d.

I'm using Eclipse, so I can't use the binary patch, unfortunately. Is this something that a new SDL2.dll should fix?
ChliHug


Joined: 05 May 2016
Posts: 7
AntTheAlchemist wrote:
Is this something that a new SDL2.dll should fix?

Sure. Once the SDL developers push a fix to the official repository, then the next release should include it. However, it's hard to say when that will happen.