{
  "id": "go/cgo-memory-leak",
  "signature": "error: cgo: leaked C memory: 1024 bytes",
  "signature_zh": "错误：cgo：泄露的C内存：1024字节",
  "regex": "error:\\ cgo:\\ leaked\\ C\\ memory:\\ 1024\\ bytes",
  "domain": "go",
  "category": "resource_error",
  "subcategory": null,
  "root_cause": "C memory allocated via `C.malloc` not freed with `C.free`, leading to memory leak.",
  "root_cause_type": "generic",
  "root_cause_zh": "通过`C.malloc`分配的C内存未使用`C.free`释放，导致内存泄漏。",
  "versions": [
    {
      "version": "1.18",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "1.19",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Assuming Go GC will collect C memory",
      "why_fails": "Go GC only manages Go memory, not C allocations.",
      "fail_rate": 1.0,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using `defer C.free` but not in all code paths",
      "why_fails": "Early return or panic may skip defer.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Always pair `C.malloc` with `defer C.free` immediately",
      "success_rate": 0.95,
      "how": "cMem := C.malloc(1024)\ndefer C.free(cMem)\n// use cMem",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use a finalizer for automatic cleanup",
      "success_rate": 0.8,
      "how": "type cBuffer struct { ptr unsafe.Pointer }\nfunc newCBuffer(size int) *cBuffer {\n    b := &cBuffer{C.malloc(C.size_t(size))}\n    runtime.SetFinalizer(b, func(b *cBuffer) { C.free(b.ptr) })\n    return b\n}",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-06-01",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}