[gst-debugger] gst-debugger: fix some memory leaks



commit 37ab4dbe1127368cafd4b73727de07297e347209
Author: Marcin Kolny <marcin kolny gmail com>
Date:   Wed Oct 18 22:05:19 2017 +0100

    gst-debugger: fix some memory leaks

 src/gst-debugger/controller/tcp_client.cpp         |   16 +++++-
 .../graphviz-plugin/gvdevice_gstdebugger.c         |   35 +++++++++++---
 src/gst-debugger/main.cpp                          |    6 +--
 src/gst-debugger/main_window.cpp                   |    2 +-
 src/gst-debugger/pipeline-drawer/graph_module.cpp  |   48 ++++++++++----------
 src/gst-debugger/pipeline-drawer/graph_module.h    |    5 +--
 src/gst-debugger/ui_utils.cpp                      |    4 +-
 7 files changed, 71 insertions(+), 45 deletions(-)
---
diff --git a/src/gst-debugger/controller/tcp_client.cpp b/src/gst-debugger/controller/tcp_client.cpp
index 2852a5f..a24f378 100644
--- a/src/gst-debugger/controller/tcp_client.cpp
+++ b/src/gst-debugger/controller/tcp_client.cpp
@@ -63,7 +63,7 @@ void TcpClient::read_data()
                data.ParseFromArray(m_buff, size);
                if (m_buff != buffer)
                {
-                       delete m_buff;
+            delete [] m_buff;
                }
                signal_frame_received(data);
        }
@@ -92,6 +92,16 @@ void TcpClient::send_command(const GstDebugger::Command &cmd)
        if (!is_connected())
                throw Gio::Error(Gio::Error::FAILED, _("Client isn't connected to a server!"));
 
-       gst_debugger_protocol_write_header(connection->get_output_stream()->gobj(), cmd.ByteSize());
-       cmd.SerializeToFileDescriptor(connection->get_socket()->get_fd());
+    GError *err = gst_debugger_protocol_write_header(connection->get_output_stream()->gobj(), 
cmd.ByteSize());
+
+    if (err)
+    {
+        // TODO: emt error
+        g_print ("cannot send size of data: %s\n", err->message);
+        g_error_free (err);
+    }
+    else
+    {
+        cmd.SerializeToFileDescriptor(connection->get_socket()->get_fd());
+    }
 }
diff --git a/src/gst-debugger/graphviz-plugin/gvdevice_gstdebugger.c 
b/src/gst-debugger/graphviz-plugin/gvdevice_gstdebugger.c
index c3f231c..b39b4a7 100644
--- a/src/gst-debugger/graphviz-plugin/gvdevice_gstdebugger.c
+++ b/src/gst-debugger/graphviz-plugin/gvdevice_gstdebugger.c
@@ -12,9 +12,17 @@
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
 
-GtkWidget* drawing_area;
+static GtkWidget* drawing_area;
 GMainLoop *graphviz_gstdebugger_plugin_main_loop = NULL;
 
+struct DisplayRef
+{
+    Display *dpy;
+    int refcount;
+};
+
+static struct DisplayRef dpy_ref = { NULL, 0 };
+
 void graphviz_gstdebugger_set_drawing_area(GtkWidget *area)
 {
        drawing_area = area;
@@ -27,19 +35,22 @@ void graphviz_gstdebugger_set_main_loop(GMainLoop *loop)
 
 static void gstdebugger_initialize(GVJ_t *firstjob)
 {
-       Display *dpy;
        const char *display_name = NULL;
        int scr;
        gtk_init (NULL, NULL);
-       dpy = XOpenDisplay(display_name);
-       if (dpy == NULL) {
+    if (dpy_ref.dpy == NULL)
+    {
+        dpy_ref.dpy = XOpenDisplay(display_name);
+    }
+    if (dpy_ref.dpy == NULL) {
                fprintf(stderr, "Failed to open XLIB display: %s\n",
                                XDisplayName(NULL));
                return;
        }
-       scr = DefaultScreen(dpy);
-       firstjob->device_dpi.x = DisplayWidth(dpy, scr) * 25.4 / DisplayWidthMM(dpy, scr);
-       firstjob->device_dpi.y = DisplayHeight(dpy, scr) * 25.4 / DisplayHeightMM(dpy, scr);
+    dpy_ref.refcount++;
+    scr = DefaultScreen(dpy_ref.dpy);
+    firstjob->device_dpi.x = DisplayWidth(dpy_ref.dpy, scr) * 25.4 / DisplayWidthMM(dpy_ref.dpy, scr);
+    firstjob->device_dpi.y = DisplayHeight(dpy_ref.dpy, scr) * 25.4 / DisplayHeightMM(dpy_ref.dpy, scr);
        firstjob->device_sets_dpi = TRUE;
 }
 
@@ -54,6 +65,16 @@ static void gstdebugger_finalize(GVJ_t *firstjob)
        }
 
        g_main_loop_run (graphviz_gstdebugger_plugin_main_loop);
+
+    if (dpy_ref.refcount > 0)
+    {
+        dpy_ref.refcount--;
+        if (dpy_ref.refcount == 0 && dpy_ref.dpy != NULL)
+        {
+            XCloseDisplay(dpy_ref.dpy);
+            dpy_ref.dpy = NULL;
+        }
+    }
 }
 
 static gvdevice_features_t device_features_gstdebugger = {
diff --git a/src/gst-debugger/main.cpp b/src/gst-debugger/main.cpp
index b9d2ae4..666d9df 100644
--- a/src/gst-debugger/main.cpp
+++ b/src/gst-debugger/main.cpp
@@ -27,9 +27,5 @@ int main(int argc, char** argv)
        MainWindow* wnd_handler;
        builder->get_widget_derived("mainWindow", wnd_handler);
 
-       std::shared_ptr<Controller> controller(new Controller(wnd_handler));
-
-       controller->run(argc, argv);
-
-       return 0;
+    return std::make_shared<Controller>(wnd_handler)->run(argc, argv);
 }
