heap_caps_malloc was called but failed to allocate

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

heap_caps_malloc was called but failed to allocate

Postby zazas321 » Wed May 24, 2023 7:24 am

Hello. I have some issues with heap memory allocation. I have started to dig into how it works.

in my code, I have registered failed alloc callback:

Code: Select all

esp_err_t error = heap_caps_register_failed_alloc_callback(heap_caps_alloc_failed_hook);

Code: Select all

void heap_caps_alloc_failed_hook(size_t requested_size, uint32_t caps, const char *function_name) {
    printf("%s was called but failed to allocate %d bytes with %ld capabilities. \n", function_name, requested_size,caps);
}
Occasionally, I can see the following errors pop up in the logs:

Code: Select all

heap_caps_malloc was called but failed to allocate 1696 bytes with 6144 capabilities.
I started looking into malloc function declaration and noticed something strange:

In flash_mock.cpp I have found the following:

Code: Select all

void *heap_caps_malloc( size_t size, uint32_t caps )
{
    return NULL;
}
Is this function deprecated? what is flash_mock.cpp?

In the heap_caps.c I have also found the real heap_caps_malloc function declaration:

Code: Select all


IRAM_ATTR void *heap_caps_malloc( size_t size, uint32_t caps){

    void* ptr = heap_caps_malloc_base(size, caps);

    if (!ptr && size > 0){
        heap_caps_alloc_failed(size, caps, __func__);
    }

    return ptr;
}

MicroController
Posts: 1708
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: heap_caps_malloc was called but failed to allocate

Postby MicroController » Wed May 24, 2023 9:13 am

zazas321 wrote:
Wed May 24, 2023 7:24 am

Occasionally, I can see the following errors pop up in the logs:

Code: Select all

heap_caps_malloc was called but failed to allocate 1696 bytes with 6144 capabilities.
This may or may not indicate a problem: heap_caps_malloc_prefer(...) may try multiple allocations which may fail until one succeeds. E.g. if PSRAM is preferred but no external RAM is present, the first attempt (PSRAM) will always fail, but the next attempt (e.g. DRAM) may succeed thus not causing any problems.

Specifically, CAPS = 6144 is "IRAM" + "byte-adresssable", which on an ESP32 is not physically present, as its IRAM is only word-adressable.
what is flash_mock.cpp?
Looks like a mock/stub for (unit-)testing of IDF. Don't bother with it.

Who is online

Users browsing this forum: Baidu [Spider] and 102 guests