]> git.tdb.fi Git - libs/gltk.git/commitdiff
Restore parent to 0 if an exception occurs while setting things up
authorMikko Rasa <tdb@tdb.fi>
Wed, 29 May 2013 20:17:00 +0000 (23:17 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 29 May 2013 20:19:53 +0000 (23:19 +0300)
This prevents an inconsistent state from forming if something goes wrong.

source/widget.cpp

index 9dbd9da5ba2df7f60f44d824a8a7989d11d917dd..bbd2e8877af608b1255d21eb10d9b23d2506fba4 100644 (file)
@@ -70,10 +70,20 @@ void Widget::set_parent(Container *p)
                throw hierarchy_error("widget already parented");
        else if(p==parent)
                return;
-       parent = p;
 
-       on_reparent();
-       update_style();
+       try
+       {
+               parent = p;
+
+               on_reparent();
+               update_style();
+       }
+       catch(...)
+       {
+               // The container has not yet added the widget as its child
+               parent = 0;
+               throw;
+       }
 }
 
 void Widget::set_style(const string &s)