From 8e8094118301d1dd71dbfc0fff431f0389bbe48b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Va=CC=81clav=20Slavi=CC=81k?= Date: Fri, 29 Jan 2021 19:21:32 +0100 Subject: [PATCH] Make refcounting actually work in wxFileSystemWatcher::Add() The order of refcounting operations should be the opposite of what it was: first check if the path is not watched already and only if it is not call m_service->Add() to actually watch it. (Previously reference count was kept track of, but m_service->Add() was always called, i.e. the calls were repeated and unbalanced with Remove() calls.) Remove() already has the correct order and only calls m_service->Remove() when the path's refcount reaches zero. This fixes a problem which was there since the original code was added back in 76cfd1bf95 (Make wxFileSystemWatcher watch entries reference-counted., 2012-10-15). Closes https://github.com/wxWidgets/wxWidgets/pull/2203 --- src/common/fswatchercmn.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/common/fswatchercmn.cpp b/src/common/fswatchercmn.cpp index d94477b2d9..25a5edcb72 100644 --- a/src/common/fswatchercmn.cpp +++ b/src/common/fswatchercmn.cpp @@ -121,17 +121,15 @@ wxFileSystemWatcherBase::AddAny(const wxFileName& path, if (canonical.IsEmpty()) return false; - // adding a path in a platform specific way - wxFSWatchInfo watch(canonical, events, type, filespec); - if ( !m_service->Add(watch) ) - return false; - - // on success, either add path to our 'watch-list' - // or, if already watched, inc the refcount. This may happen if - // a dir is Add()ed, then later AddTree() is called on a parent dir + // Check if the patch isn't already being watched. wxFSWatchInfoMap::iterator it = m_watches.find(canonical); if ( it == m_watches.end() ) { + // It isn't, so start watching it in a platform specific way: + wxFSWatchInfo watch(canonical, events, type, filespec); + if ( !m_service->Add(watch) ) + return false; + wxFSWatchInfoMap::value_type val(canonical, watch); m_watches.insert(val); }