FLTK logo

Re: [fltk.general] Examples create a ton of dynamic visual 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: Examples create a ton of dynamic visual objects Greg Ercolano Sep 29, 2016  
 
On 09/29/16 13:11, Juan Dent wrote:
> Following the Tabs example, the main() function creates a bunch of GUI objects using new but never deletes them!! 

	The widgets are destroyed in the proper order when
	the program closes.

	FLTK widgets handle their own destruction on exit.
	This is so that the widgets destroy themselves in the proper order.

	The only time you yourself should destroy widgets is if you have
	an app that needs to remain running while it creates and destroys
	widgets.

	For instance, an app that has a main UI that has small memory use,
	but periodically opens a widget hierarchy that has a large footprint..
	and when the user closes that widget hierarchy, it should all go away,
	freeing up memory for the process to be able to use for other purposes.

	In such a case you can delete the top level widget, and that will trigger
	the entire widget hierarchy (children) to destroy themselves.

	Normally you don't need to do this though; when widgets aren't
	being used, or are used occasionally (like file browsers or dialogs),
	you leave them allocated and just show() or hide() them as needed.

> This is a bad approach!!
> Please enlighten me!

	This comes up a lot from folks who are fresh to programming
	and have learned about new and delete, and that all new's should
	be deleted, which is fine to prevent memory leaks within a program's
	execution, but doesn't matter if the items last the length of the
	program until it exits, which is the case with most gui elements.

	Understand that when a process exits, all memory is automatically freed.

	There's no such thing as a program leaking memory when it exits;
	all memory it allocated with new/malloc are freed by the OS.

	In GUI apps (such as tabs), there's no reason to delete anything
	because the program UI remains at all times until the program is
	closed, and at the point the process's memory associated with all
	the widgets is freed cleanly and in the proper order.

-- 
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.
For more options, visit https://groups.google.com/d/optout.

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'.