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(); }
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(); }
_______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits