Corrupt heap with ESP32
Posted: Fri Sep 23, 2022 9:40 pm
Hello, I am having a little platformIO project with the ESP32 using VScode as IDE and arduino as framework using C++. I am currently struggling with a CORRUPT HEAP error. Here the error message:
Start -> the device initialises some stuff -> goes to sleep -> wakes up -> measures 5 sensor values and stores them in a Datacontainer -> writes data from container to file (SD card) -> clears the Datacontainer, this last step causes CORRUPT HEAP.
I was trying to track down the problem using it returns true until the datacontainer memory is released by the "delete" operation. But I made one observation, resetting the datacontainer only causes the CORRUPT HEAP error when my function is executed right before. EVEN when the whole code inside the function is commented, the error is triggered. When I comment the function call, no error is happening.
Here is some more information about the datacontainer class I am talking about (I have attached the whole class header file):
I have to confess, that I am quite new to C++ and I have taken over this DataContainer structure from someone else without further instructions - so I do not understand everything about it.
This public method finally causes the error:
I have also tried getting some information about the heap right before calling the datacontainer reset using
Here is the output:
The good thing is I think I have tracked down the origin:CORRUPT HEAP: Bad head at 0x3ffbb0f0. Expected 0xabba1234 got 0x3ffb9a34
assert failed: multi_heap_free multi_heap_poisoning.c:253 (head != NULL)
Backtrace:0x40083881:0x3ffb25400x4008e7e5:0x3ffb2560 0x40093d55:0x3ffb2580 0x4009399b:0x3ffb26b0 0x40083d41:0x3ffb26d0
0x40093d85:0x3ffb26f0 0x4014e3f5:0x3ffb2710 0x400d2dc6:0x3ffb2730 0x400d31e3:0x3ffb2750 0x400d9b02:0x3ffb2820
Start -> the device initialises some stuff -> goes to sleep -> wakes up -> measures 5 sensor values and stores them in a Datacontainer -> writes data from container to file (SD card) -> clears the Datacontainer, this last step causes CORRUPT HEAP.
I was trying to track down the problem using
Code: Select all
heap_caps_check_integrity_all(true)
Code: Select all
write_data_container_to_file(SD, data_file_path.c_str(), data, RTC_timestamp)
Here is some more information about the datacontainer class I am talking about (I have attached the whole class header file):
Code: Select all
template <typename Elementtype>
class DataContainer
{
private:
Elementtype **datalist;
int maxsize;
std::size_t currentsize; // How much data is saved in datalist
public:
DataContainer(int maxcapacity);
~DataContainer();
void init_data_saving();
void add_data(Elementtype &new_Element, const int typenmbr);
void end_data_saving();
Elementtype *operator[](int posnumber) const;
const std::size_t get_currentsize() const;
Elementtype **get_all_data() const;
void reset_all_data();
};
This public method finally causes the error:
Code: Select all
/* Deletes all Data of Datacontainer and allocates new memory*/
template <typename Elementtype>
void DataContainer<Elementtype>::reset_all_data()
{
for (int i = 0; i < currentsize; i++)
{
if (datalist[i])
Serial.println(heap_caps_check_integrity_all(true));
delete datalist[i]; <-- Triggers the Error
Serial.println(heap_caps_check_integrity_all(true));
}
delete datalist;
datalist = new Elementtype *[maxsize];
for (int i = 0; i < maxsize; i++) // Declare a memory block of size maxsize
{
datalist[i] = new Elementtype[SPIRU_SENSOR];
}
currentsize = 0;
}
Code: Select all
heap_caps_print_heap_info(MALLOC_CAP_DEFAULT);
So at this point I am quite lost and have no idea where the error comes from. If there is any suggestion it is more than welcome!Heap summary for capabilities 0x00001000:
At 0x3ffb8000 len 6688 free 168 allocated 3888 min_free 168
largest_free_block 0 alloc_blocks 39 free_blocks 1 total_blocks 40
At 0x3ffb0000 len 25480 free 14928 allocated 8304 min_free 10956
largest_free_block 14836 alloc_blocks 7 free_blocks 1 total_blocks 8
At 0x3ffae6e0 len 6192 free 3980 allocated 36 min_free 3980
largest_free_block 3956 alloc_blocks 1 free_blocks 1 total_blocks 2
At 0x3ffb6388 len 7288 free 180 allocated 4488 min_free 172
largest_free_block 0 alloc_blocks 38 free_blocks 2 total_blocks 40
At 0x3ffb9a20 len 16648 free 4768 allocated 9260 min_free 148
largest_free_block 4596 alloc_blocks 38 free_blocks 1 total_blocks 39
At 0x3ffc81a8 len 97880 free 64236 allocated 31204 min_free 64236
largest_free_block 63476 alloc_blocks 23 free_blocks 2 total_blocks 25
At 0x3ffe0440 len 15072 free 12908 allocated 0 min_free 12908
largest_free_block 12788 alloc_blocks 0 free_blocks 1 total_blocks 1
At 0x3ffe4350 len 113840 free 111676 allocated 0 min_free 111676
largest_free_block 110580 alloc_blocks 0 free_blocks 1 total_blocks 1
Totals:
free 212844 allocated 57180 min_free 204244 largest_free_block 110580