我有一个偶尔会崩溃的应用程序:
panic :运行时错误:无效的内存地址或零指针取消引用 [信号 SIGSEGV:分段违规代码=0x1 地址=0x20 pc=0x122e64a]
跟踪一直导致返回语句,该语句返回一个结构和错误。新(“一些用于调试的文本:”+ err.Error())
该结构似乎没有任何可以取消引用指针的内容,但我重构了该函数,因此它使用了按引用传递并且不需要返回该函数;它只返回了 errors.New()。 panic 还是发生了。
我检查了函数并对其进行了更改,因此它只返回错误,没有错误。New() 字符串加上 err.Error()。现在我似乎不能再引起 panic 了……
所以问题是:关于 errors.New() 使用 err.Error() 连接到字符串会导致 return 语句中出现那种类型的 panic 吗?
编辑:添加触发偶尔 panic 的代码片段:
strctStats.intThreadPool80ConnectionCount, err = strconv.ParseInt(strctStats.strThreadPool80ConnectionCount, 10, 64)
if err != nil {
// Exit external application; send the closing sequences
tmPause := time.NewTimer(time.Second * 2)
<-tmPause.C
stdIn.Write([]byte("close\n"))
tmPause = time.NewTimer(time.Second * 2)
<-tmPause.C
stdIn.Write([]byte("quit\n"))
return errors.New("Could not parse integer: " + err.Error())
}
编辑 2:@lmars 请求堆栈跟踪。这是转储到控制台的内容。不确定它有何帮助(除了函数堆栈和错误/调用产生的行号之外,您能否解释它告诉我们的内容?我是处理其中一些跟踪的新手)
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x122e64a]
goroutine 1 [running]:
main.JMXCheck(0xc42012c000, 0x1a, 0xc420018084, 0x2e, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
/Users/bsilver/go/src/nagios_tomcat_threadinfo/nagios_tomcat_threadinfo.go:590 +0x38ca
main.main()
/Users/bsilver/go/src/nagios_tomcat_threadinfo/nagios_tomcat_threadinfo.go:146 +0x3cc
最佳答案
举个例子
package main
import (
"errors"
)
func f() error {
err := error(nil)
return errors.New("Could not parse integer: " + err.Error())
}
func main() {
f()
}
输出:
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x44ecd6]
goroutine 1 [running]:
main.f(0xc420022070, 0xc420022070)
/home/peter/gopath/src/so/error.go:9 +0x26
main.main()
/home/peter/gopath/src/so/error.go:13 +0x22
exit status 2
参见 How to create a Minimal, Complete, and Verifiable example. .
关于go - 是否有可能触发 "invalid memory address or nil pointer dereference"的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46372662/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
请帮助我理解范围运算符...和..之间的区别,作为Ruby中使用的“触发器”。这是PragmaticProgrammersguidetoRuby中的一个示例:a=(11..20).collect{|i|(i%4==0)..(i%3==0)?i:nil}返回:[nil,12,nil,nil,nil,16,17,18,nil,20]还有:a=(11..20).collect{|i|(i%4==0)...(i%3==0)?i:nil}返回:[nil,12,13,14,15,16,17,18,nil,20] 最佳答案 触发器(又名f/f)是
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我正在学习Rails,并阅读了关于乐观锁的内容。我已将类型为integer的lock_version列添加到我的articles表中。但现在每当我第一次尝试更新记录时,我都会收到StaleObjectError异常。这是我的迁移:classAddLockVersionToArticle当我尝试通过Rails控制台更新文章时:article=Article.first=>#我这样做:article.title="newtitle"article.save我明白了:(0.3ms)begintransaction(0.3ms)UPDATE"articles"SET"title"='dwdwd
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案