Debugging malloc errors in Ruby on Mac OS X

I am trying to debug errors like the following that I get when running some Ruby scripts:

ruby(47333,0x7fff72aee960) malloc: *** error for object 0x7f98b6a6e3f0: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug 

Any idea how I can set such a breakpoint and debug it? I want to see if this is caused by Ruby itself or some extension.

I am using Mac OS X 10.7.3 (Lion) and ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0] .

+7
source share
2 answers

If you are on Ruby 1.9.2+, install the gem install debugger . There are two ways to debug: directly enable the debugger gem or use the redbug binary code. Suppose we have a toy script and want to know why $blah is 4 after calling foo() (pretend it is an external library).

Method 1: enable debugger

This sets a breakpoint manually in your code:

 require 'debugger' $blah = 3 def foo $blah += 1 end def bar $blah += 4 end foo() debugger() # opens rdb bar() puts $blah 

Run this as ruby debug.rb . This will launch you in the ruby-debug console:

 % ruby debug.rb debug.rb:15 bar() (rdb:1) list [10, 19] in debug.rb 10 $blah += 4 11 end 12 13 foo() 14 debugger() => 15 bar() 16 17 puts $blah (rdb:1) display $blah 1: $blah = 4 

Method 2: Run rdebug

Here is an example of our sample script, debug.rb :

 $blah = 3 def foo $blah += 1 end def bar $blah += 4 end foo() bar() puts $blah 

From the shell, run rdebug debug.rb . Here is an example session:

 % rdebug debug.rb (rdb:1) list 1,20 [1, 20] in /mnt/hgfs/src/stackoverflow/debug.rb => 1 $blah = 3 2 3 def foo 4 $blah += 1 5 end 6 7 def bar 8 $blah += 4 9 end 10 11 foo() 12 bar() 13 14 puts $blah (rdb:1) break 12 Breakpoint 1 file /mnt/hgfs/src/stackoverflow/debug.rb, line 12 (rdb:1) display $blah 1: $blah = (rdb:1) continue Breakpoint 1 at /mnt/hgfs/src/stackoverflow/debug.rb:12 1: $blah = 4 /mnt/hgfs/src/stackoverflow/debug.rb:12 bar() (rdb:1) display $blah 2: $blah = 4 

The key commands are break LINE-NUMBER and display VARIABLE . Hope this helps!

Resources

+2
source

This is what solved my problem when I saw the OP message:

I got a message because I messed up the paths while trying to install rvm or gem to install something, and I was able to really resolve my rights. Then I got the same message that the OP is reporting. For me, the only thing that was done was to go to Mac OS X Disk Utility , select my volume in the left pane (Macintosh HD), and then click Repair Disk Permissions .

After that, I was able to successfully open and launch a new terminal window.

+2
source

All Articles