Skip to content

Filter::median() leaks memory #2

@edgar-bonet

Description

@edgar-bonet

median() calls _orderedValues() in order to get a pointer to a newly allocated sorted copy of the sample buffer. When it returns, it looses that pointer and never frees the memory.

This creates a huge memory leak. I did an experiment with an Arduino Uno and a filter of size 5. Repeatedly calling median() makes the heap grow by 64 bytes per call, eventually crashing the Arduino.

The following seems to seal the leak:

diff --git a/Filter.cpp b/Filter.cpp
index 73b85a6..c18e049 100755
--- a/Filter.cpp
+++ b/Filter.cpp
@@ -167,6 +167,8 @@ long Filter::median() const {
     median = ((medianValues->peek(midpoint) + medianValues->peek(midpoint+1)) * 10) / 2;
     median = _longRound(median, 10); 
   }
+  medianValues->flush();
+  free(medianValues);
   return(median); 
 }
 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions