调试Ruby on Mac OS X中的malloc错误

调试Ruby on Mac OS X中的malloc错误,ruby,macos,debugging,malloc,Ruby,Macos,Debugging,Malloc,我正在尝试调试运行一些Ruby脚本时出现的以下错误: ruby(47333,0x7fff72aee960) malloc: *** error for object 0x7f98b6a6e3f0: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug 知道我如何设置这样的断点并进行调试吗?我想知道这是由Ruby本身还是由一些扩展引起的 我正在使用Mac OS X 10.

我正在尝试调试运行一些Ruby脚本时出现的以下错误:

ruby(47333,0x7fff72aee960) malloc: *** error for object 0x7f98b6a6e3f0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
知道我如何设置这样的断点并进行调试吗?我想知道这是由Ruby本身还是由一些扩展引起的


我正在使用Mac OS X 10.7.3(Lion)和
ruby 1.8.7(2010-01-10 patchlevel 249)[universal-darwin11.0]
如果您使用的是ruby 1.9.2+,请安装gem调试器(
gem安装调试器
)。有两种调试方法:直接包括
调试器
gem或使用
redbug
二进制文件。让我们假设我们有一个玩具脚本,我们想知道为什么调用
foo()
(假设它是一个外部库)后,
$blah
是4

方法1:包括
调试器

这是在代码中手动设置断点:

require 'debugger'

$blah = 3

def foo
  $blah += 1
end

def bar
  $blah += 4
end

foo()
debugger() # opens rdb
bar()

puts $blah
以ruby debug.rb的身份运行此程序。这会将您启动到ruby调试控制台:

% 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
方法2:运行
rdebug
下面是我们的示例脚本,
debug.rb

$blah = 3

def foo
  $blah += 1
end

def bar
  $blah += 4
end

foo()
bar()

puts $blah
从shell中执行
rdebug debug.rb
。下面是一个示例会话:

% 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
关键命令是
断开行号
显示变量
。希望有帮助

资源

以下是我看到OP信息时解决问题的方法:

我收到这条消息是因为我弄乱了路径,试图让
rvm
gem
安装一些东西,我真的弄乱了我的权限。然后我收到了OP报告的同样信息。对我来说,唯一能做的就是转到Mac OS X的
磁盘实用程序
,在左窗格中选择我的卷(Macintosh HD),然后单击
修复磁盘权限


完成后,我能够成功地打开并启动一个新的终端窗口。

这意味着使用
gdb
或其他一些C调试器设置断点。使用启用了调试功能的Ruby和扩展(即使用
-g
编译)会很有帮助。@muistooshort如果您能够深入了解如何实现这一点的步骤,或者提供一个链接,我想我可能会把这个问题交给您。问题是babonk需要在C函数中设置一个断点(
malloc\u error\u break
)这样他们就可以回溯到解释器或基于C的扩展中,以确定是谁释放了指针两次。@muistooshort:babonk正在运行一个Ruby脚本,所以我假设这个bug来自使用C扩展的外部库。听起来好像在库调用之前设置一些断点比调试某人的C扩展更有用。但是,如果需要,将演示如何执行此操作。