[Lldb-commits] [PATCH] D130802: [LLDB][NFC] Fix memory/resource leak in FifoFiles

Slava Gurevich via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Fri Jul 29 15:08:34 PDT 2022


fixathon created this revision.
fixathon added reviewers: clayborg, aadsm, jingham, JDevlieghere.
Herald added a project: All.
fixathon requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

The original code is creating a future object on the heap, but does not delete it in one of the return paths causing a leak.
Because the lifetime of the future object is the local scope of its containing function, there's no need for heap-based allocation in the first place.
This diff fixes the memory leak by moving the future object allocation to stack-based RAII. There's no change to the functionality or style of the code.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D130802

Files:
  lldb/tools/lldb-vscode/FifoFiles.cpp


Index: lldb/tools/lldb-vscode/FifoFiles.cpp
===================================================================
--- lldb/tools/lldb-vscode/FifoFiles.cpp
+++ lldb/tools/lldb-vscode/FifoFiles.cpp
@@ -53,37 +53,34 @@
   // We use a pointer for this future, because otherwise its normal destructor
   // would wait for the getline to end, rendering the timeout useless.
   Optional<std::string> line;
-  std::future<void> *future =
-      new std::future<void>(std::async(std::launch::async, [&]() {
-        std::ifstream reader(m_fifo_file, std::ifstream::in);
-        std::string buffer;
-        std::getline(reader, buffer);
-        if (!buffer.empty())
-          line = buffer;
-      }));
-  if (future->wait_for(timeout) == std::future_status::timeout || !line)
+  std::future<void> future = std::async(std::launch::async, [&]() {
+    std::ifstream reader(m_fifo_file, std::ifstream::in);
+    std::string buffer;
+    std::getline(reader, buffer);
+    if (!buffer.empty())
+      line = buffer;
+  });
+  if (future.wait_for(timeout) == std::future_status::timeout || !line)
     return createStringError(inconvertibleErrorCode(),
                              "Timed out trying to get messages from the " +
                                  m_other_endpoint_name);
-  delete future;
+
   return json::parse(*line);
 }
 
 Error FifoFileIO::SendJSON(const json::Value &json,
                            std::chrono::milliseconds timeout) {
   bool done = false;
-  std::future<void> *future =
-      new std::future<void>(std::async(std::launch::async, [&]() {
-        std::ofstream writer(m_fifo_file, std::ofstream::out);
-        writer << JSONToString(json) << std::endl;
-        done = true;
-      }));
-  if (future->wait_for(timeout) == std::future_status::timeout || !done) {
+  std::future<void> future = std::async(std::launch::async, [&]() {
+    std::ofstream writer(m_fifo_file, std::ofstream::out);
+    writer << JSONToString(json) << std::endl;
+    done = true;
+  });
+  if (future.wait_for(timeout) == std::future_status::timeout || !done) {
     return createStringError(inconvertibleErrorCode(),
                              "Timed out trying to send messages to the " +
                                  m_other_endpoint_name);
   }
-  delete future;
   return Error::success();
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130802.448728.patch
Type: text/x-patch
Size: 2319 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20220729/3a3b709b/attachment-0001.bin>


More information about the lldb-commits mailing list