Skip to content

Memory leak: CString not freed in explain.go Property() #617

@e-gineer

Description

@e-gineer

Description

The Property() function in explain.go creates two C.CString allocations that are never freed, causing a memory leak.

Current Code

func (e Explainer) Property(k, v string) {
    C.ExplainPropertyText(C.CString(k), C.CString(v), e.ES)
}

Problem

C.CString() allocates memory on the C heap which must be explicitly freed with C.free(). The current code passes the result directly to ExplainPropertyText without storing it, making it impossible to free.

Impact

  • Severity: Low
  • Frequency: Only during EXPLAIN queries (development/debugging)
  • Memory leaked: ~50-100 bytes per EXPLAIN property

Proposed Fix

Store CString results in variables and free them after use:

func (e Explainer) Property(k, v string) {
    ck := C.CString(k)
    cv := C.CString(v)
    defer C.free(unsafe.Pointer(ck))
    defer C.free(unsafe.Pointer(cv))
    C.ExplainPropertyText(ck, cv, e.ES)
}

Using defer is appropriate here since EXPLAIN is not a hot path.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions