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 7812211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:26:07+00:00 2026-06-02T04:26:07+00:00

I’m profiling code of a game I wrote and I’m wondering how it is

  • 0

I’m profiling code of a game I wrote and I’m wondering how it is possible that the following snippet causes an heap increase of 4kb (I’m profiling with Heapshot Analysis of Xcode) every time it is executed:

u8 WorldManager::versionOfMap(FILE *file)
{
  char magic[4];
  u8 version;

  fread(magic, 4, 1, file); <-- this is the line
  fread(&version,1,1,file);
  fseek(file, 0, SEEK_SET);

  return version;
}

According to the profiler the highlighted line allocates 4.00Kb of memory with a malloc every time the function is called, memory which is never released. This thing seems to happen with other calls to fread around the code, but this was the most eclatant one.

Is there anything trivial I’m missing? Is it something internal I shouldn’t care about?

Just as a note: I’m profiling it on an iPhone and it’s compiled as release (-O2).

  • 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-02T04:26:08+00:00Added an answer on June 2, 2026 at 4:26 am

    If what you’re describing is really happening and your code has no bugs elsewhere, it is a bug in the implementation, I think.

    More likely I think, is the possibility that you don’t close the file. Stdio streams use buffering by default if the device is non-interactive, and the buffer is allocated either at the time the file is opened or when I/O is performed. While only one buffer should be allocated, you can certainly leak the buffer by forgetting to close the file. But certainly, closing the file should free the buffer. Don’t forget to check the value returned by fclose.

    Supposing for the sake of argument that you are correctly closing the file there are a couple of other nits in your code which won’t be causing this problem but I’ll mention anyway.

    First your fread call reads an object having one member of size 4. You actually have an object having 4 members of size 1. In other words the numeric arguments to fread are swapped. This makes a difference only in the meaning of the return value (important in the case of a partial read).

    Second, while your first call to fread correctly hard-codes the size of char as 1 (in C, that is the definition of ‘size’), it’s probably better stylistically to use sizeof(u8) in the second call to fread.

    If the idea that this really is a memory leak is a correct interpretation (and there aren’t any bugs elsewhere) then you may be able to work around the problem by turning off the stdio buffering for this particular file:

    bool WorldManager::versionOfMap(FILE *file, bool *is_first_file_io, u8 *version)
    {
      char magic[4];
      bool ok = false;
      if (*is_first_file_io) 
      {
        // we ignore failure of this call
        setvbuf(file, NULL, _IONBF, 0);
        *is_first_file_io = false;
      }
    
      if (sizeof(magic) == fread(magic, 1, sizeof(magic), file) 
          && 1 == fread(version, sizeof(*version), 1, file))
      {
          ok = true;
      }
      if (-1 == fseek(file, 0L, SEEK_SET))
      {
          return false;
      }
      else
      {
          return ok && 0 == memcmp(magic, EXPECTED_MAGIC, sizeof(magic));
      }
    }
    

    Even if we’re going with the hypothesis that this really is a bug, and the leak is real, it is well worth condensing your code to the smallest possible example that still demonstrates the problem. If doing that reveals the true bug, you win. Otherwise, you will need the minimal example to report the bug in the implementation.

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

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a

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.