【问题标题】:invalid memory address or nil pointer dereference无效的内存地址或 nil 指针取消引用
【发布时间】:2013-09-16 21:47:09
【问题描述】:

我是 gccgo 的新手,在编译/运行以下代码时需要帮助(可以在“标准”go 编译器中正常工作(抱歉,我不知道正确的命名法)):

我的 gcc:

nailor@macbuntu:*rgo/src/tictoc-demo$ gccgo -v
Using built-in specs.
COLLECT_GCC=gccgo
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
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
Thread model: posix
gcc version 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$

我在这里做错了什么?

【问题讨论】:

    标签: go gccgo


    【解决方案1】:

    你没有做错任何事。在进行全静态链接时,这看起来像是编译器中的错误。尝试使用 -static-libgo 链接,它应该可以工作。

    这是 gdb 中的回溯:

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

    我会看看上游是否已经为此提交了一个错误,否则提交一个。

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

    【讨论】:

    • 谢谢!我现在感觉好多了!注意我需要-static-libgo -static-libgcc,原因如下:stackoverflow.com/a/10597279/2536029
    • 没问题,我明白了。它对我来说不需要那个额外的标志,但我在 Raring。
    【解决方案2】:

    此错误已在 4.8 版本中修复。

    使用 4.7 时要与 -static 链接,您可以在链接时添加 -Wl,-u,pthread_create

    【讨论】:

      猜你喜欢
      • 2020-04-17
      • 2015-06-18
      • 2017-03-05
      • 2019-04-19
      • 2017-03-18
      • 2015-04-24
      • 2014-01-12
      • 2014-01-29
      • 1970-01-01
      相关资源
      最近更新 更多