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);
}
}
}