From: Erik Lindblad Date: 2009-09-14T17:15:19+09:00 Subject: Re: Using Data_Make_Struct for incomplete type On 13 Sep, 19:13, Joel VanderWerf wrote: > Erik Lindblad wrote: > > I have read up a bit on the problem and it is like I suspected. The > > struct contains arrays of undeclared size and naturally the compiler > > cannot guess the size. For now I have worked around it bu transferring > > the struct data to VALUE instance variable, but that solution is not > > as pretty as the option of wrapping the full struct. > > One more suggestion (maybe the same as what you just said): since the > struct seems to be variable length, what about wrapping a pointer to the > struct instead of the struct itself? In other words, the struct you wrap > is of this type > > typedef struct { >    struct SomeOpaqueStruct *s; > > } MyStruct; > > where SomeOpaqueStruct is from the library. It's a bit less efficient, > because of two dereferences (ruby object -> MyStruct -> s) and the extra > allocation, but that's probably relatively small. > > -- >        vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407 Hmmm, getting very close now. The idea to wrap SomeOpaqueStruct worked like a charm. Now I can allocate, initialize and retreive the DATA through this wrapped construction. So many thanks for that one. However there is one problem left. All the stuff above is only possible within the wrapper C code. Once the library call exits and I am back in Ruby land, the wrapped struct (SomeOpaqueStruct in your example) is freed and I get a segmentation fault. I realize this is because of how the wrapped library works but do you know of any way to prevent this or how to copy the struct, thereby saving it for the afterworld? Regards Erik