[ruby-list:50214] 拡張ライブラリでのSEGV
From:
Hidetoshi Saito <hidetoshi.saito@...>
Date:
2015-08-09 02:02:53 UTC
List:
ruby-list #50214
斎藤ともうします。
拡張ライブラリを作成しています。
SEGVで落ちるのですが、原因がわかりません。
シンプルにするために以下のような実験ソースを書いています。
//= libdebug.c ===================================================
#include <ruby.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct debugobj{
VALUE ary;
};
static void clsdebug_mark(struct debugobj *ptr)
{
rb_gc_mark(ptr->ary);
}
static VALUE clsdebug_alloc(VALUE klass)
{
struct debugobj *ptr = ALLOC(struct debugobj);
return Data_Wrap_Struct(klass, clsdebug_mark, -1, ptr);
}
static VALUE
rb_clsdebug_init(VALUE self)
{
char *p_str;
char str[]="abcdefghijklmns";
int i,len;
struct debugobj *ptr;
Data_Get_Struct(self, struct debugobj, ptr);
p_str=str;
len = strlen(p_str);
ptr->ary = rb_ary_new();
for(i=0;i<len;i++)
{
volatile VALUE v = rb_str_new(p_str+i,1);
rb_ary_push(ptr->ary,v);
}
return Qnil;
}
static VALUE
rb_clsdebug_ary(VALUE self)
{
struct debugobj *ptr;
Data_Get_Struct(self, struct debugobj, ptr);
volatile VALUE v = ptr->ary;
return v;
}
void Init_libdebug(void) {
VALUE libDebug;
VALUE clsdebug;
libDebug = rb_define_module("LibDebug");
clsdebug = rb_define_class_under(libDebug, "Clsdebug", rb_cObject);
rb_define_alloc_func(clsdebug, clsdebug_alloc);
rb_define_method(clsdebug, "initialize",
RUBY_METHOD_FUNC(rb_clsdebug_init), 0);
rb_define_method(clsdebug, "to_a", RUBY_METHOD_FUNC(rb_clsdebug_ary), 0);
}
//============================================================
extconf.rb に$CFLAGS="-g -O2"の指定をして、make
その後 以下のように使用したところ、SEGVで落ちる時と落ちないときがあります。
#= use.rb ======================================================
require_relative "./libdebug.so"
include LibDebug
num = 100_000
num.times do |i|
a=Clsdebug.new
p i, a.to_a if i % 1000 == 0
end
puts "done."
#=============================================================
gdbでみると gc_markが optimized outで落ちているのがわかるのですが、
解決方法がわかりません。このCのソースの問題点をご教示いただけるとたすかります。
よろしくおねがいします
===============================
斎藤 秀俊 Hidetoshi Saito
email : hidetoshi.saito@gmail.com
===============================