From dd213a33e073c36ce4615d100cca24ca65a0d473 Mon Sep 17 00:00:00 2001 From: wanghuan2022 Date: Sat, 15 Jul 2023 17:20:21 +0800 Subject: [PATCH] DumpHeapSnapShot memory leak bugfix desc: DumpHeapSnapShot memory leak bugfix solu: 1. StreamWriter need release in deconstructing; 2. not if good, should be if is_open can close fileStream; issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/I7LE8L Signed-off-by: wanghuan2022 Change-Id: I9c4cc04870b97b118e02e974eff513518961abae --- ecmascript/dfx/hprof/file_stream.cpp | 8 ++++++-- ecmascript/dfx/hprof/file_stream.h | 2 +- ecmascript/dfx/hprof/heap_snapshot_json_serializer.cpp | 3 ++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/ecmascript/dfx/hprof/file_stream.cpp b/ecmascript/dfx/hprof/file_stream.cpp index 4199977c1e..5352806500 100644 --- a/ecmascript/dfx/hprof/file_stream.cpp +++ b/ecmascript/dfx/hprof/file_stream.cpp @@ -32,9 +32,14 @@ FileStream::FileStream(const std::string &fileName) Initialize(fileName); } +FileStream::~FileStream() +{ + EndOfStream(); +} + void FileStream::EndOfStream() { - if (Good()) { + if (fileStream_.is_open()) { fileStream_.close(); } } @@ -50,7 +55,6 @@ void FileStream::Initialize(const std::string &fileName) std::pair realPath = FilePathValid(fileName); if (!realPath.first) { LOG_ECMA(ERROR) << "FileStream: check file path failed"; - fileStream_.close(); return; } diff --git a/ecmascript/dfx/hprof/file_stream.h b/ecmascript/dfx/hprof/file_stream.h index 413b8b5082..8e4f8e8580 100644 --- a/ecmascript/dfx/hprof/file_stream.h +++ b/ecmascript/dfx/hprof/file_stream.h @@ -28,7 +28,7 @@ namespace panda::ecmascript { class FileStream : public Stream { public: explicit FileStream(const std::string &fileName); - ~FileStream() override = default; + ~FileStream() override; void EndOfStream() override; diff --git a/ecmascript/dfx/hprof/heap_snapshot_json_serializer.cpp b/ecmascript/dfx/hprof/heap_snapshot_json_serializer.cpp index 96bf98b28c..e08b80bb1a 100644 --- a/ecmascript/dfx/hprof/heap_snapshot_json_serializer.cpp +++ b/ecmascript/dfx/hprof/heap_snapshot_json_serializer.cpp @@ -22,8 +22,9 @@ namespace panda::ecmascript { HeapSnapshotJSONSerializer::~HeapSnapshotJSONSerializer() { - if (!writer_) { + if (writer_) { delete writer_; + writer_ = nullptr; } }