> > One fundamental rule I have learned in general C++ is that
> I must delete every object that I create with operator new.
> I also understand that somehow FLTK takes care of deleting
> FLTK objects created with operator new.
> >
> > Should I assume that also includes objects I create in my
> program that are independent of the FLTK hierarchy?  I don't
> think so, but please comment.  Thanks.
>
> No, it does not.  FLTK only handles deletion of child widgets and
> their data.

Also - regarding your "fundamental rule" - you need to think carefully
about the lifetime of your objects and the resources they consume. There
are several distinct cases:

1. If your object only uses memory resources, and once created the
object does not disappear until your program terminates, you never need
to delete it. The OS will release all memory resources consumed by your
objects and will do so much more efficiently and quickly than you can do
it by calling your destructors.

2. If your objects come and go during the lifetime of your program, then
you do need to destroy them. But, even here, there might be scope for
re-using an object rather than actually destroying it then later
creating a new one. This is usually more efficient than calling
delete/new.

3. If your object uses resources that your OS will not automatically
release on program termination, then you do need to call your
destructors. (Simple example: it creates tmp files or similar. Other
cases may be OS specific, e.g. how does you OS handle named pipes etc.)

I think case 1 is possibly the most common case, so in practice deleting
your objects may not actually be necessary. But you do need to
understand the lifetime and resource usage of your widgets to be
certain.
I have told this story before (so folk here are probably bored hearing
it) but we had a summer student a few years back who was tasked with
"housekeeping" on one of our in-house utilities. One of the housekeeping
actions he did was to add proper destructors for *all* the widgets, 'cos
that's what he was taught in school.
The app then took so long to exit that people started to complain...
When you quit, you don't expect to sit around for 5 minutes while the
app exits. He was made to take the destuctors back out, then check for
memory leaks. Program now quits "instantly" and there are no leaks. Job
done.







SELEX Sensors and Airborne Systems Limited
Registered Office: Sigma House, Christopher Martin Road, Basildon, Essex SS14 
3EL
A company registered in England & Wales.  Company no. 02426132
********************************************************************
This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender.
You should not copy it or use it for any purpose nor disclose or
distribute its contents to any other person.
********************************************************************

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

Reply via email to