Skip to content

finalizer-captures-object

Detect finalizers that retain the object they should release.

Updated View as Markdown

Default severity: error

A finalizer closure that captures the finalized object keeps that object reachable. The garbage collector can never make the object eligible for finalization, so the finalizer never runs and the object leaks.

Use the finalizer function’s parameter to operate on the object instead of capturing the outer variable.

Bad

runtime.SetFinalizer(object, func(*resource) { object.Close() })

Good

runtime.SetFinalizer(object, func(object *resource) { object.Close() })
Navigation

Type to search…

↑↓ navigate↵ selectEsc close