malloc recursive mutex error

thesulaj
Posts: 2
Joined: Sun Apr 11, 2021 8:28 am

malloc recursive mutex error

Postby thesulaj » Sun Apr 11, 2021 8:52 am

I am attempting to dynamically allocate memory. When I allocate relatively large blocks of memory together in succession (around 800 bytes at a time, around 5 times) I am notified that `abort()` has been called with no error message, just a file path:

I (322) cpu_start: Starting scheduler on PRO CPU.
I (727) tsens: Config temperature range [-10°C ~ 80°C], error < 1°C

abort() was called at PC 0x40024263 on core 0
0x40024263: lock_acquire_generic at /Users/ss/Documents/dev/esp/esp-idf/components/newlib/locks.c:138

Backtrace:0x40027783:0x3ffc34c0 0x4002802d:0x3ffc34e0 0x4002ceaa:0x3ffc3500 0x40024263:0x3ffc3570 0x400243ad:0x3ffc35a0 0x4001a245:0x3ffc35c0 0x40000baa:0x3ffc35e0 0x40000a65:0x3ffc3690 0x4001a428:0x3ffc36e0 0x4002c0b3:0x3ffc3710 0x4002cbe3:0x3ffc3730 0x4002357d:0x3ffc3750 0x400235ad:0x3ffc3770 0x4002ceb5:0x3ffc3790 0x4008564d:0x3ffc37b0 0x40085532:0x3ffc37d0 0x40085466:0x3ffc37f0 0x40097a62:0x3ffc3810 0x4002a289:0x3ffc3830
0x40027783: panic_abort at /Users/ss/Documents/dev/esp/esp-idf/components/esp_system/panic.c:354
0x4002802d: esp_system_abort at /Users/ss/Documents/dev/esp/esp-idf/components/esp_system/system_api.c:112
0x4002ceaa: abort at /Users/ss/Documents/dev/esp/esp-idf/components/newlib/abort.c:46
0x40024263: lock_acquire_generic at /Users/ss/Documents/dev/esp/esp-idf/components/newlib/locks.c:138
0x400243ad: _lock_acquire_recursive at /Users/ss/Documents/dev/esp/esp-idf/components/newlib/locks.c:166
0x4002c0b3: block_locate_free at /Users/ss/Documents/dev/esp/esp-idf/components/heap/heap_tlsf.c:447
(inlined by) tlsf_malloc at /Users/ss/Documents/dev/esp/esp-idf/components/heap/heap_tlsf.c:757
0x4002cbe3: multi_heap_malloc_impl at /Users/ss/Documents/dev/esp/esp-idf/components/heap/multi_heap.c:197
0x4002357d: heap_caps_malloc at /Users/ss/Documents/dev/esp/esp-idf/components/heap/heap_caps.c:145
0x400235ad: heap_caps_malloc_default at /Users/ss/Documents/dev/esp/esp-idf/components/heap/heap_caps.c:177
0x4002ceb5: malloc at /Users/ss/Documents/dev/esp/esp-idf/components/newlib/heap.c:31
0x4008564d: hu_de_ba_es_init at /Users/ss/Documents/dev/embedded/the_proj/components/hu_de/src/hu_de_ba_est.c:20
0x40085532: hu_de_init at /Users/ss/Documents/dev/embedded/the_proj/components/hu_de/src/hu_de.c:23
0x40085466: app_main at /Users/ss/Documents/dev/embedded/the_proj/hu_de_pg/build/../main/hu_de_pg.c:70
0x40097a62: main_task at /Users/ss/Documents/dev/esp/esp-idf/components/freertos/port/port_common.c:134
0x4002a289: vPortTaskWrapper at /Users/ss/Documents/dev/esp/esp-idf/components/freertos/port/xtensa/port.c:168

When I open that file and navigate to line 138, I see the following comment:

Code: Select all

/* In ISR Context */
if (mutex_type == queueQUEUE_TYPE_RECURSIVE_MUTEX) {
  abort(); /* recursive mutexes make no sense in ISR context */
}
From my understanding, malloc() is attempting to block on a mutex. However, this is occurring from a recursive ISR, which is a problem (?). However, what is confusing me is that my code calling malloc is not in an ISR at all, it is all inside an initialization function at the start of the program, right at the beginning of app_main(). So why does this code end up being called in an ISR? Is it even being called in an ISR? Here is my code calling malloc():

Code: Select all

    P = malloc(n_pixels * sizeof(float));
    if (P == NULL) {
        res = ESP_ERR_NO_MEM;
        goto cleanup;
    }
    for (i = 0; i < n_pixels; i++) {
        P[i] = P_INIT;
    }

    X = malloc(2 * n_pixels * sizeof(float));
    if (X == NULL) {
        res = ESP_ERR_NO_MEM;
        goto cleanup;
    }
    for (i = 0; i < n_pixels * 2; i++) {
        X[i] = t_init;
        X[i + 1] = td_init;
    }

    A = malloc(4 * sizeof(float));
    if (A == NULL) {
        res = ESP_ERR_NO_MEM;
        goto cleanup;
    }
    A[0] = 1; A[1] = 1; A[2] = 0; A[3] = 1;
There are other initialization functions after this one that allocate memory, resulting in around 5 total relatively large malloc's.

Any help is greatly appreciated. Thanks!

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: malloc recursive mutex error

Postby ESP_Angus » Tue Apr 13, 2021 8:34 am

Hi thesulaj,

Are you calling malloc() inside a critical section, i.e. with interrupts disabled? (this actually used to work, but looks like it might not any more.) This looks similar to ISR context from point of view of the CPU state.

Otherwise, can you please share the ESP-IDF version you're using?

Thanks,

Angus

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 92 guests