SpartanJ / efsw

efsw is a C++ cross-platform file system watcher and notifier.
Other
645 stars 98 forks source link

Paranoid exit code? #155

Closed jackokring closed 1 year ago

jackokring commented 1 year ago
Plugin* pluginInstance;
#ifdef WATCHER
#include "../efsw/include/efsw/efsw.hpp"

// File action handler
class UpdateListener : public efsw::FileWatchListener {
  public:
    void handleFileAction( efsw::WatchID watchid, const std::string& dir,
                           const std::string& filename, efsw::Action action,
                           std::string oldFilename ) override {
        // old school
        callbackWatcher(filename.c_str());
        switch ( action ) {
            case efsw::Actions::Add:

                break;
            case efsw::Actions::Delete:

                break;
            case efsw::Actions::Modified:

                break;
            case efsw::Actions::Moved:

                break;
            default:
                break;
        }
    }
};

static struct WatcherDo {

    void add() {
        wID = fileWatcher.addWatch(asset::plugin(pluginInstance, ""), &listener, true);
        fileWatcher.watch();
    }

    ~WatcherDo() {
        fileWatcher.removeWatch(wID);//just to be sure
    }
    //didn't alloc anything but keep in focus for use until
    UpdateListener listener;
    //de-alloc last (must remove before listener out of focus)
    //not sure if it would iterate over active watches
    efsw::FileWatcher fileWatcher;
    //de-alloc first (auto removed from fileWatcher?)
    efsw::WatchID wID;
} watcherInstance;
#endif

void init(Plugin* p) {
    pluginInstance = p;
    // redefine so that correct include happens
#undef MODEL    
#define MODEL(name) p->addModel(name)
#include "modules.hpp"
#undef MODEL
#ifdef WATCHER
    watcherInstance.add();
#endif
}

Just wondering what order reclamation works for a library include as the example source does not explain if a WatchID cancels on its destructor etc. Not known if this is an issue. The project plugin I'm working on seg faults on library unloading (a few Plugin after my plugin), but this could be any number of other plugin's fault.

SpartanJ commented 1 year ago

Yes, the destructor of efsw::FileWatcher will close all the watches folders for you. It shouldn't be a problem. A minor detail: watch() should be called only once. In your case, you could call watch in the constructor of WatcherDo, and add the watches whenever you need them. But calling watch many times shouldn't do any harm, it will only "execute" in the first call. You can't trace-back the segfault?

jackokring commented 1 year ago

No, it's fixed. I only need the one plugin watch and a sort on filename.