Skip to content

Commit d0c92ca

Browse files
FreFre
authored andcommitted
avoid memory leak when re-adding the eventlisteners
1 parent e136ed4 commit d0c92ca

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

ElectronNET.API/BridgeConnector.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public static Socket Socket
4141
if (_socket == null && !HybridSupport.IsElectronActive)
4242
{
4343
_socket = IO.Socket(new Uri("http://localhost"), new IO.Options { AutoConnect = false });
44+
Console.WriteLine("Electron not active. Socket created.");
4445
}
4546
}
4647
}

ElectronNET.Host/api/autoUpdater.js

Lines changed: 19 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ElectronNET.Host/api/autoUpdater.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ElectronNET.Host/main.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { app } = require('electron');
22
const { BrowserWindow } = require('electron');
3+
const { ipcMain } = require('electron');
34
const path = require('path');
45
const cProcess = require('child_process').spawn;
56
const portscanner = require('portscanner');
@@ -139,13 +140,18 @@ function startSocketApiBridge(port) {
139140
app['mainWindow'] = null;
140141

141142
io.on('connection', (socket) => {
142-
143143
// we need to remove previously cache instances
144144
// otherwise it will fire the same event multiple depends how many time
145145
// live reload watch happen.
146146
socket.on('disconnect', function (reason) {
147147
console.log('Got disconnect! Reason: ' + reason);
148-
socket.removeAllListeners();
148+
149+
//todo fre: added to avoid memory leak when re-adding the eventlisteners
150+
ipcMain.eventNames().forEach(n =>
151+
{
152+
ipcMain.removeAllListeners(n)
153+
});
154+
149155
delete require.cache[require.resolve('./api/app')];
150156
delete require.cache[require.resolve('./api/browserWindows')];
151157
delete require.cache[require.resolve('./api/commandLine')];
@@ -167,8 +173,8 @@ function startSocketApiBridge(port) {
167173
});
168174

169175
global['electronsocket'] = socket;
170-
global['electronsocket'].setMaxListeners(0);
171-
console.log('ASP.NET Core Application connected...', 'global.electronsocket', global['electronsocket'].id, new Date());
176+
//global['electronsocket'].setMaxListeners(0);
177+
console.log('ASP.NET Core Application connected...', 'global.electronsocket', global['electronsocket'].id, socket.listenerCount());
172178

173179
appApi = require('./api/app')(socket, app);
174180
browserWindows = require('./api/browserWindows')(socket, app);

0 commit comments

Comments
 (0)