S
Simon Strandgaard
I were experimenting with Init_stack, when I discovered a flaw in my mind.
I thought that: local variables on stack would be marked in the
mark-phase and therefore survive the sweep-phase.
But my instance is being destroyed.. why ?
Can someone enligthen me ?
void Dummy_Free(void *p) {
printf("free\n");
delete [] ((int*)p);
}
VALUE Dummy_Alloc(VALUE self) {
printf("alloc\n");
int *p = new int[1];
return Data_Wrap_Struct(self, 0, Dummy_Free, p);
}
VALUE Dummy_Initialize(VALUE self) {
int *p;
printf("initialize\n");
Data_Get_Struct(self, int, p);
return self;
}
void Init_dummy() {
VALUE h = rb_define_class("Dummy", rb_cObject);
rb_define_alloc_func(h, Dummy_Alloc);
typedef VALUE (*HOOK)(...);
rb_define_method(h, "initialize", (HOOK)(&Dummy_Initialize), 0);
}
VALUE tst_new_dummy() {
return rb_class_new_instance(0, 0, rb_path2class("Dummy"));
}
void test() {
VALUE instance = tst_new_dummy();
printf("1\n");
rb_p(instance);
// the instance SHOULD survive this step (1_2)
rb_gc();
printf("2\n");
rb_p(instance);
// the instance should NOT survive this step (2_3)
instance = Qnil;
rb_gc();
printf("3\n");
rb_p(instance);
}
int main(int argc, char **argv) {
ruby_init();
Init_dummy();
test();
return 0;
}
initialize
1
#<Dummy:0x8103b38>
free
2
ruby: [BUG] Segmentation fault
ruby 1.8.0 (2003-06-23) [i386-freebsd5.0]
Abort (core dumped)
As you can see the 'free' is occuring between '1' and '2'.
I thought 'free' was suppose to happen between '2' and '3' ?
Expecting output should look like this:
alloc
initialize
1
#<Dummy:0x8103b38>
2
#<Dummy:0x8103b38>
free
3
ruby: [BUG] Segmentation fault
ruby 1.8.0 (2003-06-23) [i386-freebsd5.0]
Abort (core dumped)
I thought that: local variables on stack would be marked in the
mark-phase and therefore survive the sweep-phase.
But my instance is being destroyed.. why ?
Can someone enligthen me ?
#include <ruby.h>expand -t4 rb_gc1.c
void Dummy_Free(void *p) {
printf("free\n");
delete [] ((int*)p);
}
VALUE Dummy_Alloc(VALUE self) {
printf("alloc\n");
int *p = new int[1];
return Data_Wrap_Struct(self, 0, Dummy_Free, p);
}
VALUE Dummy_Initialize(VALUE self) {
int *p;
printf("initialize\n");
Data_Get_Struct(self, int, p);
return self;
}
void Init_dummy() {
VALUE h = rb_define_class("Dummy", rb_cObject);
rb_define_alloc_func(h, Dummy_Alloc);
typedef VALUE (*HOOK)(...);
rb_define_method(h, "initialize", (HOOK)(&Dummy_Initialize), 0);
}
VALUE tst_new_dummy() {
return rb_class_new_instance(0, 0, rb_path2class("Dummy"));
}
void test() {
VALUE instance = tst_new_dummy();
printf("1\n");
rb_p(instance);
// the instance SHOULD survive this step (1_2)
rb_gc();
printf("2\n");
rb_p(instance);
// the instance should NOT survive this step (2_3)
instance = Qnil;
rb_gc();
printf("3\n");
rb_p(instance);
}
int main(int argc, char **argv) {
ruby_init();
Init_dummy();
test();
return 0;
}
alloc./a.out
initialize
1
#<Dummy:0x8103b38>
free
2
ruby: [BUG] Segmentation fault
ruby 1.8.0 (2003-06-23) [i386-freebsd5.0]
Abort (core dumped)
As you can see the 'free' is occuring between '1' and '2'.
I thought 'free' was suppose to happen between '2' and '3' ?
Expecting output should look like this:
alloc
initialize
1
#<Dummy:0x8103b38>
2
#<Dummy:0x8103b38>
free
3
ruby: [BUG] Segmentation fault
ruby 1.8.0 (2003-06-23) [i386-freebsd5.0]
Abort (core dumped)