无效的内存地址或空指针解引用

huangapple go评论128阅读模式
英文:

invalid memory address or nil pointer dereference

问题

我是新手使用gccgo,我需要帮助编译/运行下面的代码(在“标准”go编译器中可以正常工作):

我的gcc版本:

nailor@macbuntu:*rgo/src/tictoc-demo$ gccgo -v
使用内置规范。
COLLECT_GCC=gccgo
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
目标:x86_64-linux-gnu
配置为:../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.2-0ubuntu3' --with-bugurl=file:///usr/share/doc/gccgo-4.7/README.Bugs --enable-languages=c,c++,go --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
线程模型:posix
gcc版本 4.7.2 (Ubuntu/Linaro 4.7.2-0ubuntu3)

第一个文件:

nailor@macbuntu:*rgo/src/tictoc-demo$ cat tictoc.go
package tictoc

import "fmt"
import "time"

var ticTime = time.Now()
const default_section_name = "measurement"
var section_name = default_section_name

func Tic() {
TicM(default_section_name)
}

func TicM(name string) {
section_name = name
fmt.Printf("[%s] tic\n", section_name)
ticTime = time.Now()
}

func Toc() {
TocM("")
}

func TocM(message string) {
dur := time.Now().Sub(ticTime).Seconds()
fmt.Printf("[%s] [%s] toc %5f\n", section_name, message, dur)
}

func TocTic() {
Toc();
Tic();
}

func TocTicM(message string) {
Toc();
TicM(message);
}

第二个文件:

nailor@macbuntu:*rgo/src/tictoc-demo$ cat tictoc-demo.go
package main

import . "tictoc"

func main(){
Tic()
Toc()
Toc()
TicM("2nd tic")
Toc()
Toc()
TocM("Error")
Toc()
TocM("More Fancy")
Toc()
Toc()
TocTic()
Toc()
}

我的编译错误:

nailor@macbuntu:*rgo/src/tictoc-demo$ gccgo -c tictoc.go
nailor@macbuntu:*rgo/src/tictoc-demo$ gccgo -c tictoc-demo.go
nailor@macbuntu:*rgo/src/tictoc-demo$ gccgo -o tictoc-demo tictoc-demo.o tictoc.o -static
nailor@macbuntu:*rgo/src/tictoc-demo$ ./tictoc-demo
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0]

goroutine 2 [syscall]:

goroutine 1 [runnable]:
nailor@macbuntu:*rgo/src/tictoc-demo2$

我在这里做错了什么?

英文:

I am new to gccgo and I need help with compiling/running the below code (that works all right with the "standard" go compiler (sorry, I do not know the proper nomenclature)):

my gcc:

  1. nailor@macbuntu:*rgo/src/tictoc-demo$ gccgo -v
  2. Using built-in specs.
  3. COLLECT_GCC=gccgo
  4. COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
  5. Target: x86_64-linux-gnu
  6. Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.2-0ubuntu3' --with-bugurl=file:///usr/share/doc/gccgo-4.7/README.Bugs --enable-languages=c,c++,go --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
  7. Thread model: posix
  8. gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-0ubuntu3)

my first file:

  1. nailor@macbuntu:*rgo/src/tictoc-demo$ cat tictoc.go
  2. package tictoc
  3. import "fmt"
  4. import "time"
  5. var ticTime = time.Now()
  6. const default_section_name = "measurement"
  7. var section_name = default_section_name
  8. func Tic() {
  9. TicM(default_section_name)
  10. }
  11. func TicM(name string) {
  12. section_name = name
  13. fmt.Printf("[%s] tic\n", section_name)
  14. ticTime = time.Now()
  15. }
  16. func Toc() {
  17. TocM("")
  18. }
  19. func TocM(message string) {
  20. dur := time.Now().Sub(ticTime).Seconds()
  21. fmt.Printf("[%s] [%s] toc %5f\n", section_name, message, dur)
  22. }
  23. func TocTic() {
  24. Toc();
  25. Tic();
  26. }
  27. func TocTicM(message string) {
  28. Toc();
  29. TicM(message);
  30. }

