[goffice] xml: leak fixes.



commit 115eb016e933f81c500722c983a88bb8752c9cef
Author: Morten Welinder <terra gnome org>
Date:   Tue Feb 17 15:28:47 2015 -0500

    xml: leak fixes.
    
    This makes sure we get rid of the xml parsers we build -- as opposed
    to holding on to a static reference.

 goffice/app/go-doc.c               |    4 +++-
 goffice/component/go-component.c   |    4 +++-
 goffice/goffice.c                  |    1 +
 goffice/graph/gog-axis-color-map.c |    8 ++++++--
 goffice/graph/gog-object-xml.c     |    8 ++++++--
 goffice/graph/gog-theme.c          |    8 ++++++--
 goffice/utils/go-libxml-extras.c   |   22 ++++++++++++++++++++++
 goffice/utils/go-libxml-extras.h   |    4 ++++
 goffice/utils/go-style.c           |    4 +++-
 9 files changed, 54 insertions(+), 9 deletions(-)
---
diff --git a/goffice/app/go-doc.c b/goffice/app/go-doc.c
index 7281dfd..043cd36 100644
--- a/goffice/app/go-doc.c
+++ b/goffice/app/go-doc.c
@@ -641,8 +641,10 @@ go_doc_read (GODoc *doc, GsfXMLIn *xin, xmlChar const **attrs)
                GSF_XML_IN_NODE_END
        };
        static GsfXMLInDoc *xmldoc = NULL;
-       if (NULL == xmldoc)
+       if (NULL == xmldoc) {
                xmldoc = gsf_xml_in_doc_new (dtd, NULL);
+               go_xml_in_doc_dispose_on_exit (&xmldoc);
+       }
        gsf_xml_in_push_state (xin, xmldoc, doc, NULL, attrs);
 }
 
diff --git a/goffice/component/go-component.c b/goffice/component/go-component.c
index e7975fd..119ada8 100644
--- a/goffice/component/go-component.c
+++ b/goffice/component/go-component.c
@@ -763,8 +763,10 @@ go_component_sax_push_parser (GsfXMLIn *xin, xmlChar const **attrs,
        static GsfXMLInDoc *doc = NULL;
        GOCompXMLReadState *state;
 
-       if (NULL == doc)
+       if (NULL == doc) {
                doc = gsf_xml_in_doc_new (dtd, NULL);
+               go_xml_in_doc_dispose_on_exit (&doc);
+       }
        state = g_new0 (GOCompXMLReadState, 1);
        state->handler = handler;
        state->user_data = user_data;
diff --git a/goffice/goffice.c b/goffice/goffice.c
index 9d52425..44fc279 100644
--- a/goffice/goffice.c
+++ b/goffice/goffice.c
@@ -279,4 +279,5 @@ libgoffice_shutdown (void)
        g_free ((char *)libgoffice_locale_dir);
        g_free ((char *)libgoffice_lib_dir);
 #endif
+       _go_libxml_extras_shutdown ();
 }
diff --git a/goffice/graph/gog-axis-color-map.c b/goffice/graph/gog-axis-color-map.c
index 3003b44..2bf3908 100644
--- a/goffice/graph/gog-axis-color-map.c
+++ b/goffice/graph/gog-axis-color-map.c
@@ -389,8 +389,10 @@ gog_axis_color_map_prep_sax (GOPersist *gp, GsfXMLIn *xin, xmlChar const **attrs
        state->langs = g_get_language_names ();
        state->name_lang_score = G_MAXINT;
        state->color_stops = NULL;
-       if (!xml)
+       if (!xml) {
                xml = gsf_xml_in_doc_new (color_map_dtd, NULL);
+               go_xml_in_doc_dispose_on_exit (&xml);
+       }
        gsf_xml_in_push_state (xin, xml, state, (GsfXMLInExtDtor) parse_done_cb, attrs);
 }
 
@@ -918,8 +920,10 @@ color_map_load_from_uri (char const *uri)
        state.langs = g_get_language_names ();
        state.name_lang_score = G_MAXINT;
        state.color_stops = NULL;
