reference pointer in the custom container (was: Re: preferred way to destroy a window)



Murray Cumming wrote:
> On Thu, 2006-08-03 at 22:01 +0300, Paul Pogonyshev wrote:
> > Hi,
> > 
> > Maybe it is a stupid question, but I was unable to find the answer.
> > What is the preferred way to destroy a window in Gtkmm?
> 
> delete, as for other C++ objects.
> 
> [...]
> 
> >   In pure C
> > I'd call gtk_widget_destroy(), but what do I do in C++?  (Calling
> > `delete this' leads to a segmenation fault, but maybe it's my error,
> > not sure...)
> 
> "delete this" should also work, as for other C++ objects, though many
> people prefer to avoid it, because you need to be very careful that the
> window is not being used elsewhere.

My windows are supposed to be `self-managing', i.e. `delete this' is OK.
In any case, `delete bla_bla_bla' is what I was looking for, thanks.

I have narrowed the problem down.  Apparently, `delete this' as such is
not a problem, it is a custom container with an additional refererence
to one of its children.  Please see the example below.  Can it be fixed
with the referencing pointer kept, or should I just forget about the
pointer and delete it?

(I prefer GTK+ reference-counted way of managing widgets.  I'd hate to
keep pointer to and delete all the separators, boxes and all other stuff
that means nothing special to the application.  With reference counting
they are deleted automatically.)

Paul


#include <glibmm/refptr.h>
#include <gtkmm/frame.h>
#include <gtkmm/main.h>
#include <gtkmm/label.h>
#include <gtkmm/window.h>


namespace
{

  class Custom_frame : public Gtk::Frame
  {
    // A custom referencing pointer causes segmentation fault.
# if 1
    Glib::RefPtr <Gtk::Widget>  _child;
# else
    Gtk::Widget*		_child;
# endif


  public:

    Custom_frame (Gtk::Widget& child)
      : Gtk::Frame (),
	_child	   (&child)
    {
      add (child);
    }
  };

}


int
main (int argc, char** argv)
{
  Gtk::Main  main (argc, argv);

  Gtk::Window  window;

  window.add (*manage (new Custom_frame (*manage (new Gtk::Label ("test")))));
  window.show_all ();

  main.run (window);

  return 0;
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]