]> git.tdb.fi Git - libs/datafile.git/commitdiff
Restore the check to avoid deletion if an add call fails
authorMikko Rasa <tdb@tdb.fi>
Tue, 4 Dec 2012 10:28:24 +0000 (12:28 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 4 Dec 2012 21:58:55 +0000 (23:58 +0200)
It was lost to a careless rewrite in b1bc256.

source/collection.h

index 2243715b1d121d8fd64093f1a4b59b94e53e60e0..6e6900cece1b0c3428e666e918e2e901b6a4541f 100644 (file)
@@ -102,7 +102,17 @@ public:
                if(!item)
                        throw std::invalid_argument("Collection::add(item)");
 
-               insert_unique(items, name, RefPtr<typename RemoveConst<T>::Type>(item));
+               RefPtr<typename RemoveConst<T>::Type> ptr(item);
+               try
+               {
+                       insert_unique(items, name, ptr);
+               }
+               catch(...)
+               {
+                       // Avoid deleting the object
+                       ptr.release();
+                       throw;
+               }
        }
 
        /// Gets a typed object from the collection.
@@ -444,8 +454,8 @@ public:
                RefPtr<T> obj = new T;
                Collection::ItemLoader<T> ldr(*obj, coll);
                ldr.load(parser);
-               // Collection::add will delete the object if it fails
-               coll.add(name, obj.release());
+               coll.add(name, obj.get());
+               obj.release();
        }
 };