在 GTK 中向操作添加有效的键盘加速器时,为什么我的应用程序会崩溃?

问题描述 投票:0回答:1

当我使用

"<Ctrl>n"
将键盘加速器(例如
gtk_application_set_accels_for_action
)添加到 GTK 中的操作时,应用程序立即崩溃:

// "app" & "handler" are defined here

const char *action_id = "action";
const char *full_id = "app.action";
// Remove "Ctrl" ({ <>n })
// or add an invalid shortcut ({ "<Ctrl>n", "<>f" })
// and the app doesn't crash.
const char *keyboard_shortcuts[] = { "<Ctrl>n" };

GSimpleAction *action = g_simple_action_new (action_id, NULL);
g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action));
g_signal_connect (action, "activate", G_CALLBACK (handler), NULL);
// Remove the following line and the app doesn't crash.
gtk_application_set_accels_for_action (GTK_APPLICATION (app), full_id, keyboard_shortcuts);

每当我将键盘快捷键更改为无效的内容(例如

"<>n"
)或向数组添加无效的键盘快捷键(
{ "<Ctrl>n", "<>f" }
)时,应用程序都会按预期运行,但键盘快捷键不起作用。

这里是展示问题的最小应用程序的代码。

c gtk gnome
1个回答
0
投票

gtk_application_set_accels_for_action()
要求将加速器作为指向字符串的指针以空终止数组的形式给出。你没能做到这一点。有充分的理由认为结果将是 GTK 超出数组的范围,从而产生未定义的行为。您似乎想要这个:

const char *keyboard_shortcuts[] = { "<Ctrl>n", NULL };
© www.soinside.com 2019 - 2024. All rights reserved.