diff options
author | Kyle Edwards <kyle.edwards@kitware.com> | 2023-08-21 18:41:24 (GMT) |
---|---|---|
committer | Kyle Edwards <kyle.edwards@kitware.com> | 2023-08-25 14:44:06 (GMT) |
commit | 27be5ccd45500c055288d4d4cfcdb675eca38fee (patch) | |
tree | 1636824cff9b562a33c7bccef14225db5e1a8450 /Tests/CMakeLib/testUVStreambuf.cxx | |
parent | 5ae0030e90df07e56dc16b0210bd334650dcc9d4 (diff) | |
download | CMake-27be5ccd45500c055288d4d4cfcdb675eca38fee.zip CMake-27be5ccd45500c055288d4d4cfcdb675eca38fee.tar.gz CMake-27be5ccd45500c055288d4d4cfcdb675eca38fee.tar.bz2 |
cmUVStreamRead: Return RAII handle to avoid memory leak
Diffstat (limited to 'Tests/CMakeLib/testUVStreambuf.cxx')
-rw-r--r-- | Tests/CMakeLib/testUVStreambuf.cxx | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/Tests/CMakeLib/testUVStreambuf.cxx b/Tests/CMakeLib/testUVStreambuf.cxx index f3977d4..af06a8e 100644 --- a/Tests/CMakeLib/testUVStreambuf.cxx +++ b/Tests/CMakeLib/testUVStreambuf.cxx @@ -503,7 +503,7 @@ bool testUVStreamRead() std::string output; bool finished = false; - cmUVStreamRead( + auto handle = cmUVStreamRead( pipeSource, [&output](std::vector<char> data) { cm::append(output, data); }, [&output, &finished]() { @@ -524,6 +524,55 @@ bool testUVStreamRead() return true; } +bool testUVStreamReadLeak() +{ + int pipe[] = { -1, -1 }; + if (cmGetPipes(pipe) < 0) { + std::cout << "cmGetPipes() returned an error" << std::endl; + return false; + } + + cm::uv_loop_ptr loop; + loop.init(); + cm::uv_pipe_ptr pipeSink; + pipeSink.init(*loop, 0); + uv_pipe_open(pipeSink, pipe[1]); + + std::string str = "Hello world!"; + uv_write_t writeReq; + uv_buf_t buf; + buf.base = &str.front(); + buf.len = str.length(); + uv_write(&writeReq, pipeSink, &buf, 1, nullptr); + uv_run(loop, UV_RUN_DEFAULT); + pipeSink.reset(); + + cm::uv_pipe_ptr pipeSource; + pipeSource.init(*loop, 0); + uv_pipe_open(pipeSource, pipe[0]); + + std::string output; + bool finished = false; + auto handle = cmUVStreamRead( + pipeSource, + [&output](std::vector<char> data) { cm::append(output, data); }, + [&output, &finished]() { + if (output != "Hello world!") { + std::cout << "Output was \"" << output + << "\", should be \"Hello world!\"" << std::endl; + return; + } + finished = true; + }); + + if (finished) { + std::cout << "finished was set" << std::endl; + return false; + } + + return true; +} + int testUVStreambuf(int argc, char** const argv) { if (argc < 2) { @@ -547,7 +596,12 @@ int testUVStreambuf(int argc, char** const argv) } if (!testUVStreamRead()) { - std::cout << "While executing testUVPipeIStream().\n"; + std::cout << "While executing testUVStreamRead().\n"; + return -1; + } + + if (!testUVStreamReadLeak()) { + std::cout << "While executing testUVStreamReadLeak().\n"; return -1; } |