Assert failed: heap_caps_free heap_caps.c:381 (heap != NULL && "free() target pointer is outside heap areas")

Hello everyone,

Unfortunately, I haven't been able to find information on my topic. Since I lack in-depth C++ knowledge and this is my first small project, I am at a loss. Here are some background details: I want to merge and use two independently functioning codes. QRCode Reader and TFT_eSPI

Project:
It aims to create a low-cost AR glasses that can recognize, decode, and display the text of a QR code captured by a camera on a TFT display. The displays will be mirrored onto the lenses of the glasses.

Hardware:

  • ESP32-Cam Ai-Thinker
  • TFT ST7798
  • (Arduino 1.8.18)

Pins:

#define TFT_MOSI 12 // In some display driver board, it might be written as "SDA" and so on.
#define TFT_SCLK 14
#define TFT_CS   15  // Chip select control pin
#define TFT_DC   16  // Data Command control pin
#define TFT_RST  2  // Reset pin (could connect to Arduino RESET pin)
//#define TFT_BL   22  // LED back-light

#define CAMERA_MODEL_AI_THINKER \
  {                             \
    .PWDN_GPIO_NUM = 32,        \
    .RESET_GPIO_NUM = -1,       \
    .XCLK_GPIO_NUM = 0,         \
    .SIOD_GPIO_NUM = 26,        \
    .SIOC_GPIO_NUM = 27,        \
    .Y9_GPIO_NUM = 35,          \
    .Y8_GPIO_NUM = 34,          \
    .Y7_GPIO_NUM = 39,          \
    .Y6_GPIO_NUM = 36,          \
    .Y5_GPIO_NUM = 21,          \
    .Y4_GPIO_NUM = 19,          \
    .Y3_GPIO_NUM = 18,          \
    .Y2_GPIO_NUM = 5,           \
    .VSYNC_GPIO_NUM = 25,       \
    .HREF_GPIO_NUM = 23,        \
    .PCLK_GPIO_NUM = 22,        \
  }

Code:

#include <Arduino.h>
#include <ESP32QRCodeReader.h>
#include <TFT_eSPI.h>
#include <SPI.h>

ESP32QRCodeReader reader(CAMERA_MODEL_AI_THINKER);
TFT_eSPI tft = TFT_eSPI();

// Funktion für die QR-Code-Aufgabe
void vQrCodeTask(void *pvParameters)
{
  struct QRCodeData qrCodeData;

  for (;;)
  {
    if (reader.receiveQrCode(&qrCodeData, 100))
    {
      Serial.println("Found QRCode");
      if (qrCodeData.valid)
      {
        Serial.print("Payload: ");
        Serial.println((const char *)qrCodeData.payload);

        // Hier kannst du den QR-Code-Inhalt auf dem TFT-Display anzeigen
        tft.fillScreen(TFT_BLACK);
        tft.setCursor(20, 80);
        tft.setTextColor(TFT_WHITE);
        tft.setTextSize(2);
        tft.println("QR Code:");
        tft.println((const char *)qrCodeData.payload);
      }
      else
      {
        Serial.print("Invalid: ");
        Serial.println((const char *)qrCodeData.payload);
      }
    }
    vTaskDelay(100 / portTICK_PERIOD_MS);
  }
}

void setup()
{
  Serial.begin(115200);
  Serial.println();

  reader.setup();
  reader.beginOnCore(1);

  Serial.println("Setup QRCode Reader");

  tft.init();
  tft.fillScreen(TFT_BLACK);
  tft.setRotation(1);

  Serial.println("Setup TFT Display");

  // Erstelle die QR-Code-Aufgabe
  TaskHandle_t xQrCodeTaskHandle = NULL;
  xTaskCreate(vQrCodeTask, "QrCodeTask", 4096, NULL, 4, &xQrCodeTaskHandle);
  configASSERT(xQrCodeTaskHandle);
}

void loop()
{
  // Füge hier weitere Anweisungen im Hauptloop hinzu, falls erforderlich
  delay(100);
}

Problem:

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:1344
load:0x40078000,len:13936
load:0x40080400,len:3600
entry 0x400805f0

Setup QRCode Reader

assert failed: heap_caps_free heap_caps.c:381 (heap != NULL && "free() target pointer is outside heap areas")

Backtrace: 0x40083821:0x3ffd4960 0x4008a12d:0x3ffd4980 0x4008f8dd:0x3ffd49a0 0x40083c66:0x3ffd4ad0 0x4008f90d:0x3ffd4af0 0x400d4c7e:0x3ffd4b10 0x400d2538:0x3ffd4b30 0x400d2704:0x3ffd81e0

