0

I am currently programming the ESP32 board in C++ and I am having trouble with my dataContainer class and releasing/allocating memory.

I do use the following DataContainer class (simplyfied):

    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();
    ...some methods...
    void reset_all_data();
};

And here is the reset_all_data() definition:

/* 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]; <-- Error is triggered here!!!

            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 (maxsize = 50)
    {
        datalist[i] = new Elementtype[5];
    }

    currentsize = 0;
}

As you can see, I have added some integrity checks, but the one before delete datalist (this seems to trigger the error). When I call reset_all_data() from my main.cpp at a certain point in my program the following error is triggered:

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 

One more thing, the error is only triggered when a certain function is called right before it, even when the whole code inside this function is commented. This is the function's head: void write_data_container_to_file(fs::FS &fs, const char *path, DataContainer<uint16_t> data, const char *RTC_timestamp)thus, the mere call of the function plays an import role here.

Right now I am completely lost - any suggestion/idea is welcome on how to proceed.

EDIT: The dataContainer holds a 2D array of uint16_t.

Slev1n
  • 25
  • 7
  • If every element of `datalist[]` is an array itself, you need to write `delete[] datalist[i]`. – G. Sliepen Sep 23 '22 at 21:59
  • @G.Sliepen: The dataContainer holds a 2D array of uint16_t. `datalist[]` is not an array itself but is pointing to an array. I still think you are right. I have changed it to `delete[] datalist[i]` and also after the for loop I changed it to `delete[] datalist`. But the code still crashes. What I still dont get is why it only crashes when I call `write_data_container_to_file()` before even when this function is empty. When I place the `reset_all_data()` AFTER the function, it runs a little bit longer crashing with `heap != NULL && "free() target pointer is outside heap areas"` – Slev1n Sep 24 '22 at 08:26
  • If the crash happen when you calling a function as you said, it sounds that you are running out of memory, or it could be because that when a function is called, it need to push all the necessary things to the stack and the stack is crashing with the heap, so the question is how big is your array? Especially when your delete function is not working as expecting, so your heap fragmented or build-up… – hcheung Sep 26 '22 at 01:02

1 Answers1

0

I finally tracked down the, rather obvious, reason for the HEAP CORRUPTION. In the end I only called delete datalist but it would have been correct to call delete[] datalist after the for loop. The reason is, that within the for loop I delete the pointers pointing to arrays, which represent the "rows" of my allcoated 2D memory. In the end, I also have to delete the pointer, which points to the array holding the pointers I deleted within the for loop.

So I was not paying attention and one should watch out that when it comes to releasing the previously allocated memory, care should be taken if delete or delete[]should be called.

Slev1n
  • 25
  • 7