[llvm] r188615 - Go through the really awkward dance required to delete the memory

Chandler Carruth chandlerc at gmail.com
Sat Aug 17 18:20:33 PDT 2013


Author: chandlerc
Date: Sat Aug 17 20:20:32 2013
New Revision: 188615

URL: http://llvm.org/viewvc/llvm-project?rev=188615&view=rev
Log:
Go through the really awkward dance required to delete the memory
allocated by setupterm. Without this, some folks are seeing leaked
memory whenever this routine is called more than once. Thanks to Craig
Topper for the report.

Modified:
    llvm/trunk/lib/Support/Unix/Process.inc

Modified: llvm/trunk/lib/Support/Unix/Process.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Unix/Process.inc?rev=188615&r1=188614&r2=188615&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Unix/Process.inc (original)
+++ llvm/trunk/lib/Support/Unix/Process.inc Sat Aug 17 20:20:32 2013
@@ -240,10 +240,12 @@ unsigned Process::StandardErrColumns() {
 }
 
 #ifdef HAVE_TERMINFO
-// We manually declare these two extern functions because finding the correct
+// We manually declare these extern functions because finding the correct
 // headers from various terminfo, curses, or other sources is harder than
 // writing their specs down.
 extern "C" int setupterm(char *term, int filedes, int *errret);
+extern "C" struct term *set_curterm(struct term *termp);
+extern "C" int del_curterm(struct term *termp);
 extern "C" int tigetnum(char *capname);
 #endif
 
@@ -272,7 +274,15 @@ static bool terminalHasColors(int fd) {
   //
   // The 'tigetnum' routine returns -2 or -1 on errors, and might return 0 if
   // the terminfo says that no colors are supported.
-  if (tigetnum(const_cast<char *>("colors")) > 0)
+  bool HasColors = tigetnum(const_cast<char *>("colors")) > 0;
+
+  // Now extract the structure allocated by setupterm and free its memory
+  // through a really silly dance.
+  struct term *termp = set_curterm((struct term *)0);
+  (void)del_curterm(termp); // Drop any errors here.
+
+  // Return true if we found a color capabilities for the current terminal.
+  if (HasColors)
     return true;
 #endif
 





More information about the llvm-commits mailing list