Bug fs::createWatcher leaks memory when watching an invalid path
Author: itssagarKCreated Apr 1, 2026Updated Aug 29, 2026
Labelsbug
The fs::createWatcher function allocates a new __WatcherListener object on the heap before calling fileWatcher->addWatch. If addWatch fails, for example when the path doesn't exist or permissions are denied — it returns a negative error code. The code completely ignores this and proceeds anyway:
__WatcherListener* listener = new __WatcherListener();
efsw::WatchID watcherId = fileWatcher->addWatch(path, listener, true);
watchListeners[watcherId] = make_pair(listener, path); // inserted even on failureTwo things go wrong here:
- The heap-allocated
listeneris never deleted — it leaks every timeaddWatchfails - The negative error code gets inserted into
watchListenersas if it were a valid watcher ID, permanently polluting the map with a dead entry
This means repeated calls with a bad path will keep leaking memory and stuffing the map with garbage entries that can never be cleaned up.
To Reproduce
// call with a path that doesn't existawait Neutralino.filesystem.createWatcher("/path/that/does/not/exist");
// calling repeatedly leaks a new listener object each time
await Neutralino.filesystem.createWatcher("/path/that/does/not/exist");
await Neutralino.filesystem.createWatcher("/path/that/does/not/exist");Specifications
- OS: All platforms
- Neutralinojs version: Current
Source: neutralinojs/neutralinojs