{
  "id": "go/cgo-callback-gc-pin",
  "signature": "panic: runtime error: cgo callback has unpinned Go pointer",
  "signature_zh": "恐慌：运行时错误：cgo回调包含未固定的Go指针",
  "regex": "panic:\\ runtime\\ error:\\ cgo\\ callback\\ has\\ unpinned\\ Go\\ pointer",
  "domain": "go",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Passing a Go pointer to a C function that stores it and later calls back, but the Go memory was not pinned, causing GC to move it.",
  "root_cause_type": "generic",
  "root_cause_zh": "将Go指针传递给C函数，该函数存储了指针并在之后回调，但Go内存未固定，导致GC移动了它。",
  "versions": [
    {
      "version": "1.22",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "1.23",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Using `runtime.KeepAlive` after callback",
      "why_fails": "KeepAlive only prevents GC before the call, does not pin during C execution.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "Making the pointer a global variable",
      "why_fails": "GC can still move global pointers if not pinned.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use `runtime.Pin` to pin the Go object before passing to C",
      "success_rate": 0.9,
      "how": "p := &myStruct{}\nruntime.Pin(p)\ndefer runtime.Unpin(p)\nC.callback(unsafe.Pointer(p))",
      "condition": "",
      "sources": []
    },
    {
      "action": "Allocate C memory and copy data",
      "success_rate": 0.85,
      "how": "cMem := C.malloc(C.size_t(unsafe.Sizeof(myStruct{})))\nC.callback(cMem)\nC.free(cMem)",
      "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-03-15",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}