> I've a couple of questions about creation, deletion
> and memory management of these classes. I browsed
> through the source codes, but I'm still confused.
>
> Let's say I create an Fl_RGB_Image from a simple array:
>
> uchar *img; //assume w, h, and d have right values
> Fl_RGB_Image *rgbimg = new Fl_RGB_Image(img, w, h, d);
>
> At this point how many copies of the original image
> are there in the memory? As I see in Fl_Image.cxx
> there is still one copy (pointed by img), but pointers
> internal to Fl_RGB_Image class are now pointing to that
> copy as well. Is this correct?
Correct. To be precise (if we try to distinguish meaning of
copy and original) nothing is copied. Only
Fl_Image and Fl_RGB_Image points to yours creation. On other hand if you
want copy, use copy() :)
> Second, if I want to free the memory associated with this
> image what would be the right thing to do? For instance,
> again as I see from the code, calling "delete rgbimage"
> will not free the memory in this case due to the following
> statement in Fl_Image.cxx:
>
> if (alloc_array) delete[] (uchar *)array;
> and since alloc_array was set to zero in the constructor
> called above.
You can use 'rgbimage->alloc_array=1' when creating rgbimage and
destructor will do it's job. Contrary, deleting is up to you.
> Then it seems that I should also call: delete[] img;
> Isn't it?
See above.
>
> I've another question of secondary importance.
> Why are there two pointers, namely data_ (in Fl_Image)
> and array (in Fl_RGB_Image), don't they point to the
> same thing always?
Yes.
> And by the way what exactly does the
> following declaration mean?
>
> const char * const *data_; //from Fl_Image.H
> Is it a constant pointer to a constant memory location?
Well since Fl_Image is abstract class, it should handle various image
types. For example xpm use matrix for data storage, but other formats
use linear array.
Here data_ says that real data it points to can't be changed (this
assures yours allocated data will not be modified) but it can be
instructed to point into something else.
Comments are owned by the poster. All other content is copyright 1998-2025 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.