Skip to content

Commit a11c36e

Browse files
71doppioandante
andauthored
fix minor memory leaks (#416)
These are pretty minor: - The one in `editors.ts` only leaks memory until a new editor is opened. - The other one only happens when Dance is disabled. Also fixes iteration of recorder when reaching the start of a buffer. Co-authored-by: Enrico Lumetti <[email protected]>
1 parent 3183056 commit a11c36e

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

src/state/editors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ export class Editors implements vscode.Disposable {
687687
state.dispose();
688688
}
689689

690-
this._lastRemovedEditorStates.length === 0;
690+
this._lastRemovedEditorStates.length = 0;
691691

692692
// Dispose of fallback editor, if any.
693693
const fallback = this._fallbacks.get(document);

src/state/extension.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ export class Extension implements vscode.Disposable {
204204
},
205205
true,
206206
);
207-
208207
}
209208

210209
/**
@@ -219,6 +218,24 @@ export class Extension implements vscode.Disposable {
219218
assert(this._autoDisposables.size === 0);
220219

221220
this.statusBar.dispose();
221+
222+
// Clear configuration handlers.
223+
this._configurationChangeHandlers.clear();
224+
225+
// Dispose of all subscriptions.
226+
for (const subscription of this._subscriptions) {
227+
subscription.dispose();
228+
}
229+
this._subscriptions.length = 0;
230+
231+
// Dispose of core components.
232+
this.editors.dispose();
233+
this.recorder.dispose();
234+
this.modes.dispose();
235+
this.registers.dispose();
236+
237+
// Dismiss error message, if any.
238+
this.dismissErrorMessage();
222239
}
223240

224241
/**

src/state/modes.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ export declare namespace Mode {
552552
/**
553553
* The set of all modes.
554554
*/
555-
export class Modes implements Iterable<Mode> {
555+
export class Modes implements Iterable<Mode>, vscode.Disposable {
556556
private readonly _vscodeModeDefaults: Mode.Configuration = {
557557
cursorStyle: "line",
558558
inheritFrom: null,
@@ -581,6 +581,13 @@ export class Modes implements Iterable<Mode> {
581581
this._observePreferences(extension);
582582
}
583583

584+
public dispose(): void {
585+
for (const mode of this._modes.values()) {
586+
mode.dispose();
587+
}
588+
this._modes.clear();
589+
}
590+
584591
/**
585592
* The default mode configured using `dance.defaultMode`.
586593
*/

src/state/recorder.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,11 @@ export class Recorder implements vscode.Disposable {
204204
* call.
205205
*/
206206
public cursorFromEnd() {
207-
return new Cursor(this, this._previousBuffers.length, this._buffer.length - 1);
207+
return new Cursor(
208+
this,
209+
this._previousBuffers.length,
210+
this._buffer.length === 0 ? 0 : this._buffer.length - 1,
211+
);
208212
}
209213

210214
/**

0 commit comments

Comments
 (0)