多次调用ruby_init和ruby_finalize

时间:2018-09-24 12:53:56

标签: c++ ruby googletest

我目前正在尝试通过Google测试来测试一些Ruby(2.3)C API。

但是我的测试崩溃了,因为我创建了一个夹具,该夹具在设置时调用ruby_init,在拆卸时调用ruby_finalize

然后我在固定装置内创建了一个“单例”来解决该问题,但是我想知道是否还有另一种更简单的方法来实现此目的。

我希望所有测试都可以在正确/干净的VM中运行。

以下是一个重现该错误的简单示例:

#include <ruby.h>

int
main()
{
  ruby_init();
  ruby_finalize();

  ruby_init();
  ruby_finalize();
}

错误:

Program terminated with signal SIGABRT, Aborted.
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51  ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
[Current thread is 1 (Thread 0x7fa7f769a040 (LWP 6525))]
(gdb) bt
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1  0x00007fa7f6305c5d in __GI_abort () at abort.c:90
#2  0x00007fa7f634aaad in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7fa7f645957b "%s\n") at ../sysdeps/posix/libc_fatal.c:181
#3  0x00007fa7f6352298 in malloc_printerr (str=str@entry=0x7fa7f645ae88 "double free or corruption (!prev)") at malloc.c:5368
#4  0x00007fa7f635358c in _int_free (av=0x7fa7f668dc40 <main_arena>, p=0x7fa7f8023b50, have_lock=<optimized out>) at malloc.c:4288
#5  0x00007fa7f7092a8d in objspace_xfree (objspace=0x7fa7f7fb3d20, ptr=0x7fa7f8023b60, old_size=<optimized out>) at gc.c:7720
#6  0x00007fa7f70a514f in rb_io_fptr_finalize (fptr=0x7fa7f8023b60) at io.c:4338
#7  0x00007fa7f70897b1 in run_final (objspace=0x7fa7f7fb3d20, zombie=140359397235560) at gc.c:2691
#8  finalize_list (objspace=objspace@entry=0x7fa7f7fb3d20, zombie=140359397235560) at gc.c:2707
#9  0x00007fa7f7089870 in finalize_deferred (objspace=objspace@entry=0x7fa7f7fb3d20) at gc.c:2728
#10 0x00007fa7f7094280 in rb_objspace_call_finalizer (objspace=0x7fa7f7fb3d20) at gc.c:2794
#11 rb_gc_call_finalizer_at_exit () at gc.c:2780
#12 0x00007fa7f76b97e2 in main ()

1 个答案:

答案 0 :(得分:1)

您是否尝试过margin-top

看着source,看起来ruby_cleanup(0)只是确保Ruby代码完成(例如,调用ruby_finalize钩子,可能会杀死任何Ruby线程)。而at_exit调用finalize,然后清理VM本身。

编辑:

根据我的测试,此答案不起作用(尽管被接受)。您无法在同一过程中重新启动Ruby VM。我能想到的唯一解决方法是以分叉方式启动VM,以确保可以完全清除VM。