From 0163bbef6c50b18f0031c5461108dba1f1c45bf8 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Wed, 19 Jun 2019 22:23:49 +0200 Subject: [PATCH] Some memory leaks fixed. --- include/effect_clock.h | 1 + include/effect_snake.h | 1 + src/effect_clock.cpp | 4 ++++ src/effect_gol.cpp | 13 +++++++------ src/effect_pixelclock.cpp | 1 + src/effect_snake.cpp | 4 ++++ 6 files changed, 18 insertions(+), 6 deletions(-) diff --git a/include/effect_clock.h b/include/effect_clock.h index 81b2b86..1df3d5a 100644 --- a/include/effect_clock.h +++ b/include/effect_clock.h @@ -10,6 +10,7 @@ private: Window* window = new Window(0, LED_HEIGHT - 6, LED_WIDTH, 6); public: + ~ClockEffect(); void loop(); void loop(boolean invert, CRGB fg_color, CRGB bg_color); }; diff --git a/include/effect_snake.h b/include/effect_snake.h index d1efe24..a6f8465 100644 --- a/include/effect_snake.h +++ b/include/effect_snake.h @@ -17,6 +17,7 @@ private: bool is_direction_okay(uint8_t direction); public: SnakeEffect(); + ~SnakeEffect(); void loop(); boolean valid_position(Coords c); Coords update_position(Coords c, uint8_t direction); diff --git a/src/effect_clock.cpp b/src/effect_clock.cpp index 505e455..d19b955 100644 --- a/src/effect_clock.cpp +++ b/src/effect_clock.cpp @@ -40,3 +40,7 @@ void ClockEffect::loop(boolean invert, CRGB fg_color, CRGB bg_color) { window->setPixel(7, 4, &fg_color); } } + +ClockEffect::~ClockEffect() { + delete window; +} diff --git a/src/effect_gol.cpp b/src/effect_gol.cpp index fed162e..51e3102 100644 --- a/src/effect_gol.cpp +++ b/src/effect_gol.cpp @@ -3,7 +3,7 @@ GolEffect::GolEffect() { this->window = new Window(0, 0, LED_WIDTH, LED_HEIGHT-6); - + _data = new uint8_t[this->window->count]; _old = new uint8_t[this->window->count]; for(uint16_t i=0; iwindow->count; i++) { @@ -26,6 +26,7 @@ void GolEffect::_initialize() { GolEffect::~GolEffect() { delete[] _data; delete[] _old; + delete window; } void GolEffect::loop() { @@ -35,7 +36,7 @@ void GolEffect::loop() { } else { _blend += EFFECT_GOL_BLEND_SPEED; } - + _draw(); } @@ -52,14 +53,14 @@ void GolEffect::_advance() { uint16_t changes = 0; for(uint8_t x=0; xwindow->width; x++) for(uint8_t y=0; ywindow->height; y++) { uint16_t index = y*w + x; - uint8_t count = + uint8_t count = (x>0 && y>0 && _old[index - w - 1]) + (y>0 && _old[index - w]) + (xwindow->width-1 && y>0 && _old[index - w + 1]) + - + (x>0 && _old[index - 1]) + (xwindow->width-1 && _old[index + 1]) + - + (x>0 && ywindow->height-1 && _old[index + w - 1]) + (ywindow->height-1 && _old[index + w]) + (xwindow->width-1 && ywindow->height-1 && _old[index + w + 1]); @@ -79,7 +80,7 @@ void GolEffect::_advance() { } } } - + if (changes == 0) { _initialize(); } diff --git a/src/effect_pixelclock.cpp b/src/effect_pixelclock.cpp index 8ad3793..d7369ec 100644 --- a/src/effect_pixelclock.cpp +++ b/src/effect_pixelclock.cpp @@ -10,6 +10,7 @@ PixelClockEffect::PixelClockEffect() { PixelClockEffect::~PixelClockEffect() { delete _color_seconds; delete _color_minutes; + delete window; } void PixelClockEffect::loop() { diff --git a/src/effect_snake.cpp b/src/effect_snake.cpp index 5c37def..a44cd3e 100644 --- a/src/effect_snake.cpp +++ b/src/effect_snake.cpp @@ -6,6 +6,10 @@ SnakeEffect::SnakeEffect() { this->window = new Window(0, 0, LED_WIDTH, LED_HEIGHT-6); } +SnakeEffect::~SnakeEffect() { + delete window; +} + void SnakeEffect::loop() { if (run++ % EFFECT_SNAKE_SLOWDOWN == 0) { // Change the coordinates only on every n-th run. if (random8(EFFECT_SNAKE_DIRECTION_CHANGE)==0 || is_turn_needed()) turn_random();