On 24 Mar 2007, at 10:39, Alex wrote:
> I notice a lot of source code examples allocating widgets such as  
> Buttons with `new', but never `delete'ing them. Is this normal?  
> Does the library somehow clean things up, or should I be calling  
> delete on everything I allocate with new, as per normal C++.

This is pretty much a FAQ I think - and I'm pretty sure it is  
described in the docs... But anyway...

Fltk widgets that are created as part of, or are added to, fltk  
containers (e.g. Fl_Group and things that inherit from it, such as  
Fl_Window) will be automatically reaped when the container is  
destroyed, so no action is required.

The container widgets themselves are often not explicitly deleted in  
a lot of the worked examples, as they do not go out of scope until  
the application exits - at which point the underlying OS guarantees  
to reap the widgets as they are destroyed, so again no explicit  
delete is necessary.
This is a more controversial point, since an awful lot of people  
being taught C++ get taught that they *must* "delete" everything that  
they "new", and get bad marks on their coursework if they do not...  
But in this case, it is not necessary (and is in fact a performance  
hit, since calling the explicit destructor methods is a lot, lot,  
lot, slower than just letting the OS recover the resources - I have  
seen apps that took several minutes to quit doing this, when just  
allowing the app to exit and letting the OS do its thing happened  
pretty much instantly. Without leaks.)

> I don't want to be making a memory leaking application naively.

If you just use plain fltk widgets, all should be well. If you  
subclass a widget, you need to consider whether your subclass is  
taking memory or resources that should be explicitly released, memory  
is usually OK, other resources depend very much on circumstance...  
files or sockets for example might persist after your app quits...

At the end of the day - valgrind or equivalent is probably the best  
way to be sure...!

HTH,
-- 
Ian

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

Reply via email to