Fix possible leaks in graph's current design
[babeltrace.git] / lib / graph / component.c
index 7b433ea9626441bd9203a186924cc6f21ba30b41..9ed599448880b647d4d122fd9c50c2d8bb297cde 100644 (file)
@@ -41,6 +41,7 @@
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/compiler-internal.h>
 #include <babeltrace/ref.h>
+#include <babeltrace/types.h>
 #include <stdint.h>
 
 static
@@ -77,6 +78,16 @@ void bt_component_destroy(struct bt_object *obj)
                return;
        }
 
+       /*
+        * The component's reference count is 0 if we're here. Increment
+        * it to avoid a double-destroy (possibly infinitely recursive).
+        * This could happen for example if the component's finalization
+        * function does bt_get() (or anything that causes bt_get() to
+        * be called) on itself (ref. count goes from 0 to 1), and then
+        * bt_put(): the reference count would go from 1 to 0 again and
+        * this function would be called again.
+        */
+       obj->ref_count.count++;
        component = container_of(obj, struct bt_component, base);
 
        /* Call destroy listeners in reverse registration order */
@@ -209,7 +220,6 @@ struct bt_component *bt_component_create_with_init_method_data(
        int ret;
        struct bt_component *component = NULL;
        enum bt_component_class_type type;
-       struct bt_port *default_port = NULL;
 
        bt_get(params);
 
@@ -273,46 +283,20 @@ struct bt_component *bt_component_create_with_init_method_data(
                goto end;
        }
 
-       if (type == BT_COMPONENT_CLASS_TYPE_SOURCE ||
-                       type == BT_COMPONENT_CLASS_TYPE_FILTER) {
-               default_port = bt_component_add_port(component,
-                       component->output_ports, BT_PORT_TYPE_OUTPUT,
-                       DEFAULT_OUTPUT_PORT_NAME, NULL);
-               if (!default_port) {
-                       BT_PUT(component);
-                       goto end;
-               }
-
-               BT_PUT(default_port);
-       }
-
-       if (type == BT_COMPONENT_CLASS_TYPE_FILTER ||
-                       type == BT_COMPONENT_CLASS_TYPE_SINK) {
-               default_port = bt_component_add_port(component,
-                       component->input_ports, BT_PORT_TYPE_INPUT,
-                       DEFAULT_INPUT_PORT_NAME, NULL);
-               if (!default_port) {
-                       BT_PUT(component);
-                       goto end;
-               }
-
-               BT_PUT(default_port);
-       }
-
-       component->initializing = true;
+       component->initializing = BT_TRUE;
 
        if (component_class->methods.init) {
                ret = component_class->methods.init(
                        bt_private_component_from_component(component), params,
                        init_method_data);
-               component->initializing = false;
+               component->initializing = BT_FALSE;
                if (ret != BT_COMPONENT_STATUS_OK) {
                        BT_PUT(component);
                        goto end;
                }
        }
 
-       component->initializing = false;
+       component->initializing = BT_FALSE;
        ret = component_validation_funcs[type](component);
        if (ret != BT_COMPONENT_STATUS_OK) {
                BT_PUT(component);
@@ -322,7 +306,6 @@ struct bt_component *bt_component_create_with_init_method_data(
        bt_component_class_freeze(component->class);
 end:
        bt_put(params);
-       bt_put(default_port);
        return component;
 }
 
This page took 0.042733 seconds and 4 git commands to generate.