Browse files

Free all unfreed memory (pass valgrind checks)

  • Loading branch information...
1 parent 5291935 commit a1155d5242a1353a26a5252ae4acce5f9f5524e5 Nikolai Weibull committed Aug 28, 2012
Showing with 42 additions and 11 deletions.
  1. +28 −10 src/grammar.y
  2. +8 −0 src/nmc.c
  3. +2 −0 src/nmc.h
  4. +4 −1 src/parser.c
View
38 src/grammar.y
@@ -11,6 +11,9 @@
#include "nmc.h"
#include "parser.h"
+extern void nmc_grammar_initialize(void);
+extern void nmc_grammar_finalize(void);
+
typedef xmlNodePtr (*definefn)(const xmlChar *, regmatch_t *);
struct definition
@@ -36,7 +39,7 @@ definition_free(xmlLinkPtr link)
{
struct definition *definition = (struct definition *)xmlLinkGetData(link);
regfree(&definition->regex);
- xmlFree(definition);
+ nmc_free(definition);
}
static xmlNodePtr
@@ -69,8 +72,8 @@ ref(const xmlChar *buffer, regmatch_t *matches)
(const char *[]){ "title", "uri", NULL });
}
-static void
-initialize_definitions(void)
+void
+nmc_grammar_initialize(void)
{
if (definitions != NULL)
return;
@@ -79,6 +82,13 @@ initialize_definitions(void)
xmlListPushBack(definitions, definition_new("^(.+) +at +(.+)", ref));
}
+void
+nmc_grammar_finalize(void)
+{
+ if (definitions != NULL)
+ xmlListDelete(definitions);
+}
+
struct footnote_define_closure
{
const xmlChar *buffer;
@@ -128,12 +138,17 @@ footnote_new(YYLTYPE *location, xmlChar *id, xmlBufferPtr buffer)
}
static void
-footnote_free(xmlLinkPtr link)
+footnote_free1(struct footnote *footnote)
{
- struct footnote *footnote = (struct footnote *)xmlLinkGetData(link);
xmlFree(footnote->id);
xmlFreeNode(footnote->node);
- xmlFree(footnote);
+ nmc_free(footnote);
+}
+
+static void
+footnote_free(xmlLinkPtr link)
+{
+ footnote_free1((struct footnote *)xmlLinkGetData(link));
}
struct sigil
@@ -156,7 +171,7 @@ sigil_free(xmlLinkPtr link)
{
struct sigil *sigil = (struct sigil *)xmlLinkGetData(link);
xmlFree(sigil->id);
- xmlFree(sigil);
+ nmc_free(sigil);
}
struct anchor
@@ -183,7 +198,10 @@ anchor_id(struct anchor *anchor)
static void
anchor_free(xmlLinkPtr link)
{
- xmlFree(xmlLinkGetData(link));
+ struct anchor *anchor = (struct anchor *)xmlLinkGetData(link);
+ if (anchor->node->children == NULL)
+ xmlFreeNode(anchor->node);
+ nmc_free(anchor);
}
static int
@@ -275,7 +293,6 @@ node_free(struct nmc_parser *parser, xmlNodePtr node)
%error-verbose
%expect 0
%locations
-%initial-action { initialize_definitions(); }
%token END 0 "end of file"
%token ERROR
@@ -348,7 +365,7 @@ node_free(struct nmc_parser *parser, xmlNodePtr node)
%destructor { xmlBufferFree($$); } <buffer>
%destructor { node_free(parser, $$); } <node>
%destructor { xmlListDelete($$); } <list>
-%destructor { xmlFree($$); } <footnote>
+%destructor { footnote_free1($$); } <footnote>
%code
{
@@ -652,6 +669,7 @@ footnote: FOOTNOTE {
nmc_parser_error(parser, &@$,
"unrecognized footnote content: %s",
xmlBufferContent($1.buffer));
+ xmlBufferFree($1.buffer);
};
paragraph: PARAGRAPH inlines { $$ = wrap("p", $2); };
View
8 src/nmc.c
@@ -10,6 +10,8 @@
#include "parser.h"
extern int nmc_grammar_debug;
+extern void nmc_grammar_initialize(void);
+extern void nmc_grammar_finalize(void);
static int
report_nmc_parser_error(const struct nmc_parser_error *error)
@@ -49,6 +51,8 @@ main(UNUSED(int argc), UNUSED(char **argv))
xmlInitParser();
+ nmc_grammar_initialize();
+
xmlListPtr errors;
xmlDocPtr doc = nmc_parse(BAD_CAST buffer, &errors);
if (!xmlListEmpty(errors))
@@ -61,8 +65,12 @@ main(UNUSED(int argc), UNUSED(char **argv))
xmlFreeDoc(doc);
+ nmc_grammar_finalize();
+
xmlCleanupParser();
+ nmc_free(buffer);
+
return result;
}
View
2 src/nmc.h
@@ -21,4 +21,6 @@
#define nmc_new_n(type, n) (type *)xmlMalloc(sizeof(type) * (n))
+#define nmc_free(value) xmlFree(value)
+
#endif
View
5 src/parser.c
@@ -14,7 +14,10 @@ int nmc_grammar_parse(struct nmc_parser *parser);
static void
error_free(xmlLinkPtr link)
{
- xmlFree(xmlLinkGetData(link));
+ struct nmc_parser_error *error =
+ (struct nmc_parser_error *)xmlLinkGetData(link);
+ nmc_free(error->message);
+ nmc_free(error);
}
void

0 comments on commit a1155d5

Please sign in to comment.