[PATCH] fix a leak in CodeGenFunction::EmitSwitchStmt (detected by lsan)

Kostya Serebryany kcc at google.com
Wed Dec 25 04:52:03 PST 2013


Hi nicholas,

On code like switch (0) { case 0:; } clang leaks one basic block.

First, CodeGenFunction::EmitSwitchStmt creates a new BasicBlock,
then it does the following and exits:
  // See if we can constant fold the condition of the switch and therefore only
  // emit the live case statement (if any) of the switch.
This BasicBlock remains unattached to anything an leaks.

The following fix removes the leak.
Confirmed by running clang on a function with 500K such statements:
the memory footprint drops from 800K to 500K.
(also tested by "check-clang")

Alternative fix may be to move the creation of SwitchExit to some later point,
by my attempts to do so failed (the test CodeGenCXX/condition.cpp starts failing).

http://llvm-reviews.chandlerc.com/D2472

Files:
  lib/CodeGen/CGStmt.cpp

Index: lib/CodeGen/CGStmt.cpp
===================================================================
--- lib/CodeGen/CGStmt.cpp
+++ lib/CodeGen/CGStmt.cpp
@@ -1270,6 +1270,7 @@
       // switches continue to function properly
       SwitchInsn = SavedSwitchInsn;
 
+      delete SwitchExit.getBlock();  // SwitchExit will not be needed.
       return;
     }
   }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2472.1.patch
Type: text/x-patch
Size: 361 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131225/53a4139b/attachment.bin>


More information about the cfe-commits mailing list