ESP32‑CAM PSRAM causes heap corruption when added to heap (CAPS_ALLOC) — need guidance

hamimmahmud0
Posts: 1
Joined: Fri Jan 23, 2026 6:10 am

ESP32‑CAM PSRAM causes heap corruption when added to heap (CAPS_ALLOC) — need guidance

Postby hamimmahmud0 » Fri Jan 23, 2026 6:23 am

Hi ESP‑IDF community,

I’m stuck on an ESP32‑CAM (AI‑Thinker) PSRAM issue and would appreciate guidance. The system only crashes when PSRAM is added to the heap. With PSRAM disabled, SD mount and Wi‑Fi are stable

Hardware
  • Board: AI‑Thinker ESP32‑CAM
    Chip: ESP32‑D0WD‑V3 (rev 3.1)
    PSRAM: 8MB
    Flash: 4MB
Software
  • ESP‑IDF: v5.5.2-5-gc71a3906e4-dirty
    esp32-camera (managed component)
Symptom
Once PSRAM is added to the heap (even with CAPS_ALLOC), a heap corruption crash occurs during camera init:

Code: Select all

I (2893) esp_netif_handlers: sta ip: 10.13.20.57, mask: 255.255.255.0, gw: 10.13.20.1
I (2893) mdns_mem: mDNS task will be created from internal RAM
W (2893) esp_psram: Virtual address not enough for PSRAM, map as much as we can. 4MB is mapped
I (2903) esp_psram: Adding pool of 4096K of PSRAM memory to heap allocator
I (2903) cam_hal: cam init ok
I (2913) sccb-ng: pin_sda 26 pin_scl 27
I (2913) sccb-ng: sccb_i2c_port=1
Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.

PC      : 0x40094977  A1      : 0x3ffbf980  EXCVADDR: 0x00000003
--- 0x40094977: block_size at components/heap/tlsf/tlsf_block_functions.h:87

Backtrace:
0x40094974: block_locate_free
0x40094175: multi_heap_malloc_impl
0x400942ea: multi_heap_malloc (poisoning)
0x4008260d: aligned_or_unaligned_alloc
0x40082731: heap_caps_aligned_alloc_base
0x40082765: heap_caps_malloc_base
0x400828e2: heap_caps_calloc_base
0x400824f6: heap_caps_calloc
0x401a052f: i2c_new_master_bus (components/esp_driver_i2c/i2c_master.c:1032)
0x400fabb2: SCCB_Init (esp32-camera/driver/sccb-ng.c:133)
0x400efaaa: camera_probe
0x400efd12: esp_camera_init
0x400dce85: init_camera (main/app_main.c:814)
0x400de136: app_main (main/app_main.c:996)

What I tried
1) PSRAM disabled
✅ SD mount & Wi‑Fi stable
❌ No camera frame buffers in PSRAM

2) CONFIG_SPIRAM_USE_MALLOC
❌ Heap corruption in Wi‑Fi/logging or FATFS mount

3) CAPS_ALLOC + BOOT_INIT off
PSRAM initialized manually just before camera init (esp_psram_init() + esp_psram_extram_add_to_heap_allocator()).
Still crashes when heap allocations start hitting PSRAM.
4) FATFS internal only
CONFIG_FATFS_ALLOC_PREFER_EXTRAM=n
Still crashes after PSRAM heap add.

5) Patch to avoid default heap using PSRAM
I patched esp_psram_extram_add_to_heap_allocator() to exclude MALLOC_CAP_DEFAULT when CONFIG_SPIRAM_USE_MALLOC is off, so PSRAM should only be used for explicit MALLOC_CAP_SPIRAM. Testing this now.

sdkconfig snippet

Code: Select all

CONFIG_SPIRAM=y
CONFIG_SPIRAM_MODE_QUAD=y
CONFIG_SPIRAM_TYPE_AUTO=y
CONFIG_SPIRAM_SPEED_40M=y
CONFIG_SPIRAM_BOOT_HW_INIT=y
# CONFIG_SPIRAM_BOOT_INIT is not set
CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION=y
# CONFIG_SPIRAM_USE_MEMMAP is not set
CONFIG_SPIRAM_USE_CAPS_ALLOC=y
# CONFIG_SPIRAM_USE_MALLOC is not set
# CONFIG_SPIRAM_MEMTEST is not set
# CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP is not set

