Yalantis/uCrop · error · CImgIOException

load_png(): Failed to initialize 'end_info' structure for fi

Error message

load_png(): Failed to initialize 'end_info' structure for file '%s'.

What it means

Thrown by CImg<T>::load_png() when the second png_create_info_struct() call (for the end-info structure) returns null; both previously created structures (png_ptr and info_ptr) are destroyed via png_destroy_read_struct before the exception is raised.

Source

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

        throw CImgIOException(_cimg_instance
                              "load_png(): Failed to initialize 'png_ptr' structure for file '%s'.",
                              cimg_instance,
                              nfilename?nfilename:"(FILE*)");
      }
      png_infop info_ptr = png_create_info_struct(png_ptr);
      if (!info_ptr) {
        if (!file) cimg::fclose(nfile);
        png_destroy_read_struct(&png_ptr,(png_infopp)0,(png_infopp)0);
        throw CImgIOException(_cimg_instance
                              "load_png(): Failed to initialize 'info_ptr' structure for file '%s'.",
                              cimg_instance,
                              nfilename?nfilename:"(FILE*)");
      }
      png_infop end_info = png_create_info_struct(png_ptr);
      if (!end_info) {
        if (!file) cimg::fclose(nfile);
        png_destroy_read_struct(&png_ptr,&info_ptr,(png_infopp)0);
        throw CImgIOException(_cimg_instance
                              "load_png(): Failed to initialize 'end_info' structure for file '%s'.",
                              cimg_instance,
                              nfilename?nfilename:"(FILE*)");
      }

      // Error handling callback for png file reading.
      if (setjmp(png_jmpbuf(png_ptr))) {
        if (!file) cimg::fclose((std::FILE*)nfile);
        png_destroy_read_struct(&png_ptr, &end_info, (png_infopp)0);
        throw CImgIOException(_cimg_instance
                              "load_png(): Encountered unknown fatal error in libpng for file '%s'.",
                              cimg_instance,
                              nfilename?nfilename:"(FILE*)");
      }
      png_init_io(png_ptr, nfile);
      png_set_sig_bytes(png_ptr, 8);

      // Get PNG Header Info up to data block.

View on GitHub (pinned to f788b534b4)

Solutions

  1. Free memory and retry.
  2. Lower peak memory usage (load images sequentially, resize before caching).
  3. Rebuild/upgrade libpng to a consistent, current version.

Example fix

// before
img.load_png(path); // throws when end_info allocation fails
// after
img.clear();
img.load_png(path);
Defensive patterns

Strategy: try-catch

Try / catch

try { img.load_png(path); }
catch (CImgIOException &e) {
  std::fprintf(stderr, "libpng end_info init failed: %s\n", e.what());
}

Prevention

When it happens

Trigger: Allocation failure while creating the third libpng structure (end_info) during load_png(); same OOM/fragmentation conditions as the other init failures.

Common situations: Low-memory processes, memory-constrained mobile/embedded builds, or near-exhausted heaps after many image operations.

Related errors


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