Am Montag, 26. November 2007 23:45 mkajdas.mkprod wrote:

> I must be doing things differently than everyone else but every class that
> I create that contains a window does not release that window's memory. My
> fluid code looks something like this: 
> 
> class FL_EXPORT TDset_Dialog {} {
>   Function {TDset_Dialog()} {} {
>     Fl_Window TDsetWindow {
>       private xywh {390 417 500 190} type Double box ...
>     } {
> ..
>   }
>   Function {~TDset_Dialog()} {} {
>     code {;} {}
>   }
> }
> 
> When this class is created and deleted it has a leak. If I add:
> 
> TDsetWindow->clear(); delete TDsetWindow;

The clear() is not necessary, it is called by the TDsetWindow Dtor.


> to ~TDset_Dialog(), the leak is fixed.
> I assumed that when the class is deleted, it would cleanup the window and
> everything else, but it is not. I want to find out if I am doing something 
> wrong or my assumption is incorrect or I have to use Fl_Group first or ?


The question is concerned rather with a general C++ feature.
The above class TDset_Dialog is not itself a Widget or Window (derived from
Fl_Widget or Fl_Window) but a window wrapper class maintaining a dynamic
window object. Dynamic resources (objects created on heap via 'new' or
malloc()) must be deleted explicitely as in the following scetch

class A {
    Fl_Window* window;          // window pointer as member
 public:
    A()  {window = new Fl_Window(...);}
    ~A() {delete window;}       // explicit deletion 
};

(A==TDset_Dialog). To distinguish from it is the case where a class ´B´ has
a Window *object* as member:
 
class B {
    Fl_Window  window;          // window object as member
 public:  
    B() : window(...)  {}
    ~B() {}                     // nothing to do
};

Here the Dtor of the window object is automatically called when the B Dtor
is called. But the code generated by FLUID is of the first kind (otherwise
"->" and "delete" could not be applied to ´TDSetWindow´).


Yet a note on automatic widget deletion in FLTK:
The only widgets, FLTK deletes automatically are the children of a Group
(in the case the Group is deleted). Widgets without a parent, i.e. widgets
which are not child of a Group, will be not deleted automatically by
deletion of any other Group, they must be deleted explicitly. Top level
windows are such widgets without a parent, i.e. top level windows must be
deleted always explicitly -- so far at least a deletion is wanted.

Window wrapper classes as your TDset_Dialog should be used only for top
level windows, I think, otherwise the window could be deleted twice: by
its parent group and by the Dtor of the wrapper class. 

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

Reply via email to