Skip to content

Commit 5fa5489

Browse files
committed
Cleaning up memory management
1 parent 0e9a7c1 commit 5fa5489

File tree

7 files changed

+97
-12
lines changed

7 files changed

+97
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.*
22
!.gitignore
33
!.travis.yml
4+
!.valgrind.supp

.valgrind.supp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
libasound1
3+
Memcheck:Cond
4+
obj:/usr/lib/x86_64-linux-gnu/libasound.so.2.0.0
5+
}
6+
7+
{
8+
libasound2
9+
Memcheck:Param
10+
ioctl(generic)
11+
fun:ioctl
12+
obj:/usr/lib/x86_64-linux-gnu/libasound.so.2.0.0
13+
}

Makefile

+13-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ifneq ($(shell uname -s),Darwin)
99
endif
1010

1111
PACKAGES := gtk+-3.0 portaudio-2.0 fftw3f
12-
CFLAGS += -Wall -O3 -ffast-math -DVERSION='"$(VERSION)"' `pkg-config --cflags $(PACKAGES)`
12+
CFLAGS += -Wall -ffast-math -DVERSION='"$(VERSION)"' `pkg-config --cflags $(PACKAGES)`
1313
LDFLAGS += -lm -lpthread `pkg-config --libs $(PACKAGES)`
1414

1515
SRCDIR := src
@@ -40,6 +40,10 @@ test: $(BUILDDIR)/tg-dbg$(EXT)
4040
$(BUILDDIR)/tg-dbg test
4141
.PHONY: test
4242

43+
valgrind: $(BUILDDIR)/tg-vlg$(EXT)
44+
valgrind --leak-check=full -v --num-callers=99 --suppressions=.valgrind.supp $(BUILDDIR)/tg-vlg$(EXT)
45+
.PHONY: debug
46+
4347
$(BUILDDIR)/tg-timer.res: icons/tg-timer.rc icons/tg-timer.ico
4448
windres icons/tg-timer.rc -O coff -o $(BUILDDIR)/tg-timer.res
4549

@@ -54,12 +58,14 @@ ifeq ($(4),strip)
5458
endif
5559
endef
5660

57-
$(eval $(call TARGET,tg,,,strip))
58-
$(eval $(call TARGET,tg-lt,-DLIGHT,,strip))
59-
$(eval $(call TARGET,tg-dbg,-ggdb -DDEBUG,$(DEBUG_LDFLAGS),))
60-
$(eval $(call TARGET,tg-lt-dbg,-ggdb -DDEBUG -DLIGHT,$(DEBUG_LDFLAGS),))
61-
$(eval $(call TARGET,tg-prf,-pg,,))
62-
$(eval $(call TARGET,tg-lt-prf,-DLIGHT -pg,,))
61+
$(eval $(call TARGET,tg,-O3,,strip))
62+
$(eval $(call TARGET,tg-lt,-O3 -DLIGHT,,strip))
63+
$(eval $(call TARGET,tg-dbg,-O3 -ggdb -DDEBUG,$(DEBUG_LDFLAGS),))
64+
$(eval $(call TARGET,tg-lt-dbg,-O3 -ggdb -DDEBUG -DLIGHT,$(DEBUG_LDFLAGS),))
65+
$(eval $(call TARGET,tg-prf,-O3 -pg,,))
66+
$(eval $(call TARGET,tg-lt-prf,-O3 -DLIGHT -pg,,))
67+
$(eval $(call TARGET,tg-vlg,-O1 -g,,))
68+
$(eval $(call TARGET,tg-vlg-lt,-O1 -g -DLIGHT,,))
6369

6470
ICONSIZES := $(foreach SIZE, $(shell cat icons/sizes), $(SIZE)x$(SIZE))
6571

src/algo.c

+36-1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,34 @@ void setup_buffers(struct processing_buffers *b)
103103
#endif
104104
}
105105

106+
void pb_destroy(struct processing_buffers *b)
107+
{
108+
fftwf_free(b->samples);
109+
free(b->samples_sc);
110+
free(b->waveform);
111+
free(b->waveform_sc);
112+
fftwf_free(b->fft);
113+
fftwf_free(b->sc_fft);
114+
fftwf_free(b->tic_wf);
115+
fftwf_free(b->slice_wf);
116+
fftwf_free(b->tic_fft);
117+
fftwf_free(b->slice_fft);
118+
free(b->tic_c);
119+
fftwf_destroy_plan(b->plan_a);
120+
fftwf_destroy_plan(b->plan_b);
121+
fftwf_destroy_plan(b->plan_c);
122+
fftwf_destroy_plan(b->plan_d);
123+
fftwf_destroy_plan(b->plan_e);
124+
fftwf_destroy_plan(b->plan_f);
125+
fftwf_destroy_plan(b->plan_g);
126+
free(b->hpf);
127+
free(b->lpf);
128+
free(b->events);
129+
#ifdef DEBUG
130+
fftwf_free(b->debug);
131+
#endif
132+
}
133+
106134
struct processing_buffers *pb_clone(struct processing_buffers *p)
107135
{
108136
struct processing_buffers *new = malloc(sizeof(struct processing_buffers));
@@ -659,6 +687,13 @@ void setup_cal_data(struct calibration_data *cd)
659687
cd->events = malloc(cd->size * sizeof(uint64_t));
660688
}
661689

