Skip to content

Latest commit

 

History

History
38 lines (33 loc) · 1.49 KB

File metadata and controls

38 lines (33 loc) · 1.49 KB

MJPEG-Decoder

MJPEG Class for TFT LCDs Arduino/STM32/ESP32

  • Compatible with Adafruit_GFX, Arduino_GFX and TFT_eSPI
  • You can read .mjpeg files from SD card and display them on LCD
  • Using MJPEG is more efficient than displaying frames in JPEG seperatly on TFT LCD
  • Delay in in this decoding is reduced form 160/170ms in JPEG Decoder libraries to less than 80ms
  • Original JPEG Decoder : https://github.com/Bodmer/TJpg_Decoder
  • Original GIF Decoder : https://github.com/bitbank2/AnimatedGIF
  • include this .h file in the main code to use the class

Fix: Prevent memory leak in MjpegClass::setup() on ESP32

Problem

Currently, MjpegClass::setup() allocates _read_buf with malloc(READ_BUFFER_SIZE) every time it is called, but never frees the previously allocated memory. On ESP32 this causes a gradual heap leak (~1 KB per restart). After ~200 video restarts the device runs out of memory and crashes.

Changes

  • Added cleanup in setup() to free _read_buf before allocating a new buffer.
  • Added a destructor ~MjpegClass() to ensure _read_buf is released when the object is destroyed.
  • Changed return value of setup() to (_read_buf != nullptr) so allocation failures are reported properly.

Code Excerpt

// in setup():
if (_read_buf) {
    free(_read_buf);
    _read_buf = nullptr;
}
_read_buf = (uint8_t *)malloc(READ_BUFFER_SIZE);
return (_read_buf != nullptr);

// new destructor
~MjpegClass() {
    if (_read_buf) {
        free(_read_buf);
        _read_buf = nullptr;
    }
}