{
  "id": "embedded/rtos-heap-corruption",
  "signature": "FreeRTOS: heap corruption detected in prvCheckHeap() at line 234",
  "signature_zh": "FreeRTOS：在prvCheckHeap()第234行检测到堆损坏",
  "regex": "FreeRTOS:\\s*heap\\s+corruption\\s+detected\\s+in\\s+prvCheckHeap\\(\\)\\s+at\\s+line\\s+\\d+",
  "domain": "embedded",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Memory corruption caused by buffer overflow, double free, or use-after-free in heap-allocated objects.",
  "root_cause_type": "generic",
  "root_cause_zh": "内存损坏由缓冲区溢出、重复释放或释放后使用堆分配对象引起。",
  "versions": [
    {
      "version": "FreeRTOSv202212.01",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "STM32Cube_FW_F4_V1.27.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "ARM GCC 12.2.1",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Increasing total heap size in FreeRTOSConfig.h (configTOTAL_HEAP_SIZE)",
      "why_fails": "Larger heap only delays corruption detection; does not fix the root cause of memory mismanagement.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "Adding assert() statements around every malloc/free call",
      "why_fails": "Asserts only halt execution; they do not prevent corruption and may mask intermittent issues.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "Switching to heap_4.c from heap_2.c",
      "why_fails": "Different heap implementation may change behavior but cannot fix logical errors like double free.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Enable heap monitoring by setting configUSE_MALLOC_FAILED_HOOK to 1 and implementing vApplicationMallocFailedHook() to log the task name and stack trace.",
      "success_rate": 0.7,
      "how": "Enable heap monitoring by setting configUSE_MALLOC_FAILED_HOOK to 1 and implementing vApplicationMallocFailedHook() to log the task name and stack trace.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use static allocation for all tasks and queues: replace pvPortMalloc() with static buffers defined at compile time, e.g., StackType_t taskStack[256]; StaticTask_t taskBuffer;",
      "success_rate": 0.9,
      "how": "Use static allocation for all tasks and queues: replace pvPortMalloc() with static buffers defined at compile time, e.g., StackType_t taskStack[256]; StaticTask_t taskBuffer;",
      "condition": "",
      "sources": []
    },
    {
      "action": "Add memory guard words before and each allocated block: #define GUARD 0xDEADBEEF; and check them after free to detect buffer overflow.",
      "success_rate": 0.75,
      "how": "Add memory guard words before and each allocated block: #define GUARD 0xDEADBEEF; and check them after free to detect buffer overflow.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Enable heap monitoring by setting configUSE_MALLOC_FAILED_HOOK to 1 and implementing vApplicationMallocFailedHook() to log the task name and stack trace.",
    "Use static allocation for all tasks and queues: replace pvPortMalloc() with static buffers defined at compile time, e.g., StackType_t taskStack[256]; StaticTask_t taskBuffer;",
    "Add memory guard words before and each allocated block: #define GUARD 0xDEADBEEF; and check them after free to detect buffer overflow."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://www.freertos.org/Documentation/02-Kernel/02-Kernel-features/03-Memory-management/01-Memory-management",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.72,
  "resolvable": "true",
  "first_seen": "2024-01-05",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}