Yalantis/uCrop · error · CImgIOException

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

Error message

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

What it means

Thrown by CImg<T>::load_png() when png_create_info_struct(png_ptr) fails after the main read struct was created: libpng could not allocate the metadata (info) structure; the already-created png_ptr is destroyed before throwing, so no leak remains.

Source

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

                              nfilename?nfilename:"(FILE*)");
      }

      // Setup PNG structures for read.
      png_voidp user_error_ptr = 0;
      png_error_ptr user_error_fn = 0, user_warning_fn = 0;
      png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,user_error_ptr,user_error_fn,user_warning_fn);
      if (!png_ptr) {
        if (!file) cimg::fclose(nfile);
        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);

View on GitHub (pinned to f788b534b4)

Solutions

  1. Free memory and retry the load.
  2. Reduce concurrent image allocations in the process.
  3. Ensure a healthy libpng build (consistent versions) and current libpng release.
  4. If it persists, profile the heap for leaks causing the memory pressure.

Example fix

// before
img.load_png("big.png"); // throws when info struct allocation fails
// after
img.clear(); // release prior image data before decoding
img.load_png("big.png");
Defensive patterns

Strategy: retry

Try / catch

try { img.load_png(path); }
catch (CImgIOException &e) {
  std::fprintf(stderr, "libpng info init failed: %s\n", e.what());
  // free memory, then retry once
}

Prevention

When it happens

Trigger: Allocation failure inside libpng (out of memory / fragmented heap) when creating the info struct for a load_png() call.

Common situations: Low-memory devices or processes near their memory limit; the same allocation-failure family as the png_ptr error, just one step later.

Related errors


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