Re: 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;
[snip]

Glib::RefPtr is not for use with widgets. It is for use with classes that
do not allow you to use them without RefPtr. See the gtkmm book for more
detailed documentaiton. If you did use it then you would have to be a lot
more careful about whether or not it had a starting reference.

If what you actually want is for the child to be deleted automatically
when the window is deleted, then just use Gtk::manage(). Again, this is in
gtkmm book.
http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/ch03s02.html
and
http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/ch20.html

Do make sure you've read that Basics chapter. It will save you lots of time.

Murray Cumming
murrayc murrayc com
www.murrayc.com
www.openismus.com




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