Skip to content

Commit 9566be0

Browse files
committed
fix: leaks (#70)
\+ remove temp files - disconnect props signals manually - inline some thief functions (still don't get why they leak, maybe we should move to temp objects? they run in another thread anyway) - clear player when destroying window - remove running timeout from marquee when destroying - fix animation callback target preventing widgets from being destroyed by moving to dummy property based callbacks Reviewed-on: https://codeberg.org/GeopJr/Turntable/pulls/70
1 parent 56b7d11 commit 9566be0

File tree

8 files changed

+52
-23
lines changed

8 files changed

+52
-23
lines changed

src/Mpris/Entry.vala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ public class Turntable.Mpris.Entry : GLib.Object {
163163
users -= 1;
164164
if (users > 0) return;
165165

166+
this.props.properties_changed.disconnect (on_props_changed);
166167
this.props = null;
167168
this.player = null;
168169
}

src/Utils/Thief.vala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public class Turntable.Utils.Thief : GLib.Object {
130130
}
131131
}
132132

133-
private static void make_histogram_and_vbox (Gdk.Pixbuf pixbuf, out int[] histogram, out VBox vbox) {
133+
private static inline void make_histogram_and_vbox (Gdk.Pixbuf pixbuf, out int[] histogram, out VBox vbox) {
134134
histogram = new int[HISTOGRAM_SIZE];
135135

136136
uint8 r_min = uint8.MAX;
@@ -253,7 +253,7 @@ public class Turntable.Utils.Thief : GLib.Object {
253253
return cut (axis, ref vbox, histogram, partial_sum, look_ahead_sum, total, out vbox1, out vbox2);
254254
}
255255

256-
private static bool cut (ColorChannel axis, ref VBox vbox, int[] histogram, int[] partial_sum, int[] look_ahead_sum, int total, out VBox? vbox1, out VBox? vbox2) {
256+
private static inline bool cut (ColorChannel axis, ref VBox vbox, int[] histogram, int[] partial_sum, int[] look_ahead_sum, int total, out VBox? vbox1, out VBox? vbox2) {
257257
vbox1 = null;
258258
vbox2 = null;
259259

src/Views/Window.vala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class Turntable.Views.Window : Adw.ApplicationWindow {
1717
public string uuid { get; private set; }
1818

1919
~Window () {
20+
update_player (null);
2021
debug ("Destroying: %s", uuid);
2122
}
2223

src/Widgets/ControlsOverlay.vala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ public class Turntable.Widgets.ControlsOverlay : Adw.Bin {
33
public Mpris.Entry? last_player { get; set; default = null; }
44

55
~ControlsOverlay () {
6-
debug ("[ControlsOverlay] Destroying");
6+
debug ("Destroying");
77
}
88

99
private void update_style (Widgets.Cover.Style style, Gtk.Orientation orientation) {
@@ -314,7 +314,7 @@ public class Turntable.Widgets.ControlsOverlay : Adw.Bin {
314314
}
315315

316316
private void selection_changed () {
317-
debug ("[ControlsOverlay] Changed player");
317+
debug ("Changed player");
318318
bool was_null = this.last_player == null;
319319

320320
if (client_dropdown.selected == Gtk.INVALID_LIST_POSITION) {

src/Widgets/Cover.vala

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ public class Turntable.Widgets.Cover : Gtk.Widget {
244244
};
245245
var in_stream = session.send (new Soup.Message ("GET", this.file_path), cancellable);
246246
pixbuf = new Gdk.Pixbuf.from_stream (in_stream, cancellable);
247+
in_stream.close ();
247248
} else {
248249
pixbuf = new Gdk.Pixbuf.from_file (clean_path);
249250
}
@@ -304,17 +305,19 @@ public class Turntable.Widgets.Cover : Gtk.Widget {
304305
return;
305306
}
306307

307-
File tmp;
308+
File? tmp = null;
308309
try {
309310
FileIOStream ios;
310311
tmp = File.new_tmp (null, out ios);
311312
this.texture.save_to_png (tmp.get_path ());
312313
} catch {
314+
delete_tmp_file (tmp);
313315
stop_it (false);
314316
return;
315317
}
316318

317319
if (cancellable.is_cancelled ()) {
320+
delete_tmp_file (tmp);
318321
stop_it (false);
319322
return;
320323
}
@@ -325,6 +328,17 @@ public class Turntable.Widgets.Cover : Gtk.Widget {
325328
} catch (Error e) {
326329
debug ("Couldn't get pixbuf from temp file: %s", e.message);
327330
stop_it (false);
331+
} finally {
332+
delete_tmp_file (tmp);
333+
}
334+
}
335+
336+
private inline void delete_tmp_file (File? file) {
337+
if (file == null) return;
338+
try {
339+
file.delete ();
340+
} catch (Error e) {
341+
warning (@"Couldn't delete temp file: $(e.code) $(e.message)");
328342
}
329343
}
330344

@@ -448,17 +462,20 @@ public class Turntable.Widgets.Cover : Gtk.Widget {
448462
set_accessible_role (Gtk.AccessibleRole.NONE); // it's probably better if it doesn't get announced
449463
}
450464

451-
private void animation_target_cb (double value) {
452-
this.queue_draw ();
465+
// fix leak
466+
// callback animation target seems to leak
467+
public double animation_cb {
468+
set {
469+
this.queue_draw ();
470+
}
453471
}
454472

455473
construct {
456474
update_record_rects ();
457475
this.overflow = Gtk.Overflow.HIDDEN;
458476
this.notify["scale-factor"].connect (on_scale_factor_changed);
459477

460-
var target = new Adw.CallbackAnimationTarget (animation_target_cb);
461-
animation = new Adw.TimedAnimation (this, 0.0, 1.0, 5000, target) {
478+
animation = new Adw.TimedAnimation (this, 0.0, 1.0, 5000, new Adw.PropertyAnimationTarget (this, "animation-cb")) {
462479
easing = Adw.Easing.LINEAR,
463480
repeat_count = 0,
464481
follow_enable_animations_setting = false // it's slow and opt-in

src/Widgets/Marquee.vala

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,15 @@ public class Turntable.Widgets.Marquee : Gtk.Widget {
5959
set_accessible_role (Gtk.AccessibleRole.LABEL);
6060
}
6161

62-
private void animation_target_cb (double value) {
63-
this.rotation_progress = (float) value;
62+
uint animation_end_id = 0;
63+
private void on_animation_end () {
64+
animation_end_id = GLib.Timeout.add_once (1500, start_animation_once);
6465
}
6566

66-
private void on_animation_end () {
67-
GLib.Timeout.add_once (1500, start_animation);
67+
private void start_animation_once () {
68+
if (animation_end_id == 0) return;
69+
animation_end_id = 0;
70+
start_animation ();
6871
}
6972

7073
private void start_animation () {
@@ -92,8 +95,7 @@ public class Turntable.Widgets.Marquee : Gtk.Widget {
9295
label = new Gtk.Label ("");
9396
label.set_parent (this);
9497

95-
var target = new Adw.CallbackAnimationTarget (animation_target_cb);
96-
animation = new Adw.TimedAnimation (this, 0.0, 1.0, ANIMATION_DURATION, target) {
98+
animation = new Adw.TimedAnimation (this, 0.0, 1.0, ANIMATION_DURATION, new Adw.PropertyAnimationTarget (this, "rotation-progress")) {
9799
easing = Adw.Easing.EASE_IN_OUT_CUBIC
98100
};
99101
animation.done.connect (on_animation_end);
@@ -102,6 +104,8 @@ public class Turntable.Widgets.Marquee : Gtk.Widget {
102104
~Marquee () {
103105
debug ("Destroying");
104106
label.unparent ();
107+
if (animation_end_id != 0) GLib.Source.remove (animation_end_id);
108+
animation_end_id = 0;
105109
}
106110

107111
public override void measure (

src/Widgets/ProgressBin.vala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,12 @@ public class Turntable.Widgets.ProgressBin : Adw.Bin {
197197
update_color ();
198198
}
199199

200-
private void animation_target_cb (double value) {
201-
this.queue_draw ();
200+
// fix leak
201+
// callback animation target seems to leak
202+
public double animation_cb {
203+
set {
204+
this.queue_draw ();
205+
}
202206
}
203207

204208
construct {
@@ -216,8 +220,7 @@ public class Turntable.Widgets.ProgressBin : Adw.Bin {
216220
};
217221
}
218222

219-
var target = new Adw.CallbackAnimationTarget (animation_target_cb);
220-
animation = new Adw.TimedAnimation (this, 0.0, 1.0, PROGRESS_UPDATE_TIME, target) {
223+
animation = new Adw.TimedAnimation (this, 0.0, 1.0, PROGRESS_UPDATE_TIME, new Adw.PropertyAnimationTarget (this, "animation-cb")) {
221224
easing = Adw.Easing.LINEAR
222225
};
223226

src/Widgets/Tonearm.vala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ public class Turntable.Widgets.Tonearm : Adw.Bin {
2222
debug ("Destroying");
2323
}
2424

25-
private void animation_target_cb (double value) {
26-
this.queue_draw ();
25+
// fix leak
26+
// callback animation target seems to leak
27+
public double animation_cb {
28+
set {
29+
this.queue_draw ();
30+
}
2731
}
2832

2933
private int arm_height = 150;
@@ -98,8 +102,7 @@ public class Turntable.Widgets.Tonearm : Adw.Bin {
98102
construct {
99103
this.size = Views.Window.Size.REGULAR;
100104

101-
var target = new Adw.CallbackAnimationTarget (animation_target_cb);
102-
animation = new Adw.TimedAnimation (this, 0.0, 1.0, PROGRESS_UPDATE_TIME, target) {
105+
animation = new Adw.TimedAnimation (this, 0.0, 1.0, PROGRESS_UPDATE_TIME, new Adw.PropertyAnimationTarget (this, "animation-cb")) {
103106
easing = Adw.Easing.LINEAR
104107
};
105108

0 commit comments

Comments
 (0)