2012-01-21 87 views
1

我的代碼給了我一個分段錯誤,當我與命令行中,RDebug選項來運行它:爲什麼使用-rdebug選項會出現分段錯誤?

font_size = {"ft0"=>26, "ft1"=>10, "ft2"=>8, "ft3"=>9, "ft4"=>9, "ft5"=>7, "ft6"=>8, "ft7"=>9, "ft8"=>7, "ft9"=>8, "ft10"=>9, "ft11"=>8} 
classes = ["ft3", "ft10", "ft6", "ft9", "ft11", "ft4", "ft2", "ft12"] 
large_class = classes.max{|a,b| font_size[a] <=> font_size[b] } 
puts large_class.to_s 

的錯誤是:

test_segfault.rb:3: [BUG] Segmentation fault 
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.3.0] 

-- control frame ---------- 
c:0006 p:---- s:0016 b:0016 l:000011 d:000015 IFUNC 
c:0005 p:---- s:0014 b:0014 l:000013 d:000013 CFUNC :each 
c:0004 p:---- s:0012 b:0012 l:000011 d:000011 CFUNC :max 
c:0003 p:0089 s:0009 b:0009 l:001588 d:000bc0 EVAL test_segfault.rb:3 
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH 
c:0001 p:0000 s:0002 b:0002 l:001588 d:001588 TOP 
--------------------------- 
-- Ruby level backtrace information ---------------------------------------- 
test_segfault.rb:3:in `<main>' 
test_segfault.rb:3:in `max' 
test_segfault.rb:3:in `each' 

-- C level backtrace information ------------------------------------------- 

[NOTE] 
You may have encountered a bug in the Ruby interpreter or extension libraries. 
Bug reports are welcome. 
For details: http://www.ruby-lang.org/bugreport.html 

Abort trap 

當我沒有-rdebug選項來運行它,我得到一個錯誤:

ruby test_segfault.rb 
test_segfault.rb:3:in `each': comparison of String with String failed (ArgumentError) 
from test_segfault.rb:3:in `max' 
from test_segfault.rb:3:in `<main>' 

如果我修改示例數據代碼工作正常。

回答

1

看起來像是在font_size中缺少ft12

classes.map{|e| font_size[e]} 
[9, 9, 8, 8, 8, 9, 8, nil] 

我的ruby-1.9.2-p290不會段錯誤,但會發出此錯誤。段錯誤應該已經被修復了。

/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/debug.rb:130:in `eval':comparison of String with String failed 
+0

謝謝你們。這有助於很多。我會更新我的紅寶石版本。 – ppaul74

1

Ruby 1.9.2-p0是舊的,並且有一些不穩定性。我建議升級到目前的1.9.2-p290。

你是在MacOS上,但是你的輸出不顯示你是否使用RVM或其他的1.9.2-p0版本的Ruby。如果您使用的是RVM,請執行以下操作:

rvm get head 

將其更新爲當前版本。執行:

rvm reload 

將RVM的當前版本加載到內存中。

然後你就可以使用升級到1.9.2的最新版本:

rvm upgrade 1.9.2-p0 1.9.2-p290