Skip to content

Commit

Permalink
SDA-4453 Memory leak issue on snipping tool (finos#2080)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbenmoussati authored Jan 17, 2024
1 parent e1b9722 commit 127e5af
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
29 changes: 21 additions & 8 deletions src/app/screen-snippet-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class ScreenSnippet {
hideOnCapture,
);
this.uploadSnippet(currentWindowObj, webContents, hideOnCapture);
this.closeSnippet(currentWindowObj?.webContents);
this.closeSnippet(currentWindowObj);
this.copyToClipboard();
this.saveAs();
return;
Expand Down Expand Up @@ -340,8 +340,8 @@ class ScreenSnippet {
webContents: WebContents,
hideOnCapture?: boolean,
) {
ipcMain.once(
'upload-snippet',
ipcMain.on(
ScreenShotAnnotation.UPLOAD,
async (
_event,
snippetData: { screenSnippetPath: string; mergedImageData: string },
Expand All @@ -366,16 +366,22 @@ class ScreenSnippet {
`screen-snippet-handler: upload of screen capture failed with error: ${error}!`,
);
}
focusedWindow?.webContents.focus();
if (focusedWindow && !focusedWindow.isDestroyed()) {
focusedWindow.webContents.focus();
} else {
logger.info(
'screen-snippet-handler: tried to focus a destroyed window on upload ',
(focusedWindow as ICustomBrowserWindow).winName,
);
}
},
);
}

/**
* Close the current snippet
*/
private closeSnippet(webContents: WebContents | undefined) {
ipcMain.once(ScreenShotAnnotation.CLOSE, async (_event) => {
private closeSnippet(focusedWindow: BrowserWindow | null) {
ipcMain.on(ScreenShotAnnotation.CLOSE, async (_event) => {
try {
windowHandler.closeSnippingToolWindow();
await this.verifyAndUpdateAlwaysOnTop();
Expand All @@ -385,7 +391,14 @@ class ScreenSnippet {
`screen-snippet-handler: close window failed with error: ${error}!`,
);
}
webContents?.focus();
if (focusedWindow && !focusedWindow.isDestroyed()) {
focusedWindow.webContents.focus();
} else {
logger.warn(
'screen-snippet-handler: tried to focus a destroyed window on close ',
(focusedWindow as ICustomBrowserWindow).winName,
);
}
});
}

Expand Down
3 changes: 2 additions & 1 deletion src/app/window-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,6 @@ export class WindowHandler {
) {
opts.alwaysOnTop = true;
}

const areWindowsRestoredPostHide =
(winStore.windowsRestored && this.hideOnCapture) || !this.hideOnCapture;

Expand Down Expand Up @@ -1512,6 +1511,8 @@ export class WindowHandler {
logger.info(
'window-handler: createSnippingToolWindow: Closing snipping window, attempting to delete temp snip image',
);
ipcMain.removeAllListeners(ScreenShotAnnotation.CLOSE);
ipcMain.removeAllListeners(ScreenShotAnnotation.UPLOAD);
ipcMain.removeAllListeners(ScreenShotAnnotation.COPY_TO_CLIPBOARD);
ipcMain.removeAllListeners(ScreenShotAnnotation.SAVE_AS);
this.snippingToolWindow?.close();
Expand Down
1 change: 1 addition & 0 deletions src/common/ipcEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export enum ScreenShotAnnotation {
COPY_TO_CLIPBOARD = 'copy-to-clipboard',
SAVE_AS = 'save-as',
CLOSE = 'close-snippet',
UPLOAD = 'upload-snippet',
}
5 changes: 4 additions & 1 deletion src/renderer/components/snipping-tool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,10 @@ const SnippingTool: React.FunctionComponent<ISnippingToolProps> = ({
AnalyticsElements.SCREEN_CAPTURE_ANNOTATE,
ScreenSnippetActionTypes.ANNOTATE_ADD,
);
ipcRenderer.send('upload-snippet', { screenSnippetPath, mergedImageData });
ipcRenderer.send(ScreenShotAnnotation.UPLOAD, {
screenSnippetPath,
mergedImageData,
});
};

const onClose = async () => {
Expand Down

0 comments on commit 127e5af

Please sign in to comment.