diff options
author | Jarryd Beck <jarro.2783@gmail.com> | 2022-07-11 10:02:22 +0300 |
---|---|---|
committer | Jarryd Beck <jarro.2783@gmail.com> | 2022-07-11 10:02:22 +0300 |
commit | fbd79df66bde4f070ff488d8716870667736600f (patch) | |
tree | 29655b04b3ba695553b0ea445ead2bc09ed60ba2 | |
parent | 6bb1e82b6132bd290313851d352b6d02ac81cb44 (diff) |
Fixes #336
Memory leak was caused by `exit`.
-rw-r--r-- | src/example.cpp | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/src/example.cpp b/src/example.cpp index b06d806..322425b 100644 --- a/src/example.cpp +++ b/src/example.cpp @@ -21,17 +21,18 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include "cxxopts.hpp" #include <iostream> +#include <memory> -#include "cxxopts.hpp" - -void +int parse(int argc, const char* argv[]) { try { - cxxopts::Options options(argv[0], " - example command line options"); + std::unique_ptr<cxxopts::Options> allocated(new cxxopts::Options(argv[0], " - example command line options")); + auto& options = *allocated; options .positional_help("[optional args]") .show_positional_help(); @@ -82,7 +83,7 @@ parse(int argc, const char* argv[]) if (result.count("help")) { std::cout << options.help({"", "Group"}) << std::endl; - exit(0); + return true; } if(result.count("list")) @@ -98,7 +99,7 @@ parse(int argc, const char* argv[]) { std::cout << result.arguments_string() << std::endl; } - exit(0); + return true; } if (apple) @@ -184,13 +185,18 @@ parse(int argc, const char* argv[]) catch (const cxxopts::OptionException& e) { std::cout << "error parsing options: " << e.what() << std::endl; - exit(1); + return false; } + + return true; } int main(int argc, const char* argv[]) { - parse(argc, argv); + if (!parse(argc, argv)) + { + return 1; + } return 0; } |