-       if (!xml)
+       if (!xml) {
                xml = gsf_xml_in_doc_new (color_map_dtd, NULL);
+               go_xml_in_doc_dispose_on_exit (&xml);
+       }
        if (!gsf_xml_in_doc_parse (xml, input, &state))
                g_warning ("[GogAxisColorMap]: Could not parse %s", uri);
        if (state.map != NULL) {
diff --git a/goffice/graph/gog-object-xml.c b/goffice/graph/gog-object-xml.c
index 6f55a98..5af9dc4 100644
--- a/goffice/graph/gog-object-xml.c
+++ b/goffice/graph/gog-object-xml.c
@@ -529,8 +529,10 @@ gog_object_sax_push_parser (GsfXMLIn *xin, xmlChar const **attrs,
 {
        GogXMLReadState *state;
 
-       if (NULL == gog_sax_doc)
+       if (NULL == gog_sax_doc) {
                gog_sax_doc = gsf_xml_in_doc_new (gog_dtd, NULL);
+               go_xml_in_doc_dispose_on_exit (&gog_sax_doc);
+       }
        state = g_new0 (GogXMLReadState, 1);
        state->handler = handler;
        state->user_data = user_data;
@@ -545,8 +547,10 @@ gog_object_new_from_input (GsfInput *input,
 {
        GogObject *res = NULL;
        GogXMLReadState *state = g_new0 (GogXMLReadState, 1);
-       if (NULL == gog_sax_doc)
+       if (NULL == gog_sax_doc) {
                gog_sax_doc = gsf_xml_in_doc_new (gog_dtd, NULL);
+               go_xml_in_doc_dispose_on_exit (&gog_sax_doc);
+       }
        state->user_unserialize = user_unserialize;
        if (gsf_xml_in_doc_parse (gog_sax_doc, input, state)) {
                res = state->obj;
diff --git a/goffice/graph/gog-theme.c b/goffice/graph/gog-theme.c
index 393eb28..23582f1 100644
--- a/goffice/graph/gog-theme.c
+++ b/goffice/graph/gog-theme.c
@@ -803,8 +803,10 @@ gog_theme_prep_sax (GOPersist *gp, GsfXMLIn *xin, xmlChar const **attrs)
        state->name_lang_score = G_MAXINT;
        state->desc_lang_score = G_MAXINT;
        state->garbage = NULL;
-       if (!xml)
+       if (!xml) {
                xml = gsf_xml_in_doc_new (theme_dtd, NULL);
+               go_xml_in_doc_dispose_on_exit (&xml);
+       }
        gsf_xml_in_push_state (xin, xml, state, (GsfXMLInExtDtor) parse_done_cb, attrs);
 }
 
@@ -1604,8 +1606,10 @@ theme_load_from_uri (char const *uri)
        state.desc = state.lang = state.name = NULL;
        state.langs = g_get_language_names ();
        state.name_lang_score = state.desc_lang_score = G_MAXINT;
-       if (!xml)
+       if (!xml) {
                xml = gsf_xml_in_doc_new (theme_dtd, NULL);
+               go_xml_in_doc_dispose_on_exit (&xml);
+       }
        if (!gsf_xml_in_doc_parse (xml, input, &state))
                g_warning ("[GogTheme]: Could not parse %s", uri);
        if (state.theme != NULL) {
diff --git a/goffice/utils/go-libxml-extras.c b/goffice/utils/go-libxml-extras.c
index 3090ae2..5121676 100644
--- a/goffice/utils/go-libxml-extras.c
+++ b/goffice/utils/go-libxml-extras.c
@@ -354,3 +354,25 @@ go_xml_out_add_color (GsfXMLOut *output, char const *id, GOColor c)
        gsf_xml_out_add_cstr_unchecked (output, id, str);
        g_free (str);
 }
+
+static GSList *xml_in_docs;
+
+void
+_go_libxml_extras_shutdown (void)
+{
+       GSList *l;
+
+       for (l = xml_in_docs; l; l = l->next) {
+               GsfXMLInDoc **pdoc = l->data;
+               gsf_xml_in_doc_free (*pdoc);
+               *pdoc = NULL;
+       }
+       g_slist_free (xml_in_docs);
+       xml_in_docs = NULL;
+}
+
+void
+go_xml_in_doc_dispose_on_exit (GsfXMLInDoc **pdoc)
+{
+       xml_in_docs = g_slist_prepend (xml_in_docs, pdoc);
+}
diff --git a/goffice/utils/go-libxml-extras.h b/goffice/utils/go-libxml-extras.h
index a251475..08d9f28 100644
--- a/goffice/utils/go-libxml-extras.h
+++ b/goffice/utils/go-libxml-extras.h
@@ -48,6 +48,10 @@ xmlNode *go_xml_get_child_by_name_by_lang (xmlNode const *tree, char const *name
 
 void      go_xml_out_add_color (GsfXMLOut *out, char const *id, GOColor c);
 
+void       go_xml_in_doc_dispose_on_exit (GsfXMLInDoc **pdoc);
+
+void _go_libxml_extras_shutdown (void);
+
 G_END_DECLS
 
 #endif /* GO_LIBXML_EXTRAS_H */
diff --git a/goffice/utils/go-style.c b/goffice/utils/go-style.c
index df49810..f0f7372 100644
--- a/goffice/utils/go-style.c
+++ b/goffice/utils/go-style.c
@@ -1823,8 +1823,10 @@ go_style_persist_prep_sax (GOPersist *gp, GsfXMLIn *xin, xmlChar const **attrs)
        };
        static GsfXMLInDoc *doc = NULL;
 
-       if (NULL == doc)
+       if (NULL == doc) {
                doc = gsf_xml_in_doc_new (dtd, NULL);
+               go_xml_in_doc_dispose_on_exit (&doc);
+       }
        gsf_xml_in_push_state (xin, doc, gp, NULL, attrs);
 }
 


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