Commit fbd79df66bde4f070ff488d8716870667736600f
1 parent
6bb1e82b
Fixes #336
Memory leak was caused by `exit`.
Showing
1 changed file
with
14 additions
and
8 deletions
src/example.cpp
| ... | ... | @@ -21,17 +21,18 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | 21 | THE SOFTWARE. |
| 22 | 22 | |
| 23 | 23 | */ |
| 24 | +#include "cxxopts.hpp" | |
| 24 | 25 | |
| 25 | 26 | #include <iostream> |
| 27 | +#include <memory> | |
| 26 | 28 | |
| 27 | -#include "cxxopts.hpp" | |
| 28 | - | |
| 29 | -void | |
| 29 | +int | |
| 30 | 30 | parse(int argc, const char* argv[]) |
| 31 | 31 | { |
| 32 | 32 | try |
| 33 | 33 | { |
| 34 | - cxxopts::Options options(argv[0], " - example command line options"); | |
| 34 | + std::unique_ptr<cxxopts::Options> allocated(new cxxopts::Options(argv[0], " - example command line options")); | |
| 35 | + auto& options = *allocated; | |
| 35 | 36 | options |
| 36 | 37 | .positional_help("[optional args]") |
| 37 | 38 | .show_positional_help(); |
| ... | ... | @@ -82,7 +83,7 @@ parse(int argc, const char* argv[]) |
| 82 | 83 | if (result.count("help")) |
| 83 | 84 | { |
| 84 | 85 | std::cout << options.help({"", "Group"}) << std::endl; |
| 85 | - exit(0); | |
| 86 | + return true; | |
| 86 | 87 | } |
| 87 | 88 | |
| 88 | 89 | if(result.count("list")) |
| ... | ... | @@ -98,7 +99,7 @@ parse(int argc, const char* argv[]) |
| 98 | 99 | { |
| 99 | 100 | std::cout << result.arguments_string() << std::endl; |
| 100 | 101 | } |
| 101 | - exit(0); | |
| 102 | + return true; | |
| 102 | 103 | } |
| 103 | 104 | |
| 104 | 105 | if (apple) |
| ... | ... | @@ -184,13 +185,18 @@ parse(int argc, const char* argv[]) |
| 184 | 185 | catch (const cxxopts::OptionException& e) |
| 185 | 186 | { |
| 186 | 187 | std::cout << "error parsing options: " << e.what() << std::endl; |
| 187 | - exit(1); | |
| 188 | + return false; | |
| 188 | 189 | } |
| 190 | + | |
| 191 | + return true; | |
| 189 | 192 | } |
| 190 | 193 | |
| 191 | 194 | int main(int argc, const char* argv[]) |
| 192 | 195 | { |
| 193 | - parse(argc, argv); | |
| 196 | + if (!parse(argc, argv)) | |
| 197 | + { | |
| 198 | + return 1; | |
| 199 | + } | |
| 194 | 200 | |
| 195 | 201 | return 0; |
| 196 | 202 | } | ... | ... |