From e95936c624e05c52fe77cac1251d6ed524078600 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 1 Jul 2021 15:57:17 +0200 Subject: [PATCH] Fix memory leak of `Editor` due to blinking cursors Co-Authored-By: Nathan Sobo --- zed/src/editor.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/zed/src/editor.rs b/zed/src/editor.rs index a844b03f920b72ac52e0cf1223202a925af6a7af..7c627d78757bf8bf59ffb71026b2d78cd3e73f68 100644 --- a/zed/src/editor.rs +++ b/zed/src/editor.rs @@ -2369,11 +2369,14 @@ impl Editor { cx.notify(); let epoch = self.next_blink_epoch(); - cx.spawn(|this, mut cx| async move { - Timer::after(CURSOR_BLINK_INTERVAL).await; - this.update(&mut cx, |this, cx| { - this.resume_cursor_blinking(epoch, cx); - }) + cx.spawn(|this, mut cx| { + let this = this.downgrade(); + async move { + Timer::after(CURSOR_BLINK_INTERVAL).await; + if let Some(this) = cx.read(|cx| this.upgrade(cx)) { + this.update(&mut cx, |this, cx| this.resume_cursor_blinking(epoch, cx)) + } + } }) .detach(); } @@ -2391,9 +2394,14 @@ impl Editor { cx.notify(); let epoch = self.next_blink_epoch(); - cx.spawn(|this, mut cx| async move { - Timer::after(CURSOR_BLINK_INTERVAL).await; - this.update(&mut cx, |this, cx| this.blink_cursors(epoch, cx)); + cx.spawn(|this, mut cx| { + let this = this.downgrade(); + async move { + Timer::after(CURSOR_BLINK_INTERVAL).await; + if let Some(this) = cx.read(|cx| this.upgrade(cx)) { + this.update(&mut cx, |this, cx| this.blink_cursors(epoch, cx)); + } + } }) .detach(); }