correct libxml memory management to avoid leaks on exit

This commit is contained in:
Alexey Fedoseev
2024-04-13 19:04:53 +03:00
parent 3900eb492f
commit eee047577a

View File

@@ -2931,16 +2931,18 @@ int cyberiada_read_sm_document(CyberiadaDocument* cyb_doc, const char* filename,
NamesList* nl = NULL; NamesList* nl = NULL;
cyberiada_init_sm_document(cyb_doc); cyberiada_init_sm_document(cyb_doc);
cyberiada_init_action_regexps();
xmlInitParser();
do {
/* parse the file and get the DOM */ /* parse the file and get the DOM */
if ((doc = xmlReadFile(filename, NULL, 0)) == NULL) { if ((doc = xmlReadFile(filename, NULL, 0)) == NULL) {
ERROR("error: could not parse file %s\n", filename); ERROR("error: could not parse file %s\n", filename);
return CYBERIADA_XML_ERROR; res = CYBERIADA_XML_ERROR;
break;
} }
cyberiada_init_action_regexps();
do {
/* get the root element node */ /* get the root element node */
root = xmlDocGetRootElement(doc); root = xmlDocGetRootElement(doc);
@@ -4139,14 +4141,16 @@ int cyberiada_write_sm_document(CyberiadaDocument* doc, const char* filename, Cy
return CYBERIADA_BAD_PARAMETER; return CYBERIADA_BAD_PARAMETER;
} }
xmlInitParser();
do {
writer = xmlNewTextWriterFilename(filename, 0); writer = xmlNewTextWriterFilename(filename, 0);
if (!writer) { if (!writer) {
ERROR("cannot open xml writter for file %s\n", filename); ERROR("cannot open xml writter for file %s\n", filename);
xmlCleanupParser(); res = CYBERIADA_XML_ERROR;
return CYBERIADA_XML_ERROR;
} }
do {
res = xmlTextWriterStartDocument(writer, NULL, GRAPHML_XML_ENCODING, NULL); res = xmlTextWriterStartDocument(writer, NULL, GRAPHML_XML_ENCODING, NULL);
if (res < 0) { if (res < 0) {
ERROR("error writing xml start document: %d\n", res); ERROR("error writing xml start document: %d\n", res);
@@ -4181,6 +4185,7 @@ int cyberiada_write_sm_document(CyberiadaDocument* doc, const char* filename, Cy
} while (0); } while (0);
xmlFreeTextWriter(writer); xmlFreeTextWriter(writer);
xmlCleanupParser();
if (res != CYBERIADA_NO_ERROR) { if (res != CYBERIADA_NO_ERROR) {
return res; return res;