CONFIG_SPIRAM_CACHE_WORKAROUND=y
CONFIG_SPIRAM_CACHE_WORKAROUND_STRATEGY_MEMW=y

CONFIG_SPIRAM_BANKSWITCH_ENABLE=y
CONFIG_SPIRAM_BANKSWITCH_RESERVE=8
CONFIG_SPIRAM_OCCUPY_NO_HOST=y

CONFIG_FATFS_ALLOC_PREFER_EXTRAM=n

Camera config:

Code: Select all

diff --git a/components/esp_psram/system_layer/esp_psram.c b/components/esp_psram/system_layer/esp_psram.c
index 0000000..0000000 100644
--- a/components/esp_psram/system_layer/esp_psram.c
+++ b/components/esp_psram/system_layer/esp_psram.c
@@ -446,7 +446,14 @@ esp_err_t esp_psram_extram_add_to_heap_allocator(void)
 {
     esp_err_t ret = ESP_FAIL;

-    uint32_t byte_aligned_caps[] = {MALLOC_CAP_SPIRAM | MALLOC_CAP_DEFAULT, 0, MALLOC_CAP_8BIT | MALLOC_CAP_32BIT | MALLOC_CAP_SIMD};
+    uint32_t byte_aligned_caps[] = {
+#if CONFIG_SPIRAM_USE_MALLOC
+        MALLOC_CAP_SPIRAM | MALLOC_CAP_DEFAULT,
+#else
+        MALLOC_CAP_SPIRAM,
+#endif
+        0, MALLOC_CAP_8BIT | MALLOC_CAP_32BIT | MALLOC_CAP_SIMD
+    };

     if (s_psram_ctx.regions_to_heap[PSRAM_MEM_32BIT_ALIGNED].size) {
         assert(s_psram_ctx.regions_to_heap[PSRAM_MEM_32BIT_ALIGNED].vaddr_start);
-        uint32_t word_aligned_caps[] = {MALLOC_CAP_SPIRAM | MALLOC_CAP_DEFAULT, 0, MALLOC_CAP_32BIT};
+        uint32_t word_aligned_caps[] = {
+#if CONFIG_SPIRAM_USE_MALLOC
+            MALLOC_CAP_SPIRAM | MALLOC_CAP_DEFAULT,
+#else
+            MALLOC_CAP_SPIRAM,
+#endif
+            0, MALLOC_CAP_32BIT
+        };
         ret = heap_caps_add_region_with_caps(word_aligned_caps,
                                              s_psram_ctx.regions_to_heap[PSRAM_MEM_32BIT_ALIGNED].vaddr_start,
                                              s_psram_ctx.regions_to_heap[PSRAM_MEM_32BIT_ALIGNED].vaddr_end);

Question

Is there a supported way in ESP‑IDF 5.5 to keep PSRAM out of the default heap while still allowing explicit heap_caps_malloc(..., MALLOC_CAP_SPIRAM) allocations?

Has anyone seen heap corruption like this on ESP32‑CAM when PSRAM is added to the heap? Any recommended config or known errata?

Thanks for any advice.

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

Re: ESP32‑CAM PSRAM causes heap corruption when added to heap (CAPS_ALLOC) — need guidance

Postby MicroController » Fri Jan 23, 2026 8:44 am

Is there a supported way in ESP‑IDF 5.5 to keep PSRAM out of the default heap while still allowing explicit heap_caps_malloc(..., MALLOC_CAP_SPIRAM) allocations?
Yes, you already have it set up for this:

Code: Select all

CONFIG_SPIRAM_USE_CAPS_ALLOC=y
# CONFIG_SPIRAM_USE_MALLOC is not set
I suggest testing with a minimal application to check if PSRAM does work at all on your chip/module.

Who is online

Users browsing this forum: Baidu [Spider], Semrush [Bot] and 5 guests