At present hiding destructor of Fl_Shared image is futile because user can 
always do

  delete (Fl_Image *) my_shared_image;

Also the user (as in my case) can have a placeholder to an image pointer which 
can point to both "normal" and shared image   - so we do not know how to 
destroy it.
I wonder if it would make sense to override operator delete to behave like 
release() method:

  void Fl_Shared_Image::operator delete(void *p){

    // At this point the destructors were already called.
    // If refcount_ is <=0, internal data were already destroyed in the 
destructor
    // and we have to free the memory, otherwise we do nothing!
    // The base destructor Fl_Image::~Fl_Image() does nothing too  - oh joy!

    if(((Fl_Shared_Image *)p)->refcount()<=0)
      ::delete(p); //freeing the memory by calling global operator delete on 
(void *)
  }

We have to modify the destructor as

  Fl_Shared_Image::~Fl_Shared_Image() {
    refcount_--;
    if(refcount<=0){
      if (name_) delete[] (char *)name_;
      if (alloc_image_) delete image_;
    }
  }

which can be declared public now.

According to C++ specification you can not declare operator delete as 
virtual(it is a static function after all) but it behaves as "virtual" (that is 
the proper
version is resolved at runtime) if the the class itself is polymorphic (which 
is our case, destructor is of course virtual) - so this should be ok.

Any thoughts?

Roman


_______________________________________________
fltk-dev mailing list
fltk-dev@easysw.com
http://lists.easysw.com/mailman/listinfo/fltk-dev

Reply via email to