> Michael Sweet wrote:
> > Albrecht Schlosser wrote:
> >> ...
> >> What happens? Note that img points to an Fl_Shared_Image object.
> >> The compiler can't detect this, can it? Will the Fl_Shared_Image
> >> be destroyed?
> >
> > Yes, it will, since the destructor of a class is always virtual.
> >
> >> Will ~Fl_Shared_Image() be called, and what should this do?
> >
> > The Fl_Shared_Image destructor will get called.  It *should* remove
> > the image from the shared image list.
> >
> >> Currently the Fl_Shared_Image would be removed from the image
> >> list only if release() would be called, but we'd have a problem
> >> anyway, since there might be other pointers to the shared image.
> >
> > Yup, and we'll just have to educate users about this - don't use
> > delete on shared images, don't mix delete with retain/release, etc.
>
> And that's the reason why I don't *like* the current implementation
> of Fl_Shared_Image. IMHO education is not the way to prevent
> obvious errors, especially in this case, where we would like to
> have polymorphism (in a way that we can use Fl_Image pointers
> everywhere), but then we must take care how to destroy the object.
> And this should preferably work without dynamic_cast ...
I think you mix 2 things:
Polymorphism has nothing to here with the deallocation problem.
This is the reference counting that makes the complexity, if any.
As the Fl_Image destructor is explicitly defined as virtual, it is not a 
problem of casting to the correct derived class before deleting: not only it is 
not necessary (because the correct destructors will be called whatever your 
object pointer class is derived from in the class tree) but you should not do 
it.

>
> I have some thoughts about a (hopefully compatible) re-implementation,
> and I think that I can write a short overview shortly. I'll do this
> in another thread though.
I don't think it would be necessary but I'm looking forward your overview.
>
> Albrecht
Fabien

_______________________________________________
fltk-dev mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-dev

Reply via email to