[llvm] [StringMap] Move free into StringMapImpl dtor (NFC) (PR #91908)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sun May 12 19:38:07 PDT 2024


https://github.com/nikic created https://github.com/llvm/llvm-project/pull/91908

StringMapImpl allocates the memory for the table, but does not have a dtor that free it. Instead, StringMap (which inherits from StringMapImpl) contains the free call. I don't really see a good reason why this free is performed in the "wrong" class, so move it into StringMapImpl.

(I have no motivation for this change beyond seeing this in a static analyzer report.)

>From 31813bf9b2ef9b72f736ddc543e1d7188a1cb5fc Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 13 May 2024 10:49:57 +0900
Subject: [PATCH] [StringMap] Move free into StringMapImpl dtor (NFC)

StringMapImpl allocates the memory for the table, but does not
have a dtor that free it. Instead, StringMap (which inherits from
StringMapImpl) contains the free call. I don't really see a good
reason why this free is performed in the "wrong" class, so move
it into StringMapImpl.

(I have no motivation for this change beyond seeing this in a
static analyzer report.)
---
 llvm/include/llvm/ADT/StringMap.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/ADT/StringMap.h b/llvm/include/llvm/ADT/StringMap.h
index daaf82654e094..9b58af7327391 100644
--- a/llvm/include/llvm/ADT/StringMap.h
+++ b/llvm/include/llvm/ADT/StringMap.h
@@ -53,6 +53,7 @@ class StringMapImpl {
   }
 
   StringMapImpl(unsigned InitSize, unsigned ItemSize);
+  ~StringMapImpl() { free(TheTable); }
   unsigned RehashTable(unsigned BucketNo = 0);
 
   /// LookupBucketFor - Look up the bucket that the specified string should end
@@ -203,7 +204,6 @@ class LLVM_ALLOCATORHOLDER_EMPTYBASE StringMap
         }
       }
     }
-    free(TheTable);
   }
 
   using AllocTy::getAllocator;



More information about the llvm-commits mailing list