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:

cpp
__WatcherListener* listener = new __WatcherListener();
efsw::WatchID watcherId = fileWatcher->addWatch(path, listener, true);
watchListeners[watcherId] = make_pair(listener, path); // inserted even on failure

Two things go wrong here:

  • The heap-allocated listener is never deleted — it leaks every time addWatch fails
  • The negative error code gets inserted into watchListeners as 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

javascript
// 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