FLTK logo

[fltk.general] Re: Is FLTK performing automatic memory de-allocation of dynamically created objects?

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.general  ]
 
Previous Message ]New Message | Reply ]Next Message ]

Re: Is FLTK performing automatic memory de-allocation of dynamically created objects? Pierre Rouleau Sep 26, 2022  
 

On Monday, September 26, 2022 at 3:57:04 AM UTC-4 Ian MacArthur wrote:
On Monday, 26 September 2022 at 02:14:08 UTC+1 Pierre wrote:

For FLTK 1.3.8,
I'v seen documentation and discussion show how easy it is to dynamically create widgets as in:

       Fl_Button *fluidButton = new Fl_Button(25, 80, 70, 50, "button2");

but did not see much code where those dynamically allocated objects are freed (deleted) in destructors.

I would assume that the objects allocated by ``new`` statements would have to be freed by corresponding ``delete`` statements in destructors.  I can understand that it's not that important in the main() function of a program since once it terminates all process memory will be reclaimed by the C++ library code and process termination but for other classes that might be located else where and could be created during the execution a long-running program I would think that not freeing the widgets is just bad practice and memory leaks.

Am I missing something?  

The key point is that the fltk container widgets (typically groups or windows, or derivatives thereof) will reap all of their children when they are themselves destroyed.
How does FLTK class that has a straight member pointer to a widget know to delete that widget at destruction time?  Does FLTK override the operator new?

 
So if a window is deleted or goes out of scope, it will also remove all of its children. (Though if you want a widget to persist after its parent container is removed you can explicitly remove the child widget from the parent before disposing of the parent, of course...) 
This is in the docs somewhere - I know I've read it!
(A rider to this is that you sometimes need to be aware NOT to delete widgets that have already been deleted by their parent...)
 
And it becomes important to understand how their parent delete them.  Which is what I am after.   I don't see how the parent could delete a widget unless there is a tie somewhere between the parent and its widget.  


Another point is that it is often, at least with fltk, cheaper to hang onto "temporary" widgets you have created, to use them again later, rather than instantiating them and deleting them all the time as you go along, since many "temporary" widgets actually get used many times during the lifetime of the program, and the retained footprint of a hidden widget in fltk is generally pretty small.

So, it turns out that explicitly deleting the widgets may not be needed, in many cases - there will always be cases where a widget really is transient and used only once, and in that case deleting it explicitly will reduce memory usage and is correct, but there are a lot of cases where just hanging on to the widget and using it again later is faster and cheaper overall.
Or used only for a given situation that may repeat.  If the code does not pre-allocate the widget *once* but allocates it every time the processing needs to be done then you'd have a leak based on the use of the program, no?

--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkgeneral/5e0fe550-7d39-4283-b3fd-fdbf498d44bcn%40googlegroups.com.
Direct Link to Message ]
 
     
Previous Message ]New Message | Reply ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.