diff --git a/src/gst-debugger/main_window.cpp b/src/gst-debugger/main_window.cpp
index d7c0f9d..2c64aaf 100644
--- a/src/gst-debugger/main_window.cpp
+++ b/src/gst-debugger/main_window.cpp
@@ -75,7 +75,7 @@ MainWindow::MainWindow(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>
        });
 
        signal_delete_event().connect([this](GdkEventAny*){
-               graph_module->free_graph();
+               // TODO graph_module->free_graph(); - do we need this?
                return false;
        });
 }
diff --git a/src/gst-debugger/pipeline-drawer/graph_module.cpp 
b/src/gst-debugger/pipeline-drawer/graph_module.cpp
index 433663c..b03861c 100644
--- a/src/gst-debugger/pipeline-drawer/graph_module.cpp
+++ b/src/gst-debugger/pipeline-drawer/graph_module.cpp
@@ -18,7 +18,7 @@
 
 static void ptr_free(std::shared_ptr<ElementModel>* ptr)
 {
-       delete ptr;
+    delete ptr;
 }
 
 extern gvplugin_library_t gvplugin_gstdebugger_LTX_library;
@@ -54,7 +54,7 @@ GraphModule::GraphModule(const Glib::RefPtr<Gtk::Builder>& builder)
 
 GraphModule::~GraphModule()
 {
-       g_main_loop_unref(graphviz_plugin_loop);
+    g_main_loop_unref(graphviz_plugin_loop);
 }
 
 void GraphModule::set_controller(const std::shared_ptr<Controller> &controller)
@@ -143,7 +143,7 @@ void GraphModule::update_model(std::shared_ptr<ElementModel> new_model)
 
 void GraphModule::update_selected_object()
 {
-       gui_emit("update-selected-object");
+    gui_emit("update-selected-object");
 }
 
 void GraphModule::update_selected_object_()
@@ -242,21 +242,21 @@ bool GraphModule::graphDrawingArea_draw_cb(const Cairo::RefPtr<Cairo::Context>&
        return false;
 }
 
-void GraphModule::free_graph()
+void GraphModule::free_graph(GVC_t *gvc, Agraph_t *g)
 {
-       if (gvc != nullptr)
-       {
-               gvFreeContext (gvc);
-               gvFinalize (gvc);
-
-               gvc = nullptr;
-       }
-       if (g != nullptr)
-       {
-               agclose (g);
-               g = nullptr;
-       }
-       g_main_loop_quit(graphviz_plugin_loop);
+    if (gvc != nullptr)
+    {
+        gvFreeLayout(gvc, g);
+        gvFinalize(gvc);
+        gvFreeContext(gvc);
+        gvc = nullptr;
+    }
+    if (g != nullptr)
+    {
+        agclose(g);
+        g = nullptr;
+    }
+    g_main_loop_quit(graphviz_plugin_loop);
 }
 
 void GraphModule::update_model_()
@@ -290,15 +290,15 @@ void GraphModule::update_model_()
                { 0, 0 }
        };
 
-       gvc = gvContextPlugins(lt_preloaded_symbols, 1);
-       g = agmemread (model_str.c_str());
-       gvLayout (gvc, g, "dot");
-       graph_drawing_area->hide();
-       gvRender (gvc, g, "gstdebugger", NULL);
-       free_graph();
+    GVC_t *gvc = gvContextPlugins(lt_preloaded_symbols, 1);
+    Agraph_t *g = agmemread (model_str.c_str());
+    gvLayout (gvc, g, "dot");
+    graph_drawing_area->hide();
+    gvRender (gvc, g, "gstdebugger", NULL);
+    free_graph(gvc, g);
 }
 
 void GraphModule::refreshGraphButton_clicked_cb()
 {
-       controller->send_request_entire_topology_command();
+    controller->send_request_entire_topology_command();
 }
diff --git a/src/gst-debugger/pipeline-drawer/graph_module.h b/src/gst-debugger/pipeline-drawer/graph_module.h
index 40a03c0..ae30167 100644
--- a/src/gst-debugger/pipeline-drawer/graph_module.h
+++ b/src/gst-debugger/pipeline-drawer/graph_module.h
@@ -23,9 +23,6 @@ protected:
 
        GstBinToDotConverter dot_converter;
 
-       Agraph_t *g = nullptr;
-       GVC_t * gvc = nullptr;
-
        Gtk::DrawingArea *graph_drawing_area;
        Gtk::Button *up_graph_button;
        Gtk::Entry *selected_element_entry;
@@ -54,7 +51,7 @@ public:
 
        void update_model_();
        void update_model(std::shared_ptr<ElementModel> new_model);
-       void free_graph();
+    void free_graph(GVC_t *gvc, Agraph_t *g);
 
        void set_controller(const std::shared_ptr<Controller> &controller) override;
 };
diff --git a/src/gst-debugger/ui_utils.cpp b/src/gst-debugger/ui_utils.cpp
index aacab46..2466486 100644
--- a/src/gst-debugger/ui_utils.cpp
+++ b/src/gst-debugger/ui_utils.cpp
@@ -93,7 +93,9 @@ void display_caps(const Glib::RefPtr<Gst::Caps> &caps,
                        Glib::ValueBase base;
 
                        structure.get_field(field_name, base);
-                       APPEND_SUB_ROW (field_name, gst_value_serialize(base.gobj()), row);
+            gchar* value = gst_value_serialize(base.gobj());
+            APPEND_SUB_ROW (field_name, value, row);
+            g_free(value);
                }
        }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]