Cleaned up timers in uiUninit() on GTK+. Update #395.
This commit is contained in:
parent
ad1641f9ab
commit
24df7bc3f2
38
unix/main.c
38
unix/main.c
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
uiInitOptions uiprivOptions;
|
uiInitOptions uiprivOptions;
|
||||||
|
|
||||||
|
static GHashTable *timers;
|
||||||
|
|
||||||
const char *uiInit(uiInitOptions *o)
|
const char *uiInit(uiInitOptions *o)
|
||||||
{
|
{
|
||||||
GError *err = NULL;
|
GError *err = NULL;
|
||||||
|
@ -16,11 +18,21 @@ const char *uiInit(uiInitOptions *o)
|
||||||
}
|
}
|
||||||
uiprivInitAlloc();
|
uiprivInitAlloc();
|
||||||
uiprivLoadFutures();
|
uiprivLoadFutures();
|
||||||
|
timers = g_hash_table_new(g_direct_hash, g_direct_equal);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct timer; // TODO get rid of forward declaration
|
||||||
|
|
||||||
|
static void uninitTimer(gpointer key, gpointer value, gpointer data)
|
||||||
|
{
|
||||||
|
uiprivFree((struct timer *) key);
|
||||||
|
}
|
||||||
|
|
||||||
void uiUninit(void)
|
void uiUninit(void)
|
||||||
{
|
{
|
||||||
|
g_hash_table_foreach(timers, uninitTimer, NULL);
|
||||||
|
g_hash_table_destroy(timers);
|
||||||
uiprivUninitMenus();
|
uiprivUninitMenus();
|
||||||
uiprivUninitAlloc();
|
uiprivUninitAlloc();
|
||||||
}
|
}
|
||||||
|
@ -108,27 +120,29 @@ void uiQueueMain(void (*f)(void *data), void *data)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct timer {
|
struct timer {
|
||||||
int (*f)(void *);
|
int (*f)(void *);
|
||||||
void *data;
|
void *data;
|
||||||
};
|
};
|
||||||
|
|
||||||
static gboolean doTimer(gpointer data)
|
static gboolean doTimer(gpointer data)
|
||||||
{
|
{
|
||||||
struct timer *t = (struct timer *) data;
|
struct timer *t = (struct timer *) data;
|
||||||
|
|
||||||
if (!(*(t->f))(t->data)) {
|
if (!(*(t->f))(t->data)) {
|
||||||
uiprivFree(t);
|
g_hash_table_remove(timers, t);
|
||||||
return FALSE;
|
uiprivFree(t);
|
||||||
}
|
return FALSE;
|
||||||
|
}
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void uiTimer(int milliseconds, int (*f)(void *data), void *data)
|
void uiTimer(int milliseconds, int (*f)(void *data), void *data)
|
||||||
{
|
{
|
||||||
struct timer *t;
|
struct timer *t;
|
||||||
|
|
||||||
t = uiprivNew(struct timer);
|
t = uiprivNew(struct timer);
|
||||||
t->f = f;
|
t->f = f;
|
||||||
t->data = data;
|
t->data = data;
|
||||||
g_timeout_add(milliseconds, doTimer, t);
|
g_timeout_add(milliseconds, doTimer, t);
|
||||||
|
g_hash_table_add(timers, t);
|
||||||
}
|
}
|
||||||
|
|
|
@ -150,6 +150,7 @@ void uiprivFreeTimer(uiprivTimer *t)
|
||||||
uiprivFree(t);
|
uiprivFree(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// since timers use uiprivAlloc(), we have to clean them up in uiUninit(), or else we'll get dangling allocation errors
|
||||||
void uiprivUninitTimers(void)
|
void uiprivUninitTimers(void)
|
||||||
{
|
{
|
||||||
// TODO why doesn't auto t : timers work?
|
// TODO why doesn't auto t : timers work?
|
||||||
|
|
Loading…
Reference in New Issue