690+
void cal_data_destroy(struct calibration_data *cd)
691+
{
692+
free(cd->times);
693+
free(cd->phases);
694+
free(cd->events);
695+
}
696+
662697
int add_sample_cal(struct processing_buffers *p, struct calibration_data *cd)
663698
{
664699
int i;
@@ -732,7 +767,7 @@ void process(struct processing_buffers *p, int bph, double la)
732767
{
733768
prepare_data(p,1);
734769
p->ready = !compute_period(p,bph);
735-
if(p->period >= p->sample_rate / 2) {
770+
if(p->ready && p->period >= p->sample_rate / 2) {
736771
debug("Detected period too long\n");
737772
p->ready = 0;
738773
}

src/computer.c

+19
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,28 @@ void *computing_thread(void *void_computer)
211211
}
212212

213213
debug("Terminating computation thread\n");
214+
214215
return NULL;
215216
}
216217

218+
void computer_destroy(struct computer *c)
219+
{
220+
int i;
221+
for(i=0; i<NSTEPS; i++)
222+
pb_destroy(&c->pdata->buffers[i]);
223+
free(c->pdata->buffers);
224+
free(c->pdata);
225+
cal_data_destroy(c->cdata);
226+
free(c->cdata);
227+
snapshot_destroy(c->actv);
228+
if(c->curr)
229+
snapshot_destroy(c->curr);
230+
pthread_mutex_destroy(&c->mutex);
231+
pthread_cond_destroy(&c->cond);
232+
pthread_join(c->thread, NULL);
233+
free(c);
234+
}
235+
217236
struct computer *start_computer(int nominal_sr, int bph, double la, int cal)
218237
{
219238
struct processing_buffers *p = malloc(NSTEPS * sizeof(struct processing_buffers));

src/interface.c

+9-4
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ void computer_quit(void *w)
179179

180180
gboolean quit(struct main_window *w)
181181
{
182+
g_source_remove(w->kick_timeout);
183+
g_source_remove(w->save_timeout);
182184
lock_computer(w->computer);
183185
w->computer->recompute = -1;
184186
w->computer->callback = computer_quit;
@@ -590,8 +592,8 @@ int start_interface(GtkApplication* app, void *p)
590592

591593
init_main_window(w);
592594

593-
g_timeout_add_full(G_PRIORITY_LOW,100,(GSourceFunc)kick_computer,w,NULL);
594-
g_timeout_add_full(G_PRIORITY_LOW,10000,(GSourceFunc)save_on_change_timer,w,NULL);
595+
w->kick_timeout = g_timeout_add_full(G_PRIORITY_LOW,100,(GSourceFunc)kick_computer,w,NULL);
596+
w->save_timeout = g_timeout_add_full(G_PRIORITY_LOW,10000,(GSourceFunc)save_on_change_timer,w,NULL);
595597
#ifdef DEBUG
596598
if(testing)
597599
g_timeout_add_full(G_PRIORITY_LOW,3000,(GSourceFunc)quit,w,NULL);
@@ -605,9 +607,12 @@ int start_interface(GtkApplication* app, void *p)
605607
void on_shutdown(GApplication *app, void *p)
606608
{
607609
debug("Main loop has terminated\n");
608-
save_config(g_object_get_data(G_OBJECT(app), "main-window"));
610+
struct main_window *w = g_object_get_data(G_OBJECT(app), "main-window");
611+
save_config(w);
612+
computer_destroy(w->computer);
613+
op_destroy(w->active_panel);
614+
free(w);
609615
terminate_portaudio();
610-
// We leak the main_window structure
611616
}
612617

613618
int main(int argc, char **argv)

src/tg.h

+6
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ struct calibration_data {
105105
};
106106

107107
void setup_buffers(struct processing_buffers *b);
108+
void pb_destroy(struct processing_buffers *b);
108109
struct processing_buffers *pb_clone(struct processing_buffers *p);
109110
void pb_destroy_clone(struct processing_buffers *p);
110111
void process(struct processing_buffers *p, int bph, double la);
111112
void setup_cal_data(struct calibration_data *cd);
113+
void cal_data_destroy(struct calibration_data *cd);
112114
int test_cal(struct processing_buffers *p);
113115
int process_cal(struct processing_buffers *p, struct calibration_data *cd);
114116

@@ -180,6 +182,7 @@ struct computer {
180182

181183
struct snapshot *snapshot_clone(struct snapshot *s);
182184
void snapshot_destroy(struct snapshot *s);
185+
void computer_destroy(struct computer *c);
183186
struct computer *start_computer(int nominal_sr, int bph, double la, int cal);
184187
void lock_computer(struct computer *c);
185188
void unlock_computer(struct computer *c);
@@ -237,6 +240,9 @@ struct main_window {
237240
GKeyFile *config_file;
238241
gchar *config_file_name;
239242
struct conf_data *conf_data;
243+
244+
guint kick_timeout;
245+
guint save_timeout;
240246
};
241247

242248
extern int preset_bph[];

0 commit comments

Comments
 (0)