/bin/bash: line 1: 82501 Segmentation fault: 11 ./main
while I try to call a method on a null variable, which is not that friendly to newcomers.Sample code:
import std.stdio;
void main()
{
Greetings g = null;
g.hola();
} /bin/bash: line 1: 82501 Segmentation fault: 11 ./main
while I try to call a method on a null variable, which is not that friendly to newcomers.Sample code:
import std.stdio;
void main()
{
Greetings g = null;
g.hola();
} ~/temp_code » ./greetings
Traceback (most recent call last)
greetings.nim(9) greetings
greetings.nim(6) hola
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
For the code: type
Greetings = ref object
name: string
proc hola(g: Greetings) =
echo g.name
var g: Greetings
g.hola() $ cat main.d
import std.stdio;
class Greetings {
void hello() {
writeln("hello");
}
}
void main()
{
Greetings g = null;
g.hello();
}
$ dmd -g main.d && ./main
Segmentation fault: 11
$ uname -a
Darwin Johan-Ride-Mac.local 15.0.0 Darwin Kernel Version 15.0.0: Sat Sep 19 15:53:46 PDT 2015; root:xnu-3247.10.11~1/RELEASE_X86_64 x86_64
$ dmd --version
DMD64 D Compiler v2.069.0
Copyright (c) 1999-2015 by Digital Mars written by Walter Bright
(EDIT: added osx and dmd versions)I know it's because I'm trying to call a method on "null", but If I'm working on a huge code base, how do I debug this kind of issues if I don't have stack trace with line and error number of the error?
I'd like to get at least the stack trace and the error number with the line of the source code that caused the problem, like in golang:
$ cat main.go
package main
type Greetings struct {
}
func (g Greetings) hello() {
}
func main() {
g := &Greetings{} // heap instance
g = nil // intentionally shooting myself in the foot like I did in D
g.hello()
}
$ go run main.go
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x2025]
goroutine 1 [running]:
main.main()
/Users/ride/Documents/main.go:13 +0x15
goroutine 2 [runnable]:
runtime.forcegchelper()
/opt/boxen/homebrew/Cellar/go/1.4.2/libexec/src/runtime/proc.go:90
runtime.goexit()
/opt/boxen/homebrew/Cellar/go/1.4.2/libexec/src/runtime/asm_amd64.s:2232 +0x1
exit status 2If you are working in a big codebase and have this come up, you can enable core dumps and run it in a debugger to get far more information than just a line number (and line numbers are there too at least if compiled in debug mode).
$ dmd -g main.d && ./main
Segmentation fault: 11
is there anything else I can try?I'm OSX El Capitan 10.11.1
import etc.linux.memoryerror;
shared static this() {
static if (is(typeof(registerMemoryErrorHandler)))
registerMemoryErrorHandler();
}to be honest, it's better than nothing but still kinda difficult to work with:
etc.linux.memoryerror.NullPointerError@src/etc/linux/memoryerror.d(325)
----------------
??:? void etc.linux.memoryerror.sigsegvUserspaceProcess(void*) [0x439045]
??:? void etc.linux.memoryerror.sigsegvDataHandler() [0x438f92]
??:? _Dmain [0x435c17]
??:? _D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv [0x4376da]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x437630]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll() [0x437696]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x437630]
??:? _d_run_main [0x43758d]
??:? main [0x436075]
??:? __libc_start_main [0xb0fa1ec4]You can also do it manually btw with `addr2line -e your_executable 0x435c17` and the sort.
$ dmd -g main.d && ./main
etc.linux.memoryerror.NullPointerError@src/etc/linux/memoryerror.d(325)
----------------
??:? void etc.linux.memoryerror.sigsegvUserspaceProcess(void*) [0x423321]
??:? void etc.linux.memoryerror.sigsegvDataHandler() [0x42326e]
main.d:12 _Dmain [0x4204e5]
??:? _D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv [0x421b8a]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x421ae0]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll() [0x421b46]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x421ae0]
??:? _d_run_main [0x421a3d]
??:? main [0x420597]
??:? __libc_start_main [0xdb825ec4]
this is linux, I use mac as development machine, what can I do for mac? is there a error handler for mac too?2) Spanish ?