I have the exact same problem and opened my topic here yesterday. With the same lib. But I got the error when I cleared the whole eeprom(flash memory) and cannot restore it. Looking for a help too

Start by using the ESP32 Exception Decoder to get a trace of function call chain that ended up in heap_caps_free heap_caps.c.

I did:

Please copy that into a Code-Tag segment. That's easier to work with than an image. Also, post a GitHub link to the ESP32QRCodeReader library.

Running into the same issue:

assert failed: heap_caps_free heap_caps.c:381 (heap != NULL && "free() target pointer is outside heap areas")

Backtrace: 0x40083e0d:0x3ffcd980 0x40096aa5:0x3ffcd9a0 0x4009c3f9:0x3ffcd9c0 0x40084376:0x3ffcdaf0 0x4009c429:0x3ffcdb10 0x40123a5d:0x3ffcdb30 0x40123a6d:0x3ffcdb50 0x400dc733:0x3ffcdb70 0x400e1979:0x3ffcdbf0 0x400e278e:0x3ffcdc80 0x400d4485:0x3ffcdce0 0x400d4a31:0x3ffcdde0 0x40123605:0x3ffcde10

ELF file SHA256: 160e43d95feb8064

E (11466) esp_core_dump_flash: Core dump flash config is corrupted! CRC=0x7bd5c66f instead of 0x0
Rebooting...


Where it breaks down is in a simple piece of code:


bool CmdProcClass::StartDischarge(uint8_t CiD, uint16_t DiR)
{
	CellData.UpdateCellState(CiD);

	float volt = CellData.GetVoltage(CiD);

	if (volt < 1.5)
	{
		LogWriter.ConsoleOutputLn("StartDischarge cancelled for CiD: " + String(CiD) + " - to low voltage: " + String(volt) + "V.", eDebugLevel::Notice);
		return false;
	}

	DischargeCurrentRampDownValue[CiD] = DiR;
	Charger.set_cell_state(CiD, eCellStatus::StartDischarging);
}

been running into strange behavior all day.

When running the code, it seems to 'jump' to a random part of the code and executes from there onwards. It almost looks like code optimization is making a mess of things. Been rewriting sections of the code understand where it is going wrong, though no success yet.

I didn't even notice until having spend days on finding the issue. Post successful compiled code, the code execution was far from correct. Code executed randomly, like the pointer where the program ran just jumped to a random location and continued from their.

The problem is visible in the code snipped. The developer studio, compiler and all the other bells and wissels a developer uses did NOT pickup this code issue:

bool CmdProcClass::StartDischarge(uint8_t CiD, uint16_t DiR)
{
	CellData.UpdateCellState(CiD);

	float volt = CellData.GetVoltage(CiD);

	if (volt < 1.5)
	{
		LogWriter.ConsoleOutputLn("StartDischarge cancelled for CiD: " + String(CiD) + " - to low voltage: " + String(volt) + "V.", eDebugLevel::Notice);
		return false;
	}

	DischargeCurrentRampDownValue[CiD] = DiR;
	Charger.set_cell_state(CiD, eCellStatus::StartDischarging);
 
      << ______ SOMETHING IS MISSING HERE _______ >>
}

Yes, there is something missing in this area. The function requires a return value, though only in the 'if' statement there is a return false. Though at the end of the code there is no return value provided...

After applying a 'return true;' at the last line of the function, the issues all went away. No more heap errors, no more random code execution and unexplained crashes.


bool CmdProcClass::StartDischarge(uint8_t CiD, uint16_t DiR)
{
	CellData.UpdateCellState(CiD);

	float volt = CellData.GetVoltage(CiD);

	if (volt < 1.5)
	{
		LogWriter.ConsoleOutputLn("StartDischarge cancelled for CiD: " + String(CiD) + " - to low voltage: " + String(volt) + "V.", eDebugLevel::Notice);
		return false;
	}

	LogWriter.ConsoleOutputLn("StartDischarge for CiD: " + String(CiD) + " - with current: " + String(DiR) + "mA.", eDebugLevel::Notice);
	
	DischargeCurrentRampDownValue[CiD] = DiR;
	NewDischargeCurrentChangeTime[CiD] = millis();
	Charger.set_cell_state(CiD, eCellStatus::StartDischarging);

	return true;
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.