{"record":{"id":"47298b69b3bd270e","repo":"go-delve/delve","slug":"couldn-t-read-pointer-w","errorCode":null,"errorMessage":"couldn't read pointer: %w","messagePattern":"couldn't read pointer: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/proc/eval.go","lineNumber":2327,"sourceCode":"\t\treturn\n\t}\n\n\tif xev == nilVariable {\n\t\tstack.err = errors.New(\"nil can not be dereferenced\")\n\t\treturn\n\t}\n\n\tif len(xev.Children) == 1 {\n\t\t// this branch is here to support pointers constructed with typecasts from ints\n\t\txev.Children[0].OnlyAddr = false\n\t\tstack.push(&(xev.Children[0]))\n\t\treturn\n\t}\n\txev.loadPtr()\n\tif xev.Unreadable != nil {\n\t\tval, ok := constant.Uint64Val(xev.Value)\n\t\tif ok && val == 0 {\n\t\t\tstack.err = fmt.Errorf(\"couldn't read pointer: %w\", xev.Unreadable)\n\t\t\treturn\n\t\t}\n\t}\n\trv := &xev.Children[0]\n\tif rv.Addr == 0 {\n\t\tstack.err = errors.New(\"nil pointer dereference\")\n\t\treturn\n\t}\n\tstack.push(rv)\n}\n\n// Evaluates expressions &<subexpr>\nfunc (scope *EvalScope) evalAddrOf(op *evalop.AddrOf, stack *evalStack) {\n\txev := stack.pop()\n\tif xev.Addr == 0 || xev.DwarfType == nil {\n\t\tstack.err = fmt.Errorf(\"can not take address of %q\", astutil.ExprToString(op.Node.X))\n\t\treturn\n\t}","sourceCodeStart":2309,"sourceCodeEnd":2345,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/proc/eval.go#L2309-L2345","documentation":"Thrown by evalPointerDeref after xev.loadPtr() fails to read the pointed-to memory from the debuggee: the variable's Unreadable field is set and the pointer's constant value is 0, so the failure is a nil-pointer dereference reported with the underlying read error wrapped via %w. Delve wraps the memory-read error so both the cause (nil address) and the original read failure are visible.","triggerScenarios":"Evaluating `*p` where p is a nil pointer (address 0): loadPtr attempts to read at address 0, the read fails, and since the constant value of the pointer is 0 the error is reported as `couldn't read pointer: <read error>` rather than continuing to inspect children.","commonSituations":"Inspecting a pointer field before it was assigned, a function returned nil, an optional field was not populated, or a struct pointer from a map/interface is nil during a panic investigation.","solutions":["Print the pointer itself (`print p`) to confirm it is nil before dereferencing.","Step forward in execution until the pointer is assigned a valid address, then evaluate `*p`.","Use conditional breakpoints or `if p != nil` checks in the inspected code to avoid reaching a state with nil pointers.","If the pointer is non-nil but still unreadable, verify the memory region is valid and the binary/debug info match the running process."],"exampleFix":"// before\n(dlv) print *p\ncouldn't read pointer: invalid address 0x0\n\n// after: check first\n(dlv) print p // nil\n(dlv) break main.go:42 // stop after p = &x\n(dlv) continue\n(dlv) print *p","handlingStrategy":"validation","validationCode":"// Check the pointer before dereferencing:\n(dlv) print p          // nil -> do not evaluate *p\n(dlv) print p != nil   // use as a breakpoint condition guard\n// In program code: if p != nil { use(*p) }","typeGuard":"func validPointer[T any](p *T) bool { return p != nil }","tryCatchPattern":null,"preventionTips":["Print the pointer before dereferencing it.","Place breakpoints after pointer assignment.","Guard breakpoint conditions with `p != nil &&`."],"tags":["debugger","pointer","nil-pointer","memory-read"],"backgroundTag":"nil-pointer-dereference","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}