gpt4 book ai didi

c - 为什么在调用计时器的函数中参数具有其他值?

转载 作者:行者123 更新时间:2023-11-30 15:30:43 24 4
gpt4 key购买 nike

我有这段代码:

int gui_createButtonStart(struct GameGUI **gameGUI)
{
.
.
.
g_signal_connect(G_OBJECT((*gameGUI)->buttonStart), "clicked",
G_CALLBACK(gui_clickStart), gameGUI);

return 0;
}

int gui_deleteGameGUI(struct GameGUI **gameGUI)
{
if (gameGUI == NULL || *gameGUI == NULL)
return POINTER_NULL;

free(*gameGUI);
*gameGUI = NULL;

return 0;
}

int gui_closeWhistGame(GtkWidget *windowTable, struct GameGUI **gameGUI)
{
if (windowTable == NULL)
return POINTER_NULL;

gui_deleteGameGUI(gameGUI);
gtk_widget_destroy(windowTable);
gtk_main_quit();

return FUNCTION_NO_ERROR;
}

int gui_clickStart(GtkWidget *button, struct GameGUI **gameGUI)
{
if (gameGUI == NULL)
return POINTER_NULL;
if (*gameGUI == NULL)
return POINTER_NULL;
if ((*gameGUI)->game == NULL)
return GAME_NULL;

gtk_widget_hide(button);
gui_startRound(gameGUI);

return FUNCTION_NO_ERROR;
}

int gui_startRound(struct GameGUI **gameGUI)
{
gui_startTime(gameGUI);

return 0;
}

int gui_startTime(struct GameGUI **gameGUI)
{
if (gameGUI == NULL)
return POINTER_NULL;
if (*gameGUI == NULL)
return POINTER_NULL;
if ((*gameGUI)->limitTimeGUI == NULL)
return POINTER_NULL;

guint interval = (1000 * LIMIT_TIME) / LENGTH_TIME_LINE;
g_timeout_add(interval, (GSourceFunc)gui_timer, (gpointer)gameGUI);

return FUNCTION_NO_ERROR;
}

gboolean gui_timer(gpointer data)
{
struct GameGUI *gameGUI = *((struct GameGUI**)data);

if (gameGUI == NULL)
return FALSE;
}

int main(int argc, char *argv[])
{
struct GameGUI *gameGUI = gui_createGameGUI();
GtkWidget *window;
.
.
.
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gui_closeWhistGame), &gameGUI);
gtk_main();
return 0;
}

这段代码取 self 的一个项目。当我运行代码时,屏幕上会出现一个按钮。当我按下他时,调用 gui_clickStart() 并启动计时器。当我按“x”时,窗口将关闭(但我有多个打开的窗口)并且 *gameGUI 将收到 NULL。问题是在计时器 *gameGUI 中并不总是 NULL,有时是 NULL,有时不是 NULL。为什么?

最佳答案

您应该在窗口关闭时终止超时。

g_timeout_add 返回一个 guint32,它是源 ID。您可以使用它来删除 gui_deleteGameGUI 函数中的超时。类似的东西

int gui_deleteGameGUI(struct GameGUI **gameGUI)
{
if (gameGUI == NULL || *gameGUI == NULL)
return POINTER_NULL;

g_source_remove((*gameGUI)->gameTimer);
(*gameGUI)->gameTimer = 0;

free(*gameGUI);
*gameGUI = NULL;

return 0;
}

假设您将其存储在 gui_startTime 函数中。

guint interval = (1000 * LIMIT_TIME) / LENGTH_TIME_LINE;
(*gameGUI)->gameTimer = g_timeout_add(interval, (GSourceFunc)gui_timer, (gpointer)gameGUI);

关于c - 为什么在调用计时器的函数中参数具有其他值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25444389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com