Subject:
[ruby-ffi] SystemStackError of callback
From:
tlayboy
Date:
7/1/10 9:40 AM
To:
ruby-ffi

Hi all,

I am in troble with SystemStackError of callback.
Does anyone know why this error is occured?

My code is like this.

############################
C library
############################
typedef void (*callback)(int, void *);
pthread_t thread;
struct cb_info info;

struct cb_info {
	callback cb;
	int data;
	void *p;
};

void *call(void *data)
{
	struct cb_info *info = (struct cb_info *)data;
	info->cb(info->data, info->p);
}

void async_call(callback cb, unsigned int data, void *p)
{
	int ret;

	info.cb = rcb;
	info.data = data;
	info.p = p;

	if ((ret = pthread_create(&thread, NULL, call, (void *)&info)) != 0)
{
		perror("ptherad_create");
		exit(EXIT_FAILURE);
	}
}

############################
Ruby Code
############################
p = FFI::MemoryPointer.new(:int)
p.write_int(2)

async_call(1, p) do |a, b|
	100.times do |d|
		puts a, b.read_int
	end
end

############################
Output
############################
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
2
/usr/lib/ruby/1.8/pp.rb:133:in `pp': stack level too deep
(SystemStackError)
	from /usr/lib/ruby/1.8/pp.rb:77:in `pp'
	from /usr/lib/ruby/1.8/pp.rb:119:in `guard_inspect_key'
	from /usr/lib/ruby/1.8/pp.rb:77:in `pp'
	from /usr/lib/ruby/1.8/pp.rb:60:in `pp'
	from /usr/lib/ruby/1.8/pp.rb:59:in `each'
	from /usr/lib/ruby/1.8/pp.rb:59:in `pp'
	from libcb.rb:35
	from (eval):6:in `call'
	from (eval):6:in `async_call'
	from libcb.rb:34
	from libcb.rb:33:in `times'
	from libcb.rb:33

thank you,