Matthew S. wrote in post #1035055:
> // alloc & dealloc
> static void deallocate(void * image){
>   xfree((TSK_IMG_INFO *)image);
> }
> static VALUE allocate(VALUE klass){
>   TSK_IMG_INFO *ptr;
>   return Data_Make_Struct(klass, TSK_IMG_INFO, 0, xfree, ptr);
> }
>
> // init a FirstClass (Image) object
> VALUE initialize(VALUE self){
>   char * filename;
>   filename = "/tmp/test.image";
>   TSK_IMG_INFO *image;  // declare inner struct
>   Data_Get_Struct(self, TSK_IMG_INFO, image); // fetch inner struct from
> self
>
>   // use function from libtsk3 to open disk image
>   image = tsk_img_open_sing(filename, TSK_IMG_TYPE_DETECT, 0);
>   fprintf(stdout, "image size: %d\tsector size:%d\n", (int)image->size,
> (int)image->sector_size);
>
>   Check_Type(self, T_DATA);
>
>   // done
>   fprintf(stdout, "DATA_PTR(self): %lu\n", (long)DATA_PTR(self));
>   DATA_PTR(self) = image;  // Not sure I should be doing this...
>   fprintf(stdout, "DATA_PTR(self): %lu\n", (long)DATA_PTR(self));
>
>   return self;
> }

Hi,

This is similar to the issue of pointer and address of pointer.

First, it seems the allocation is done through tsk_img_open_sing().  So
then in your allocate(), you actually want something like this:

static VALUE allocate(VALUE klass){
  TSK_IMG_INFO *ptr;
  ptr = tsk_img_open_sing( ... );
  return Data_Wrap_Struct(klass, 0, deallocate, ptr);
}

But are you sure that xfree() is the delete function corresponds to
tsk_img_open_sing()?

With this, the problem is that you cannot pass the filename (except
maybe
through some global variable), so you want to do it in the initialize().
Then instead you can just use DATA_PTR directly:

DATA_PTR(self) = tsk_img_open_sing(filename, TSK_IMG_TYPE_DETECT, 0);

If this seems getting too "unclean", then you can use an intermediate
handle:

struct myHandle
{
  TSK_IMG_INFO *image;
};

static VALUE allocate(VALUE klass){
  myHandle *ptr;
  return Data_Make_Struct(klass, myHandle, 0, deallocate, ptr);
} // but you have to modify deallocate()

VALUE initialize(VALUE self){
  ...
  myHandle* ptr;
  Data_Get_Struct(self, myHandle, ptr);
  ptr->image = tsk_img_open_sing(filename, TSK_IMG_TYPE_DETECT, 0);
  ...
}

Hope this helps.

Regards,

Bill

-- 
Posted via http://www.ruby-forum.com/.