freeze when returning a component from a function #664
-
I want to have a function that returns a component, I have the function defined as follows: template <typename T>
auto number(T &source) -> ftxui::Component {
using namespace ftxui;
auto decrease = Button("-", [&] { --source; });
auto increase = Button("+", [&] { ++source; });
auto container = Container::Horizontal({decrease, increase});
return Renderer(container, [&] {
return hbox({
decrease->Render(),
text(fmt::format("{}", source)) | center,
increase->Render(),
});
});
} then I have the following snippet in main int main() {
using namespace ftxui;
auto level = std::uint8_t{0};
auto renderer = number(level);
auto screen = ScreenInteractive::Fullscreen();
screen.Loop(renderer);
} but then the application just freezes when I run it, what am I doing wrong here? |
Beta Was this translation helpful? Give feedback.
Answered by
ArthurSonzogni
Jun 4, 2023
Replies: 1 comment 7 replies
-
Hello @uyha-kwz Your lambda captures Your binary suffers from undefined behavior / memory safety issues. Capture the Component (
|
Beta Was this translation helpful? Give feedback.
7 replies
Answer selected by
uyha-kwz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @uyha-kwz
Your lambda captures
decrease
andincrease
declared from the stack by reference. It is not valid using them after the end of the function.Your binary suffers from undefined behavior / memory safety issues.
Capture the Component (
std::shared_ptr
) by value in the lambda. (Not recommanded)