rykovv / spiffs_circular_queue

ESP-IDF and PlatformIO compatible library for a circular queue or circular FIFO buffer over SPIFFS.
https://rykovv.github.io/spiffs_circular_queue/
MIT License
13 stars 4 forks source link

Variable size struct elem? #6

Open sacalata opened 1 week ago

sacalata commented 1 week ago

Hi, can this be used to store structs of variable size?

I attempted to use a struct with the following members:

struct MyStruct{
    String str;
    int n;
    bool boolean;
    int anotherInt;
    String anotherStr;
};

I was able to enqueue, then dequeue it, and the values were loaded correctly, however it instantly gave me an heap poisoning error: CORRUPT HEAP: Bad head at 0x3ffb99a8. Expected 0xabba1234 got 0x3ffb8014 assert failed: multi_heap_free multi_heap_poisoning.c:259 (head != NULL)

Am I doing anything wrong on the implementation?

bool enqueue(const MyStruct& elem) {
    return spiffs_circular_queue_enqueue(&cq, &elem, sizeof(elem)) == 1;
}

bool dequeue(MyStruct& elem) {
    uint16_t elemSize = sizeof(elem);
    return  spiffs_circular_queue_dequeue(&cq, &elem, &elemSize) == 1;
}

bool initQueue() {
    snprintf(cq.fn, SPIFFS_FILE_NAME_MAX_SIZE, "/spiffs/storedElems");
    cq.elem_size = 0;
    cq.max_size = 1048576 // 1mb;
    return(spiffs_circular_queue_init(&cq) == 1);
}
sacalata commented 1 week ago

I've started to use char[] instead of String, and a fixed size queue, and it worked without errors, I assume the problem is with String allocating memory dynamically?