Fix memory leaks in CFC

Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/43de47bc
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/43de47bc
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/43de47bc

Branch: refs/heads/master
Commit: 43de47bc42e51c6c418477991c7b80c6b279d371
Parents: 6c5b034
Author: Nick Wellnhofer <wellnho...@aevum.de>
Authored: Mon Feb 29 23:23:39 2016 +0100
Committer: Nick Wellnhofer <wellnho...@aevum.de>
Committed: Mon Feb 29 23:23:39 2016 +0100

----------------------------------------------------------------------
 compiler/c/cfc.c                  |  2 ++
 compiler/src/CFCCHtml.c           |  5 ++++-
 compiler/src/CFCDocument.c        | 12 ++++++++++++
 compiler/src/CFCDocument.h        |  3 +++
 compiler/src/CFCPerlPod.c         |  2 ++
 compiler/src/CFCTestDocuComment.c |  2 ++
 6 files changed, 25 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/43de47bc/compiler/c/cfc.c
----------------------------------------------------------------------
diff --git a/compiler/c/cfc.c b/compiler/c/cfc.c
index 7ff3a5e..6f5f527 100644
--- a/compiler/c/cfc.c
+++ b/compiler/c/cfc.c
@@ -18,6 +18,7 @@
 #include "CFCBindCore.h"
 #include "CFCC.h"
 #include "CFCClass.h"
+#include "CFCDocument.h"
 #include "CFCHierarchy.h"
 #include "CFCParcel.h"
 #include "CFCUtil.h"
@@ -255,6 +256,7 @@ main(int argc, char **argv) {
     FREEMEM(footer);
 
     CFCClass_clear_registry();
+    CFCDocument_clear_registry();
     CFCParcel_reap_singletons();
 
     S_free_arguments(&args);

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/43de47bc/compiler/src/CFCCHtml.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCCHtml.c b/compiler/src/CFCCHtml.c
index b44aea7..6f42ea6 100644
--- a/compiler/src/CFCCHtml.c
+++ b/compiler/src/CFCCHtml.c
@@ -318,12 +318,14 @@ CFCCHtml_write_html_docs(CFCCHtml *self) {
         char *html_doc = html_docs[i];
         CFCUtil_write_if_changed(path, html_doc, strlen(html_doc));
         FREEMEM(html_doc);
+        FREEMEM(dir);
         FREEMEM(path);
         FREEMEM(filename);
     }
 
     FREEMEM(html_docs);
     FREEMEM(filenames);
+    FREEMEM(md_docs);
     FREEMEM(ordered);
 }
 
@@ -475,7 +477,7 @@ S_create_standalone_doc(CFCCHtml *self, CFCDocument *doc) {
     char *title  = CFCUtil_global_replace(path, CHY_DIR_SEP, "::");
     char *header = CFCUtil_global_replace(self->header, "{title}", title);
 
-    const char *md = CFCDocument_get_contents(doc);
+    char *md = CFCDocument_get_contents(doc);
     int dir_level = 0;
     for (size_t i = 0; path[i]; i++) {
         if (path[i] == CHY_DIR_SEP_CHAR) { ++dir_level; }
@@ -485,6 +487,7 @@ S_create_standalone_doc(CFCCHtml *self, CFCDocument *doc) {
     char *html_doc = CFCUtil_sprintf("%s%s%s", header, body, self->footer);
 
     FREEMEM(body);
+    FREEMEM(md);
     FREEMEM(header);
     FREEMEM(title);
     return html_doc;

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/43de47bc/compiler/src/CFCDocument.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCDocument.c b/compiler/src/CFCDocument.c
index 6d1325c..0cdf9a0 100644
--- a/compiler/src/CFCDocument.c
+++ b/compiler/src/CFCDocument.c
@@ -81,6 +81,7 @@ CFCDocument_destroy(CFCDocument *self) {
     FREEMEM(self->path);
     FREEMEM(self->path_part);
     FREEMEM(self->name);
+    CFCBase_destroy((CFCBase*)self);
 }
 
 static void
@@ -123,6 +124,17 @@ CFCDocument_fetch(const char *name) {
     return NULL;
 }
 
+void
+CFCDocument_clear_registry(void) {
+    for (size_t i = 0; i < registry_size; i++) {
+        CFCBase_decref((CFCBase*)registry[i]);
+    }
+    FREEMEM(registry);
+    registry_size = 0;
+    registry_cap  = 0;
+    registry      = NULL;
+}
+
 char*
 CFCDocument_get_contents(CFCDocument *self) {
     size_t len;

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/43de47bc/compiler/src/CFCDocument.h
----------------------------------------------------------------------
diff --git a/compiler/src/CFCDocument.h b/compiler/src/CFCDocument.h
index b138803..7d575bb 100644
--- a/compiler/src/CFCDocument.h
+++ b/compiler/src/CFCDocument.h
@@ -42,6 +42,9 @@ CFCDocument_get_registry(void);
 CFCDocument*
 CFCDocument_fetch(const char *name);
 
+void
+CFCDocument_clear_registry(void);
+
 char*
 CFCDocument_get_contents(CFCDocument *self);
 

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/43de47bc/compiler/src/CFCPerlPod.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCPerlPod.c b/compiler/src/CFCPerlPod.c
index 855b698..c09eacf 100644
--- a/compiler/src/CFCPerlPod.c
+++ b/compiler/src/CFCPerlPod.c
@@ -606,6 +606,7 @@ CFCPerlPod_md_doc_to_pod(const char *module, const char 
*md) {
         cmark_node *header_child = cmark_node_first_child(maybe_header);
         char *short_desc = S_nodes_to_pod(header_child, NULL, 1);
         name = CFCUtil_sprintf("%s - %s", module, short_desc);
+        FREEMEM(short_desc);
 
         cmark_node *remaining = cmark_node_next(maybe_header);
         desc = S_nodes_to_pod(remaining, NULL, 1);
@@ -628,6 +629,7 @@ CFCPerlPod_md_doc_to_pod(const char *module, const char 
*md) {
 
     FREEMEM(name);
     FREEMEM(desc);
+    cmark_node_free(doc);
     return retval;
 }
 

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/43de47bc/compiler/src/CFCTestDocuComment.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCTestDocuComment.c 
b/compiler/src/CFCTestDocuComment.c
index 69ab177..2e697d5 100644
--- a/compiler/src/CFCTestDocuComment.c
+++ b/compiler/src/CFCTestDocuComment.c
@@ -17,6 +17,7 @@
 #define CFC_USE_TEST_MACROS
 #include "CFCBase.h"
 #include "CFCDocuComment.h"
+#include "CFCDocument.h"
 #include "CFCCHtml.h"
 #include "CFCCMan.h"
 #include "CFCClass.h"
@@ -313,6 +314,7 @@ S_test_generator(CFCTest *test) {
     CFCBase_decref((CFCBase*)parcel);
     CFCBase_decref((CFCBase*)hierarchy);
 
+    CFCDocument_clear_registry();
     CFCParcel_reap_singletons();
 }
 

Reply via email to