Put MCSectionCOFF::Name into the MCContext instead of leaking it.
authorJeffrey Yasskin <jyasskin@google.com>
Mon, 22 Mar 2010 23:26:12 +0000 (23:26 +0000)
committerJeffrey Yasskin <jyasskin@google.com>
Mon, 22 Mar 2010 23:26:12 +0000 (23:26 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@99231 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/MC/MCSection.h
lib/MC/MCSection.cpp

index 3d8815a7783fb16755e321303ae4c72d9ab73674..4a1c46ccb926f83093272f58c064d3f25f0103e6 100644 (file)
@@ -42,9 +42,8 @@ namespace llvm {
   };
 
   class MCSectionCOFF : public MCSection {
-    // FIXME: This memory is leaked because MCSectionCOFF is bump pointer
-    // allocated and this never gets freed.
-    std::string Name;
+    // The memory for this string is stored in the same MCContext as *this.
+    StringRef Name;
     
     /// IsDirective - This is true if the section name is a directive, not
     /// something that should be printed with ".section".
@@ -61,7 +60,7 @@ namespace llvm {
     static MCSectionCOFF *Create(StringRef Name, bool IsDirective, 
                                  SectionKind K, MCContext &Ctx);
 
-    const std::string &getName() const { return Name; }
+    StringRef getName() const { return Name; }
     bool isDirective() const { return IsDirective; }
     
     virtual void PrintSwitchToSection(const MCAsmInfo &MAI,
index 24c89efefc66acea0311b259f95a1e79da5aa2a9..f6e96368eb2056b5d8168948117ee4f32af2b6b2 100644 (file)
@@ -26,7 +26,11 @@ MCSection::~MCSection() {
 
 MCSectionCOFF *MCSectionCOFF::
 Create(StringRef Name, bool IsDirective, SectionKind K, MCContext &Ctx) {
-  return new (Ctx) MCSectionCOFF(Name, IsDirective, K);
+  char *NameCopy = static_cast<char*>(
+    Ctx.Allocate(Name.size(), /*Alignment=*/1));
+  memcpy(NameCopy, Name.data(), Name.size());
+  return new (Ctx) MCSectionCOFF(StringRef(NameCopy, Name.size()),
+                                 IsDirective, K);
 }
 
 void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI,