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

  • Home
  • SEARCH
  • 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 3215570
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:11:35+00:00 2026-05-17T15:11:35+00:00

I have a piece of C++ code that deallocates memory as follows for (int

  • 0

I have a piece of C++ code that deallocates memory as follows

for (int i = Pages-1; i >= NewPages; i--)
{
    LPVOID p = m_Pages[i];
    free(p);
}

While the code works ok, when called from an exception handler, it runs very slowly. Looking at task manager while single stepping through the above loop, the amount of physical memory used by the process (Mem Usage) increases with each call to free, whereas the virtual memory (VM Size) stays the same. Eventually the process terminates abnormally.

The code that allocates the memory that throws the exception is as follows;

for (int i = Pages; i < NewPages; i++)
{
    LPVOID p = malloc(m_ElementsPerPage*m_ElementSize);
    if (!p)
        AfxThrowMemoryException( );
    m_Pages.Add(p);
}

Now at a glance I suspect that if I reverse the ordering of the deallocation loop, such that the last block allocated is the first freed, I might avoid this problem. But is there any reason why process memory usage should increase with a call to free, and why calling free on a valid heap memory block should cause the program to terminate? (n.b. the termination could well be debugger related rather than the app itself).

Edit: The OS is Windows XP SP3, the compiler is MSVC++ 8

Edit2: A more complete version of the code is as follows;

void  MyArray::ResizeArray(int NewPages)
{
    int Pages = m_Pages.GetSize();
    if (NewPages != Pages)
    {
        if (NewPages > Pages)   // Grow the page array
        {
            for (int i = Pages; i < NewPages; i++)
            {
                LPVOID p = malloc(m_ElementsPerPage*m_ElementSize);
                if (!p)
                    AfxThrowMemoryException( );
                m_Pages.Add(p);
            }
        } else  // Shrink the page array
        {
            for (int i = Pages-1; i >= NewPages; i--)
            {
                LPVOID p = m_Pages[i];
                    free(p);
            }
            m_Pages.SetSize(NewPages);
        }
    }
}
  • 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-05-17T15:11:36+00:00Added an answer on May 17, 2026 at 3:11 pm

    Your process can use two megabytes of memory. When malloc fails, the used memory will be near that value. If your computer has less physical memory, a lot of memory pages of that process will have been paged out to the disk.

    While your exception handler frees back memory, the heap manager will touch all pages, bringing them back to physical memory. This will be slow.

    This is what you are observing with the Task Manager. The column names are deceptives: VM Size is really private memory, which will not change because freed memory is not given back to the system by the Heap Manager (answering why would be a question by itself). Mem Usage is the size of the working set of the process, that is the physical memory used by that process. It will increase as pages are mapped back to physical memory from the page file.

    You do not mention how the process terminates abnormally, but fragmentation may also occur in memory, which may explain that mallocs may continue to fail even after freeing your array.

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

Sidebar

Related Questions

i have piece of code that works fine on my local test server but
I have this piece of code that works very well and gives me the
I have a piece of code that works in a VS.C# project, but when
I have a piece of C# code that add the values of an enum
I have a piece of C# code that needs to convert a string array
I have a piece of code here that i really could use some help
Let's say I have to implement a piece of T-SQL code that must return
I have a piece of templated code that is never run, but is compiled.
Hy all, I believe that the following piece of code is generating memory leak?
I have a piece of code that involves multiple inserts but need to execute

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.