Hi Ben,

Please see my responses below:

On Mon, Aug 22, 2011 at 3:01 PM, Ben Stott <[email protected]> wrote:

> > > > On Mon, Aug 22, 2011 at 1:36 PM, asif saeed
> > > > > I read the following tutorial yesterday - it creates class
> > > > members on stack
> > > > > - that is, it does not make them pointers and allocate them
> > > > using new():
> > > > >
> > > > > http://www.fltk.org/doc-2.0/html/example3.html
> > > > >
> > > > > I have also gone through examples that create members on
> > > > heap - that is,
> > > > > the members are pointers - not values/objects. I have read
> > > > in docs that
> > > > > Fl_Group automatically deletes the children, that you add,
> > > > upon exit. What
> > > > > is the difference between creating members on stack and on
> > > > heap? Don't you
> > > > > think creating them on stack will automatically release
> > > > them that could
> > > > > cause Fl_Group to release non-existing memory?
> > >
> > > Why do you think that would happen?
> >
> > The docs say that the groups automatically add whatever children get
> created
> > after them - they automatically issue a begin or something like that -
> > apparently each widget adds itself to the group in its constructor. I
> read
> > that while reading about creating members on heap using new(). I also
> read
> > that the groups automatically delete the children. Now if you create
> > children on stack then they will first be released via stack pop and then
> > the group will delete them - and here the group will delete something
> that
> > will have already been deallocated through stack unwinding.
> >
>
> So, two things:
> 1) You've posted a link to fltk2.0 documentation -- and judging by some of
> your comments in a few threads, seem to be referencing 2.0 classes -- but
> are talking about Fl_Group and the 1.3 counterparts.


Yes, you're right. I posted an fltk-2.0 link but the example applies to 1.3
as well.


The really cool thing about C++ is that you can't call operator delete on
> any variable that's not dynamically allocated.


   {
      int i;
      delete &i; //this works but blows up big-time, of course. But compiler
doesn't check it at compile time.
   }


> You also don't have your deallocation ordering strictly correct. The stack
> unwind will always be the last-constructed order - so if you declare a
> Fl_Group above a Fl_Widget, the Fl_Widget will be destructed *before* the
> Fl_Group.
>

Ya, I know - I call it DMB (DUMB) destruction - (D)erived classes, (M)embers
and (B)ase. I probably mis-typed something in a hurry.

Hopefully this answers your question. The group will never delete anything
> that has been deallocated via the stack, and vice versa.


That's exactly what I wanted to know. But the question remans: when should I
create member widgets as pointers and when should I create them as direct
object members (non pointers, that is)?

Ben
>

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

Reply via email to