Closed
Description
Board
any board using updater.cpp
Device Description
tested on many ESP32 dev boards
Hardware Configuration
mainly WLED strings, not much else.
Version
latest master (checkout manually)
IDE Name
PlatformIO
Operating System
Windows 10/11
Flash frequency
40Mhz/80Mhz
PSRAM enabled
no
Upload speed
921600, 115200
Description
On an OTA firmware attempt, if the transfer fails, and updateClass is defined statically, the buffers used for the update are not freed.
There is no destructor.
Sketch
any sketch using update.cpp
Debug Message
ESP.getFreeHeap() shows heap memory from buffers being allocated, and not used.
Other Steps to Reproduce
No response
I have checked existing issues, online documentation and the Troubleshooting Guide
- I confirm I have checked existing issues, online documentation and Troubleshooting guide.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Done
Activity
DamronDan commentedon Mar 23, 2023
Solution to this problem is easy: add this code:
MathewHDYT commentedon Sep 13, 2023
@DamronDan Is correct this issue can be resolved immediately and does not require Investigation.
@mrengineer7777 Using
malloc
in combination withdelete
is undefined behaviour and can therefore cause a multitude of issues. This should be immediately fixed if possible.See this blog post for more information on the issue.
The affected line where the memory is instantiated can be found here and the line where the memory is deleted and causes undefined behaviour can be found here.
Would be nice if this could be fixed as soon as possible using the code portion above should resolve that issue.
Additionally in the aforementioned code portion, the
_skipBuffer
is also never freed, so we have both a memory leak and undefined behaviour.It would probably be best if the
_skipBuffer
could be changed to an array on the stack instead so something like this:This would resolve the memory leak and the need to allocate 16 bytes on the heap, which doesn't really make sense for most use cases.
I am also assuming that
ESP8266
Updater.cpp
was a major inspiration for this implementation, but there both issues do not exists, because neither a_skipBuffer
is allocated, nor is the memory for the_buffer
allocated withmalloc
, but instead withnew
as it should.The question is why is the Updater even implemented that way, if the
ESP8266
code has been used as inspiration that would make sense because it does not have the sameAPI
as theESP32
Because with the
ESP32
we can include the"esp_ota_ops.h"
header and already do we simply don't use it. This header already contains a lot of helper methods to writeOTA data
, see the offical documentation for more information. There is no need to implement this feature by hand instead we can simply use theesp-idf API
.An example implementation using some of the same methods as the
Update.cpp
can be found below:Update.cpp
Whereas the header could look something like this.
Update.h
As can be seen below the actual implementation could be much easier and shorter and not even need any internal buffer allocated on the heap that holds temporary memory. Especially
4KB
of it, which is not an insignificant amount especially for micro-controllers.Fix memory leak and undefined behavour in Updater espressif#7984
Fix memory leak and undefined behavour in Updater.cpp (UpdaterClass) (#…
2 remaining items