Yalantis/uCrop · error · CImgDisplayException

CImgDisplay::assign(): %s

Error message

CImgDisplay::assign(): %s

What it means

CImgDisplay::assign() (SDL3 backend) throws CImgDisplayException containing the string returned by SDL_GetError() when SDL_CreateWindowAndRenderer fails to create the window/renderer. The %s is SDL's own diagnostic message, so the real cause is in SDL.

Source

Thrown at ucrop/src/main/jni/CImg.h:12113

      _height = std::min(dimh,(unsigned int)screen_height());
      _normalization = normalization_type<4?normalization_type:3;
      _is_fullscreen = is_fullscreen;
      _window_x = _window_y = cimg::type<int>::min();
      _is_closed = closed_flag;
      _is_cursor_visible = true;
      _paint_request = false;
      _title = tmp_title;
      _thread_id = SDL_GetCurrentThreadID();
      flush();

      // Create window and renderer.
      if (!SDL_CreateWindowAndRenderer(_title,(int)_width,(int)_height,
                                       SDL_WINDOW_RESIZABLE | SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS |
                                       (_is_fullscreen?SDL_WINDOW_FULLSCREEN:0) |
                                       (_is_closed?SDL_WINDOW_HIDDEN:0),
                                       &_window,&_renderer)) {
        SDL3_attr.unlock();
        throw CImgDisplayException("CImgDisplay::assign(): %s",SDL_GetError());
      }
      SDL_RaiseWindow(_window);
      SDL_SetRenderDrawColor(_renderer,0,0,0,255);
      if (!_is_fullscreen)
        SDL_SetWindowPosition(_window,SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED);
      _window_width = _width;
      _window_height = _height;
      _update_window_pos();

      // Create texture.
      _texture = SDL_CreateTexture(_renderer,SDL_PIXELFORMAT_RGBA8888,SDL_TEXTUREACCESS_STREAMING,
                                   (int)_width,(int)_height);
      _data = new unsigned int[_width*_height];

      // Add to managed list of CImgDisplays.
      if (SDL3_attr.nb_cimg_displays>=512) {
        SDL3_attr.unlock();
        throw CImgDisplayException("CImgDisplay::assign(): Max number of displays (512) already opened.");

View on GitHub (pinned to f788b534b4)

Solutions

  1. Initialize SDL video before creating the display: SDL_Init(SDL_INIT_VIDEO) (or ensure CImg's SDL3 attribute init ran).
  2. Read the SDL_GetError() text embedded in the message for the concrete SDL cause (e.g. 'Video subsystem has not been initialized').
  3. Run on a machine with a working display server, or use a virtual framebuffer (xvfb) in CI/containers.
  4. Pass positive non-zero width/height to assign().

Example fix

// before
CImgDisplay disp(0, 0, "win"); // invalid size, throws with SDL_GetError()
// after
if (SDL_Init(SDL_INIT_VIDEO) == 0) {
  CImgDisplay disp(640, 480, "win");
} else {
  std::fprintf(stderr, "SDL init failed: %s\n", SDL_GetError());
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (SDL_WasInit(SDL_INIT_VIDEO) == 0) SDL_Init(SDL_INIT_VIDEO); // ensure video subsystem before assign
if (width <= 0 || height <= 0) width = height = 1;

Try / catch

try { disp.assign(w, h, title); } catch (CImgDisplayException& e) { std::fprintf(stderr, "window creation failed: %s\n", e.what()); }

Prevention

When it happens

Trigger: Calling assign(width,height,title) (or constructing a CImgDisplay) when SDL video subsystem is not initialized, the requested window size is invalid (<=0), SDL was built without video support, or the platform cannot create a window/renderer.

Common situations: Forgetting cimg::SDL3_attr()/SDL_Init(SDL_INIT_VIDEO) before creating displays (headless servers, CI containers without X/Wayland), requesting a 0-sized window, SDL3 migration issues where the video driver is unavailable.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/71f4f6d2b824e369. Report an issue: GitHub.