Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8124683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:32:25+00:00 2026-06-06T06:32:25+00:00

Valgrind is complaining about some of my code, but this code is pretty much

  • 0

Valgrind is complaining about some of my code, but this code is pretty much the sample libpng code from the documentation:

An example of the Valgrind output.

==15847== 14,384 bytes in 31 blocks are definitely lost in loss record 239 of 240
==15847==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
==15847==    by 0x5837381: ??? (in /lib/x86_64-linux-gnu/libpng12.so.0.46.0)
==15847==    by 0x581FD63: png_create_info_struct (in /lib/x86_64-linux-gnu/libpng12.so.0.46.0)
==15847==    by 0x67837C: Star::Image::loadPng(char const*) (StarImage.cpp:35)
==15847==    by 0x4E7721: Star::Assets::threadedFetchImage(Star::String const&, bool) (StarAssets.cpp:400)
==15847==    by 0x4EB4F4: Star::Assets::threadPoolMain() (StarAssets.cpp:256)
==15847==    by 0x6B18B9: Star::ThreadImpl::runThread(void*) (StarThread_unix.cpp:19)
==15847==    by 0x5CE2EFB: start_thread (pthread_create.c:304)
==15847==    by 0x6A1359C: clone (clone.S:112)

and the code method around line 35. Line 35 is marked by a comment and starts with png_infop end_info

ImagePtr Image::loadPng(char const* filename) {
  FILE *fp = fopen(filename, "rb");
  if (!fp)
    throw ImageException(strf("Could not open file %s in Image::loadPng", filename));

  png_byte header[8];
  int res = fread(header, 1, sizeof(header), fp);
  if (!res)
    throw ImageException(strf("Cannot read file %s", filename));

  if (png_sig_cmp(header, 0, sizeof(header)))
    throw ImageException(strf("File %s is not a png image!", filename));

  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
  if (!png_ptr) {
    fclose(fp);
    throw ImageException("Internal libPNG error");
  }

  png_infop info_ptr = png_create_info_struct(png_ptr);
  if (!info_ptr) {
    png_destroy_read_struct(&png_ptr, nullptr, nullptr);
    fclose(fp);
    throw ImageException("Internal libPNG error");
  }

  png_infop end_info = png_create_info_struct(png_ptr);  //this is line 35.
  if (!end_info) {
    png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
    fclose(fp);
    throw ImageException("Internal libPNG error");
  }

  if (setjmp(png_jmpbuf(png_ptr))) {
    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
    fclose(fp);
    throw ImageException("Internal error reading png.");
  }

//snip some

  png_read_image(png_ptr, row_ptrs.get());
  png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)0);
  fclose(fp);

  return image;
}

Which is nearly identical to the example located at http://www.libpng.org/pub/png/libpng-1.2.5-manual.html#section-3

Is this an internal leak? Or am I just missing something really obvious? Or is Valgrind choking?

For reference, I’m currently using libpng1.2.46.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:32:28+00:00Added an answer on June 6, 2026 at 6:32 am

    The last line

     png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)0);
    

    should not be rather

     png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
    

    ?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

valgrind is reporting uninitialized memory errors from code like this: unsigned char buf[100]; struct
On OSX Valgrind reports this memory leak, Where is it coming from? The code
Here is the valgrind output from a project: ==2433== Invalid free() / delete /
I have the following stack trace from valgrind. But it does not give me
When I run valgrind on the following (example) code it reports an Invalid free()
Can anyone explain me why on that c++ simple code valgrind returns this .
When I use this valgrind --leak-check=yes ./Main I have about 185236 errors. It said
When I use this valgrind --leak-check=yes ./Main I have about 185236 errors. It said
Valgrind detect an invalid write of size 1 in this piece of code. In
If you've used Memcheck (from Valgrind) you'll probably be familiar with this message... Conditional

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.