Alan Williams-Key
2009-10-15 16:22:55 UTC
(Sorry if this is not the right newsgroup - couldn't find a more appropriate
one).
I'm trying to find a problem in my program and I suspected a problem with
the heap - maybe leaky code. So I was looking at the heap usage with a small
function I found on the internet somehwere. Only trouble is that it entered
an infinite loop. I think this might point to a problem but I don't know what
it is. Here's the code with an additional fix to catch the inifinite loop:
int CountWalk()
{
int HeapStatus;
int lastPentry = 0;
BOOL running = TRUE;
_HEAPINFO info;
info._pentry = NULL;
int UsedBytes = 0;
while(running)
{ /* scan heap */
HeapStatus = _heapwalk(&info);
switch(HeapStatus)
{ /* check status */
case _HEAPOK:
break;
case _HEAPEND:
running = FALSE;
break;
default:
ASSERT(FALSE);
running = FALSE;
continue;
} /* check status */
if(info._useflag == _USEDENTRY)
{ /* used block */
UsedBytes += info._size;
} /* used block */
// extra termination required to avoid infinite loop !!!
if ((int)info._pentry == lastPentry)
running = FALSE;
else
lastPentry = (int)info._pentry;
} /* scan heap */
return UsedBytes;
} // CMemStatsDlg::CountWalk
What was happening was that _heapwalk() was returning the same unused heap
block. I know this because the _pentry data didn't change. The status return
was _HEAPOK.
Anyone got any idea what sort of error could put the heap into a state where
_heapwalk() could malfunction this way?
Alan
(VS6 C++)
one).
I'm trying to find a problem in my program and I suspected a problem with
the heap - maybe leaky code. So I was looking at the heap usage with a small
function I found on the internet somehwere. Only trouble is that it entered
an infinite loop. I think this might point to a problem but I don't know what
it is. Here's the code with an additional fix to catch the inifinite loop:
int CountWalk()
{
int HeapStatus;
int lastPentry = 0;
BOOL running = TRUE;
_HEAPINFO info;
info._pentry = NULL;
int UsedBytes = 0;
while(running)
{ /* scan heap */
HeapStatus = _heapwalk(&info);
switch(HeapStatus)
{ /* check status */
case _HEAPOK:
break;
case _HEAPEND:
running = FALSE;
break;
default:
ASSERT(FALSE);
running = FALSE;
continue;
} /* check status */
if(info._useflag == _USEDENTRY)
{ /* used block */
UsedBytes += info._size;
} /* used block */
// extra termination required to avoid infinite loop !!!
if ((int)info._pentry == lastPentry)
running = FALSE;
else
lastPentry = (int)info._pentry;
} /* scan heap */
return UsedBytes;
} // CMemStatsDlg::CountWalk
What was happening was that _heapwalk() was returning the same unused heap
block. I know this because the _pentry data didn't change. The status return
was _HEAPOK.
Anyone got any idea what sort of error could put the heap into a state where
_heapwalk() could malfunction this way?
Alan
(VS6 C++)