Skip to content

Commit

Permalink
Bugfix HeapUsed(): sometimes never returns on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
KlausMu committed Dec 23, 2024
1 parent 07eeef8 commit 79fb23a
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions Platformio/hardware/windows_linux/heapUsage_hal_windows_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,29 @@
#if defined(WIN32)
// https://www.daniweb.com/programming/software-development/threads/135188/calculate-the-amount-of-heap-memory
// returns used heap size in bytes or negative if heap is corrupted.
// It seems the while loop sometimes never ends. Reason unknown.
// If this happens, stop after 200 iterations and return the fake numer 800000, as we are doing in Linux.
long HeapUsed()
{
_HEAPINFO info = { 0, 0, 0 };
long used = 0;
int rc;

while ((rc=_heapwalk(&info)) == _HEAPOK)

int loopCounter = 0;
while (((rc=_heapwalk(&info)) == _HEAPOK) && (loopCounter < 100))
{
if (info._useflag == _USEDENTRY)
used += info._size;
loopCounter++;
}
if (rc != _HEAPEND && rc != _HEAPEMPTY)
used = (used?-used:-1);

return used;
if (loopCounter <= 100) {
return used;
} else {
return 800000;
}
}

#elif defined(__linux__) || defined(__APPLE__)
Expand Down

0 comments on commit 79fb23a

Please sign in to comment.