Skip to content

Commit 24df7bc

Browse files
committed
Cleaned up timers in uiUninit() on GTK+. Update #395.
1 parent ad1641f commit 24df7bc

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

unix/main.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
uiInitOptions uiprivOptions;
55

6+
static GHashTable *timers;
7+
68
const char *uiInit(uiInitOptions *o)
79
{
810
GError *err = NULL;
@@ -16,11 +18,21 @@ const char *uiInit(uiInitOptions *o)
1618
}
1719
uiprivInitAlloc();
1820
uiprivLoadFutures();
21+
timers = g_hash_table_new(g_direct_hash, g_direct_equal);
1922
return NULL;
2023
}
2124

25+
struct timer; // TODO get rid of forward declaration
26+
27+
static void uninitTimer(gpointer key, gpointer value, gpointer data)
28+
{
29+
uiprivFree((struct timer *) key);
30+
}
31+
2232
void uiUninit(void)
2333
{
34+
g_hash_table_foreach(timers, uninitTimer, NULL);
35+
g_hash_table_destroy(timers);
2436
uiprivUninitMenus();
2537
uiprivUninitAlloc();
2638
}
@@ -108,27 +120,29 @@ void uiQueueMain(void (*f)(void *data), void *data)
108120
}
109121

110122
struct timer {
111-
int (*f)(void *);
112-
void *data;
123+
int (*f)(void *);
124+
void *data;
113125
};
114126

115127
static gboolean doTimer(gpointer data)
116128
{
117-
struct timer *t = (struct timer *) data;
129+
struct timer *t = (struct timer *) data;
118130

119-
if (!(*(t->f))(t->data)) {
120-
uiprivFree(t);
121-
return FALSE;
122-
}
131+
if (!(*(t->f))(t->data)) {
132+
g_hash_table_remove(timers, t);
133+
uiprivFree(t);
134+
return FALSE;
135+
}
123136
return TRUE;
124137
}
125138

126139
void uiTimer(int milliseconds, int (*f)(void *data), void *data)
127140
{
128-
struct timer *t;
141+
struct timer *t;
129142

130-
t = uiprivNew(struct timer);
131-
t->f = f;
132-
t->data = data;
133-
g_timeout_add(milliseconds, doTimer, t);
143+
t = uiprivNew(struct timer);
144+
t->f = f;
145+
t->data = data;
146+
g_timeout_add(milliseconds, doTimer, t);
147+
g_hash_table_add(timers, t);
134148
}

windows/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ void uiprivFreeTimer(uiprivTimer *t)
150150
uiprivFree(t);
151151
}
152152

153+
// since timers use uiprivAlloc(), we have to clean them up in uiUninit(), or else we'll get dangling allocation errors
153154
void uiprivUninitTimers(void)
154155
{
155156
// TODO why doesn't auto t : timers work?

0 commit comments

Comments
 (0)