Search code examples
ruby-on-railsrmagickgruff

Segmentation fault in Gruff prevents me from generating a simple chart


I am experimenting with simple projects and RonR, specifically trying to generate some graphs using the gruff gem with rmagick and image magic. Everything is installed successfully, but when I run even a simple .rb with a chart it gives me the:

C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/gruff-0.3.4/lib/gruff/base.rb:467: [BUG] Segmentation fault ruby 1.8.7 (2011-06-30 patchlevel 352) [i386-mingw32]

This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.

The line in the base.rb file is referring to this piece of code:

# Writes the graph to a file. Defaults to 'graph.png'
#
# Example:
#   write('graphs/my_pretty_graph.png')
def write(filename="graph.png")
   draw()
   @base_image.write(filename)
end

At the same time, the simple graph I am trying to draws seems simple enough, and it is from an example from Gruff's website:

#!/usr/bin/ruby

require 'rubygems'
require 'gruff'
g = Gruff::Bar.new('800x500') # Define a custom size
g.sort = false  # Do NOT sort data based on values
g.maximum_value = 50  # Declare a max value for the Y axis
g.minimum_value = 0   # Declare a min value for the Y axis
g.theme_37signals  # Declare a theme from the presets available

g.title = 'A more advanced bar chart'

g.data('Foo', [5, 10, 24])
g.data('Bar', [15, 3, 10])
g.data('Else', [38, 15, 32])

g.labels = {0 => 'Last year', 1 => 'This year', 2 => 'Next year'} # Define labels for each of the "columns" in data

g.write('a_more_advanced_graph.png')

I either try to load the above .rb script in irb, or directly load it in a controller. Both ways fail.

Even if try to do a graph in the controller and then show it in the view, it fails again. Every time the same segmentation error.

Just to note, I have the gems installed, as well added in Gemfile (if trying to do it from withing an app). I am running Rails on Windows and gruff is version 0.3.4, although I also tried with latest version 0.3.6 but the same problem there as well.

Any ideas or suggestions are highly appreciated!


Solution

  • It's probably NOT related to your code, but I can't be positive.

    Analyze the error:

    C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/gruff-0.3.4/lib/gruff/base.rb:467:
       [BUG] Segmentation fault ruby 1.8.7 (2011-06-30 patchlevel 352) [i386-mingw32]
    

    That says that while the gruff gem was executing, when it hit line 467 in the base.rb file, ruby threw a segmentation fault (a very LOW level error, usually an attempt to access a memory location that doesn't exist)

    So don't focus on your code, focus on versions (i.e. version of ruby required, or version of gems that gruff depends on, etc.)