1

My code gives me a segmentation fault when I run it with the rdebug option on command line:

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

The error is:

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

When I run it without the -rdebug option, I get an error:

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>'

If I modify the sample data the code works fine.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
ppaul74
  • 751
  • 1
  • 13
  • 21

2 Answers2

1

Looks like you are missing ft12 in font_size.

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

My ruby-1.9.2-p290 doesn't segfault but gives out this error. The segfault bug should have already been fixed.

/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/debug.rb:130:in `eval':comparison of String with String failed
chifung7
  • 2,531
  • 1
  • 19
  • 17
1

Ruby 1.9.2-p0 is old, and had some instabilities. I'd recommend upgrading to the current 1.9.2-p290.

You're on MacOS, but your output doesn't show if you're using RVM or something else for the 1.9.2-p0 version of Ruby. If you are using RVM, do:

rvm get head

to update it to the current version. Do:

rvm reload

to load the current version of RVM into memory.

Then you can upgrade to the current version of 1.9.2 using:

rvm upgrade 1.9.2-p0 1.9.2-p290
the Tin Man
  • 158,662
  • 42
  • 215
  • 303