From fab05bac798015cf7a651fb8cea048b154aa8230 Mon Sep 17 00:00:00 2001 From: Neil McKechnie Date: Tue, 9 Mar 2021 10:13:04 +0000 Subject: [PATCH] Update freeMemory.cpp Inhibit interrupts while updating/reading minimum_free_memory as it is accessed from interrupt handler. --- freeMemory.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/freeMemory.cpp b/freeMemory.cpp index 6735ac1..500cff8 100644 --- a/freeMemory.cpp +++ b/freeMemory.cpp @@ -18,6 +18,7 @@ * along with CommandStation. If not, see . */ +#include #include "freeMemory.h" // thanks go to https://github.com/mpflaga/Arduino-MemoryFree @@ -50,6 +51,10 @@ int freeMemory() { // called subroutines. int updateMinimumFreeMemory(unsigned char extraBytes) { int spare = freeMemory()-extraBytes; + byte sreg_save = SREG; + noInterrupts(); if (spare < minimum_free_memory) minimum_free_memory = spare; - return minimum_free_memory; + int returnValue = minimum_free_memory; + SREG = sreg_save; + return returnValue; }