From: "Iñaki Baz Castillo" Date: 2012-05-10T20:23:30+09:00 Subject: Re: How to use Data_Wrap_Struct to assign the DATA VALUE to an exsiting Ruby object? 2012/5/10 Iñaki Baz Castillo : > C land: > ---------- > > VALUE MyModule_init_udp_socket(VALUE self, VALUE rb_bind_ip, VALUE rb_bind_port) > { >  [...] >  struct my_udp_cdata* cdata = ALLOC(struct my_udp_cdata); > >  cdata->xxxx = xxxx; >  cdata->xxxx = xxxx; > >  DATA_PTR(self) = cdata; > >  [...] >  return self; > } > > > This still does NOT crash, but if after the C function I call to any > attribute reader of _sock_ or use sock.instance_variable_set(), then > it crashes. DATA_PTR is defined as follows: #define DATA_PTR(dta) (RDATA(dta)->data) #define RDATA(obj) (R_CAST(RData)(obj)) #define R_CAST(st) (struct st*) struct RData { struct RBasic basic; void (*dmark)(void*); void (*dfree)(void*); void *data; }; struct RBasic { VALUE flags; VALUE klass; }; So I think that using DATA_PTR(self) is not what I expected. IMHO it completely replaces the current C data of the Ruby object, while the only I want is to "attach" a C structure to a given Ruby object. Am I right? -- Iñaki Baz Castillo