-
Notifications
You must be signed in to change notification settings - Fork 226
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
selection menu crash (use after free) #147
Comments
This is not an MCVE is it? It seems the button (make_button and related code) can be removed? |
I think the issue here is the lifetime of the menu item strings. You are responsible in making them 'alive' in the duration of the menu. |
Yes, confirmed. You need to hold the vector of strings in the entire duration of the menu. The selection menu does not hold the strings internally. |
This is the corrected code: #include <elements.hpp>
using namespace cycfi::elements;
// Main window background color
auto constexpr bkd_color = rgba(35, 35, 37, 255);
auto background = box(bkd_color);
struct user_state
{
std::string menu_item;
};
template <typename Subject>
proxy<cycfi::remove_cvref_t<Subject>>
make_proxy(Subject&& subject)
{
return { std::forward<Subject>(subject) };
}
auto make_menu(const std::vector<std::string>& names, user_state& state)
{
return selection_menu(
[&state](std::string_view selected)
{ state.menu_item = selected; },
names).first;
}
auto make_elements(view& view, user_state& state, std::vector<std::string> const& names)
{
auto m = share(make_proxy(make_menu(names, state)));
return m;
}
int main(int argc, char* argv[])
{
app _app(argc, argv);
window _win(_app.name());
_win.on_close = [&_app]() { _app.stop(); };
user_state state;
std::vector<std::string> names = { "One", "Two" };
view view_(_win);
view_.content(
make_elements(view_, state, names),
background
);
_app.run();
return 0;
} |
Yes, I just wanted to make something compileable fast, did not realize I could strip more code.
That's ... unintuitive. This is the first element I encounter that does not keep the lifetime of used strings. Anyway, I'm currently doing other things but I'm gathering information for an issue about menus - I think there is a lot to improve on their API and factories. |
This is not a basic element. This is a gallery composition. Moreover, we want to keep element compositions as light as possible. IMO, this is simply a matter of documentation. |
Actually, I take that back... You might be right. The menu items should be holding the strings already in the labels. I'm re-opening this for further investigation. |
OK, I have a fix :-) I'll push later... Thanks you for your attention to detail! |
Another small thing: if the supplied sequence is empty, some actions on the created menu crash (no elements/lib/src/element/gallery/menu.cpp Lines 87 to 103 in a0dd1e6
And btw, I think |
items ( |
Fixed in develop.
Fixed this as well. |
Please close after you verify the fix. Also, please check the "Menus" example. I think I am getting an ASAN false positive similar to this: google/sanitizers#362. I need to investigate my ASAN setup. |
Both my MCVE and the menu example work, on Windows and Linux. No ASAN problems. |
Thank you, @Xeverous. And thanks for your patience! |
MCVE:
My suspection - the selector is capturing string view only but the provided strings are freed when
make_menu
function returnselements/lib/src/element/gallery/menu.cpp
Line 93 in a0dd1e6
elements/lib/include/elements/element/gallery/menu.hpp
Lines 147 to 151 in a0dd1e6
The text was updated successfully, but these errors were encountered: