Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sub menu example in README segmentation fault #240

Closed
lu-maca opened this issue Apr 29, 2024 · 0 comments
Closed

Sub menu example in README segmentation fault #240

lu-maca opened this issue Apr 29, 2024 · 0 comments

Comments

@lu-maca
Copy link
Contributor

lu-maca commented Apr 29, 2024

Hi,

I'm having some trouble with the example in README. This compiles correctly but gives a segfault when executed.

#include "cli/cli.h"
#include "cli/clifilesession.h"

using namespace cli;
using namespace std;

int main() {
  // setup cli

  auto rootMenu = make_unique<Menu>("myprompt");

  auto menuA = make_unique<Menu>("a_prompt");
  rootMenu->Insert(std::move(menuA));

  auto menuAA = make_unique<Menu>("aa_prompt");
  menuA->Insert(std::move(menuAA));

  auto menuAB = make_unique<Menu>("ab_prompt");
  menuA->Insert(std::move(menuAB));

  auto menuAC = make_unique<Menu>("ac_prompt");
  menuA->Insert(std::move(menuAC));

  auto menuACA = make_unique<Menu>("aca_prompt");
  menuAC->Insert(std::move(menuACA));

  auto menuB = make_unique<Menu>("b_prompt");
  rootMenu->Insert(std::move(menuB));

  auto menuBA = make_unique<Menu>("ba_prompt");
  menuB->Insert(std::move(menuBA));

  auto menuBB = make_unique<Menu>("bb_prompt");
  menuB->Insert(std::move(menuBB));

  Cli cli(std::move(rootMenu));
  // global exit action
  cli.ExitAction(
      [](auto &out) { out << "Goodbye and thanks for all the fish.\n"; });

  CliFileSession input(cli);
  input.Start();

  return 0;
}

A way of making it work is the following:

int main() {
  // setup cli

  auto rootMenu = make_unique<Menu>("myprompt");

  auto menuA = make_unique<Menu>("a_prompt");
  auto menuAA = make_unique<Menu>("aa_prompt");
  auto menuAB = make_unique<Menu>("ab_prompt");
  auto menuAC = make_unique<Menu>("ac_prompt");
  auto menuACA = make_unique<Menu>("aca_prompt");

  auto menuB = make_unique<Menu>("b_prompt");
  auto menuBA = make_unique<Menu>("ba_prompt");
  auto menuBB = make_unique<Menu>("bb_prompt");
  
  menuAC->Insert(std::move(menuACA));
  
  menuB->Insert(std::move(menuBB));
  menuB->Insert(std::move(menuBA));

  menuA->Insert(std::move(menuAA));
  menuA->Insert(std::move(menuAB));
  menuA->Insert(std::move(menuAC));

  rootMenu->Insert(std::move(menuA));
  rootMenu->Insert(std::move(menuB));

  Cli cli(std::move(rootMenu));
  // global exit action
  cli.ExitAction(
      [](auto &out) { out << "Goodbye and thanks for all the fish.\n"; });

  CliFileSession input(cli);
  input.Start();

  return 0;
}

I'm not sure why, unfortunately I don't have much time to study the problem. Hope this can help.

Thanks for you work,
Luca

Repository owner locked and limited conversation to collaborators Apr 29, 2024
@daniele77 daniele77 converted this issue into discussion #241 Apr 29, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant