[jit] Fix memory leak within diagnostic.c

2014-10-03 Thread David Malcolm
Committed to branch dmalcolm/jit:

Running e.g. test-factorial.exe under valgrind shows that libgccjit.so
was leaking ~13.5KB of RAM per invocation of the compiler here:

==57074== 21,440 bytes in 4 blocks are definitely lost in loss record 896 of 907
==57074==at 0x4A0645D: malloc (in 
/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==57074==by 0x58EFFC7: xmalloc (xmalloc.c:147)
==57074==by 0x58A9256: diagnostic_initialize(diagnostic_context*, int) 
(diagnostic.c:129)
==57074==by 0x4DF6062: toplev::main(int, char**) (toplev.c:1120)
==57074==by 0x4E09E13: gcc::jit::playback::context::compile() 
(internal-api.c:4987)
==57074==by 0x4E015C2: gcc::jit::recording::context::compile() 
(internal-api.c:878)
==57074==by 0x401CA4: test_jit (harness.h:188)
==57074==by 0x401D88: main (harness.h:230)
==57074==
==57074== 34,112 (256 direct, 33,856 indirect) bytes in 4 blocks are definitely 
lost in loss record 902 of 907
==57074==at 0x4A0645D: malloc (in 
/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==57074==by 0x58EFFC7: xmalloc (xmalloc.c:147)
==57074==by 0x58A91D4: diagnostic_initialize(diagnostic_context*, int) 
(diagnostic.c:122)
==57074==by 0x4DF6062: toplev::main(int, char**) (toplev.c:1120)
==57074==by 0x4E09E13: gcc::jit::playback::context::compile() 
(internal-api.c:4987)
==57074==by 0x4E015C2: gcc::jit::recording::context::compile() 
(internal-api.c:878)
==57074==by 0x401CA4: test_jit (harness.h:188)
==57074==by 0x401D88: main (harness.h:230)

Fix it.

gcc/ChangeLog.jit:
* diagnostic.c (diagnostic_finish): Free the memory for
context-classify_diagnostic and context-printer, running the
destructor for the latter.
---
 gcc/diagnostic.c | 9 +
 1 file changed, 9 insertions(+)

diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c
index b1b6366..642cbe3 100644
--- a/gcc/diagnostic.c
+++ b/gcc/diagnostic.c
@@ -177,6 +177,15 @@ diagnostic_finish (diagnostic_context *context)
 }
 
   diagnostic_file_cache_fini ();
+
+  XDELETEVEC (context-classify_diagnostic);
+  context-classify_diagnostic = NULL;
+
+  /* diagnostic_initialize allocates context-printer using XNEW
+ and placement-new.  */
+  context-printer-~pretty_printer ();
+  XDELETE (context-printer);
+  context-printer = NULL;
 }
 
 /* Initialize DIAGNOSTIC, where the message MSG has already been
-- 
1.7.11.7



RE: [jit] Fix memory leak within diagnostic.c

2014-10-03 Thread Manuel López-Ibáñez
 gcc/ChangeLog.jit:
 * diagnostic.c (diagnostic_finish): Free the memory for
 context-classify_diagnostic and context-printer, running the
 destructor for the latter.

It would be easier to review and merge your branch if you directly
propose these self-contained and generally useful patches directly to
trunk, no?

Cheers,

Manuel.