BT_MOVE(): call bt_put(_dst)
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 1 Feb 2016 13:21:00 +0000 (08:21 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 19 Feb 2016 20:15:48 +0000 (15:15 -0500)
It makes no semantical sense to move a reference from a variable
to another without first putting the destination, if it has one.
Otherwise this would be a lost reference, unless it was moved
elsewhere or put manually using bt_put() previously, which is why
BT_MOVE() should always be used for move semantics and BT_PUT() is
preferred to bt_put() to avoid this situation.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
include/babeltrace/ref.h
tests/lib/test_bt_values.c

index f03f8c9b01f67c9494c8bbf6476a2a84f2e1159f..68f872cf926ab4ca49a8050ea30533412bc5a887 100644 (file)
  * then sets _src to NULL, effectively moving the ownership of an
  * object from one variable to the other.
  *
+ * Before assigning _src to _dst, it puts _dst. Therefore it is not safe to
+ * call this function with an uninitialized value of _dst.
+ *
  * @param obj Babeltrace object.
  */
 #define BT_MOVE(_dst, _src)    \
        do {                    \
+               bt_put(_dst);   \
                (_dst) = (_src);\
                (_src) = NULL;  \
        } while (0)
index 34fbc82cef448983be2d4dec9973739c605a42db..d10ae0cf31e9cfdf7401fd90a32400c1fc7d2640 100644 (file)
@@ -1112,7 +1112,7 @@ void test_macros(void)
 {
        struct bt_value *obj = bt_value_bool_create();
        struct bt_value *src;
-       struct bt_value *dst;
+       struct bt_value *dst = NULL;
 
        assert(obj);
        BT_PUT(obj);
This page took 0.0273 seconds and 4 git commands to generate.