Re: when we can delete a class derived from Gtk::Window or others
aitor <[email protected]> Thu, 17 Feb 2022 20:55:52 +0100
| Newsgroups | gmane.comp.gnome.gtkmm |
|---|---|
| Message-ID | <[email protected]> |
--===============8102026194092049903== Content-Type: multipart/alternative; boundary="------------3F18973E0AD926DD611BA093" Content-Language: en-US --------------3F18973E0AD926DD611BA093 Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit Hi Klaus, On 17/2/22 13:00, Klaus Rudolph<[email protected]> wrote: > I want to create some windows on demand with "new". If the user closes > the window, when I can call delete on that class derived from > Gtk::Dialog or Gtk::Window? <snip> > void on_hide() override > { > std::cout << "Hide event" << std::endl; > delete this; // is it safe to delete our self here????? > } I tested you example and it works whenever the user closes the windows in the reverse order from creation. But, what happens if the user closes the parent window first? In that case, the destructor of the child window won't be called and this isn't safe because you are using raw pointers instead of smart pointers. On the other hand, bear in mind that new windows can be created from both the parent and the child all over again. Therefore, I guess that you should use a weak pointer together with a shared one to keep track of the nodes in the tree. My2Cents, Aitor. --------------3F18973E0AD926DD611BA093 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: 7bit <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <pre>Hi Klaus, </pre> <div class="moz-cite-prefix"> <pre>On 17/2/22 13:00, Klaus Rudolph <a class="moz-txt-link-rfc2396E" href="mailto:[email protected]"><[email protected]></a> wrote:</pre> </div> <blockquote type="cite" cite="mid:[email protected]"> <pre class="moz-quote-pre" wrap="">I want to create some windows on demand with "new". If the user closes the window, when I can call delete on that class derived from Gtk::Dialog or Gtk::Window?</pre> </blockquote> <font size="-1"><snip></font><br> <blockquote type="cite" cite="mid:[email protected]"> <pre class="moz-quote-pre" wrap="">void on_hide() override { std::cout << "Hide event" << std::endl; delete this; // is it safe to delete our self here????? }</pre> </blockquote> <pre>I tested you example and it works whenever the user closes the windows in the reverse order from creation. But, what happens if the user closes the parent window first? In that case, the destructor of the child window won't be called and this isn't safe because you are using raw pointers instead of smart pointers.</pre> <pre>On the other hand, bear in mind that new windows can be created from both the parent and the child all over again. Therefore, I guess that you should use a weak pointer together with a shared one to keep track of the nodes in the tree. </pre> <pre>My2Cents,</pre> <pre>Aitor.</pre> <p><br> </p> <p><br> </p> </body> </html> --------------3F18973E0AD926DD611BA093-- --===============8102026194092049903== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ gtkmm-list mailing list [email protected] https://mail.gnome.org/mailman/listinfo/gtkmm-list --===============8102026194092049903==--