>
> 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

These were very helpful suggestions.  You're right, the
tsk_img_open_sing() function is the right way to go for allocation, and
the corresponding tsk_img_close() was the proper deallocation function.
Looking into the lib's source it became clear that the intention was to
protect alloc and dealloc by wrapping them in open() and close()
methods, which makes sense for an I/O module like this.  The lesson I
take from this is to follow the source library's way of doing things,
rather than seek a generic approach.

I was wary of opening the image during the ruby allocate() function, as
that seemed to violate Ruby's separation of concerns: allocate() is for
reserving memory, while init() is the place for constructing the object
itself.

I used your suggestion to wrap the TSK_IMG_INFO in another struct and my
init() went like so:

  struct myHandle* ptr;
  Data_Get_Struct(self, struct myHandle, ptr);
  ptr->image = tsk_img_open_sing(filename, TSK_IMG_TYPE_DETECT, 0);

with myHandle prototyped as you illustrated.  Assigning the
tsk_img_open() function to DATA_PTR(self) worked OK, but it made it
difficult to access components of the TSK_IMG_INFO struct, which made it
impractical.

With the intermediate handle in place, I was able to access the lower
level pointer in other instance methods like so:

TSK_IMG_INFO *image = ptr->image;

Which seems a safer means of retrieval than touching DATA_PTR.

Deallocation had to be re-written as follows, to dereference the
TSK_IMG_INFO pointer:

static void deallocate(struct myHandle * ptr){
  TSK_IMG_INFO *image = ptr->image;
  tsk_img_close(image);
}

Thanks for the enlightening suggestions.

Matthew S.

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