Skip to content

Commit 6551ef3

Browse files
committed
[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
1 parent 9e9f6ad commit 6551ef3

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

amd/comgr/src/comgr-env.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "llvm/Support/VirtualFileSystem.h"
3939

4040
#include <fstream>
41+
#include <memory>
4142
#include <stdlib.h>
4243

4344
using namespace llvm;
@@ -241,40 +242,35 @@ class SpackInstallationDetector : public InstallationDetector {
241242
}
242243
};
243244

244-
InstallationDetector *CreatePathDetector(StringRef Path,
245-
bool isComgrPath = false) {
245+
std::shared_ptr<InstallationDetector>
246+
CreatePathDetector(StringRef Path, bool isComgrPath = false) {
246247
StringRef DirName = llvm::sys::path::filename(Path);
247248
if ((!isComgrPath && DirName.starts_with("rocm-cmake-")) ||
248249
(isComgrPath && DirName.starts_with("comgr-"))) {
249-
return new SpackInstallationDetector(Path, isComgrPath);
250+
return std::make_shared<SpackInstallationDetector>(Path, isComgrPath);
250251
}
251252

252-
return new InstallationDetector(Path, isComgrPath);
253+
return std::make_shared<InstallationDetector>(Path, isComgrPath);
253254
}
254255

255-
InstallationDetector *getDetectorImpl() {
256+
std::shared_ptr<InstallationDetector> getDetectorImpl() {
256257
SmallString<128> ROCmInstallPath;
257258

258259
static const char *EnvROCMPath = std::getenv("ROCM_PATH");
259260
if (EnvROCMPath) {
260261
ROCmInstallPath = EnvROCMPath;
261262
}
262263

263-
InstallationDetector *Detector;
264264
if (ROCmInstallPath == "") {
265265
std::string ComgrInstallationPath = getComgrInstallPathFromExecutable();
266-
Detector =
267-
CreatePathDetector(ComgrInstallationPath, true /* isComgrPath */);
268-
} else {
269-
Detector = CreatePathDetector(ROCmInstallPath);
266+
return CreatePathDetector(ComgrInstallationPath, true /* isComgrPath */);
270267
}
271-
272-
return Detector;
268+
return CreatePathDetector(ROCmInstallPath);
273269
}
274270

275271
InstallationDetector *getDetector() {
276-
static InstallationDetector *Detector = getDetectorImpl();
277-
return Detector;
272+
static auto Detector = getDetectorImpl();
273+
return Detector.get();
278274
}
279275

280276
llvm::StringRef getROCMPath() { return getDetector()->getROCmPath(); }

0 commit comments

Comments
 (0)