# error: cgo: leaked C memory: 1024 bytes

- **ID:** `go/cgo-memory-leak`
- **Domain:** go
- **Category:** resource_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

C memory allocated via `C.malloc` not freed with `C.free`, leading to memory leak.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.18 | active | — | — |
| 1.19 | active | — | — |

## Workarounds

1. **Always pair `C.malloc` with `defer C.free` immediately** (95% success)
   ```
   cMem := C.malloc(1024)
defer C.free(cMem)
// use cMem
   ```
2. **Use a finalizer for automatic cleanup** (80% success)
   ```
   type cBuffer struct { ptr unsafe.Pointer }
func newCBuffer(size int) *cBuffer {
    b := &cBuffer{C.malloc(C.size_t(size))}
    runtime.SetFinalizer(b, func(b *cBuffer) { C.free(b.ptr) })
    return b
}
   ```

## Dead Ends

- **Assuming Go GC will collect C memory** — Go GC only manages Go memory, not C allocations. (100% fail)
- **Using `defer C.free` but not in all code paths** — Early return or panic may skip defer. (60% fail)