my second file:

  1. nailor@macbuntu:*rgo/src/tictoc-demo$ cat tictoc-demo.go
  2. package main
  3. import . "tictoc"
  4. func main(){
  5. Tic()
  6. Toc()
  7. Toc()
  8. TicM("2nd tic")
  9. Toc()
  10. Toc()
  11. TocM("Error")
  12. Toc()
  13. TocM("More Fancy")
  14. Toc()
  15. Toc()
  16. TocTic()
  17. Toc()
  18. }

my compilation with the error:

  1. nailor@macbuntu:*rgo/src/tictoc-demo$ gccgo -c tictoc.go
  2. nailor@macbuntu:*rgo/src/tictoc-demo$ gccgo -c tictoc-demo.go
  3. nailor@macbuntu:*rgo/src/tictoc-demo$ gccgo -o tictoc-demo tictoc-demo.o tictoc.o -static
  4. nailor@macbuntu:*rgo/src/tictoc-demo$ ./tictoc-demo
  5. panic: runtime error: invalid memory address or nil pointer dereference
  6. [signal 0xb code=0x1 addr=0x0]
  7. goroutine 2 [syscall]:
  8. goroutine 1 [runnable]:
  9. nailor@macbuntu:*rgo/src/tictoc-demo2$

What am I doing wrong here?

答案1

得分: 4

你没有做错任何事情。这看起来像是编译器在进行完全静态链接时的一个错误。尝试使用-static-libgo进行链接,应该可以解决问题。

这是在gdb中的回溯信息:

  1. 程序收到 SIGSEGV,段错误。
  2. 0x0000000000000000 in ?? ()
  3. (gdb) bt
  4. #0 0x0000000000000000 in ?? ()
  5. #1 0x00000000004adf67 in __wrap_pthread_create ()
  6. #2 0x000000000040657e in runtime_newm ()
  7. #3 0x000000000040665b in matchmg ()
  8. #4 0x0000000000406f15 in syscall.Entersyscall ()
  9. #5 0x0000000000403e5c in runtime_MHeap_Scavenger ()
  10. #6 0x0000000000406e15 in kickoff ()
  11. #7 0x00000000004ba910 in ?? ()
  12. #8 0x0000000000000000 in ?? ()

我会查看是否已经有人报告了这个问题,如果没有的话,我会提交一个报告。

(问题已提交:http://golang.org/issue/6375)

英文:

You are not doing anything wrong. This looks like a bug in the compiler when doing full-static linkage. Try linking with -static-libgo instead, and it should work.

This is the backtrace in gdb:

  1. Program received signal SIGSEGV, Segmentation fault.
  2. 0x0000000000000000 in ?? ()
  3. (gdb) bt
  4. #0 0x0000000000000000 in ?? ()
  5. #1 0x00000000004adf67 in __wrap_pthread_create ()
  6. #2 0x000000000040657e in runtime_newm ()
  7. #3 0x000000000040665b in matchmg ()
  8. #4 0x0000000000406f15 in syscall.Entersyscall ()
  9. #5 0x0000000000403e5c in runtime_MHeap_Scavenger ()
  10. #6 0x0000000000406e15 in kickoff ()
  11. #7 0x00000000004ba910 in ?? ()
  12. #8 0x0000000000000000 in ?? ()

I'll see if there's a bug filed upstream for this already, or file one otherwise.

(issue filed: http://golang.org/issue/6375 )

答案2

得分: 4

这个错误在4.8版本中已经修复。

在使用4.7版本时,可以在链接时添加-Wl,-u,pthread_create来链接-static

英文:

This bug is fixed in the 4.8 release.

To link with -static when using 4.7, you can add -Wl,-u,pthread_create when you link.

huangapple
  • 本文由 发表于 2013年9月12日 21:16:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/18765416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定