From e5bf7e55c91490b07c49d8960fa7983d864936c4 Mon Sep 17 00:00:00 2001 From: Jatin Chaudhary Date: Tue, 3 Sep 2024 11:57:39 +0100 Subject: [PATCH] [Comgr] use shared_ptr for rocm path detection This shows up as a leak. Make sure the memory is released after the program is done executing. Change-Id: I5b3e0a3f0e2e744cb19a9a42be2eb525d5c2bd9d --- amd/comgr/src/comgr-env.cpp | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/amd/comgr/src/comgr-env.cpp b/amd/comgr/src/comgr-env.cpp index b22a9b3e65e28b8..0019aa1db1d3ee9 100644 --- a/amd/comgr/src/comgr-env.cpp +++ b/amd/comgr/src/comgr-env.cpp @@ -38,6 +38,7 @@ #include "llvm/Support/VirtualFileSystem.h" #include +#include #include using namespace llvm; @@ -245,18 +246,18 @@ class SpackInstallationDetector : public InstallationDetector { } }; -InstallationDetector *CreatePathDetector(StringRef Path, - bool isComgrPath = false) { +std::shared_ptr +CreatePathDetector(StringRef Path, bool isComgrPath = false) { StringRef DirName = llvm::sys::path::filename(Path); if ((!isComgrPath && DirName.starts_with("rocm-cmake-")) || (isComgrPath && DirName.starts_with("comgr-"))) { - return new SpackInstallationDetector(Path, isComgrPath); + return std::make_shared(Path, isComgrPath); } - return new InstallationDetector(Path, isComgrPath); + return std::make_shared(Path, isComgrPath); } -InstallationDetector *getDetectorImpl() { +std::shared_ptr getDetectorImpl() { SmallString<128> ROCmInstallPath; static const char *EnvROCMPath = std::getenv("ROCM_PATH"); @@ -264,21 +265,16 @@ InstallationDetector *getDetectorImpl() { ROCmInstallPath = EnvROCMPath; } - InstallationDetector *Detector; if (ROCmInstallPath == "") { std::string ComgrInstallationPath = getComgrInstallPathFromExecutable(); - Detector = - CreatePathDetector(ComgrInstallationPath, true /* isComgrPath */); - } else { - Detector = CreatePathDetector(ROCmInstallPath); + return CreatePathDetector(ComgrInstallationPath, true /* isComgrPath */); } - - return Detector; + return CreatePathDetector(ROCmInstallPath); } InstallationDetector *getDetector() { - static InstallationDetector *Detector = getDetectorImpl(); - return Detector; + static auto Detector = getDetectorImpl(); + return Detector.get(); } llvm::StringRef getROCMPath() { return getDetector()->getROCmPath(); }