Skip to content

Commit

Permalink
Fix possible crash on start scan on windows
Browse files Browse the repository at this point in the history
Increment version
  • Loading branch information
funbiscuit committed May 2, 2020
1 parent 8d31540 commit 7bdb707
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 0.2.0.{build}
version: 0.2.1.{build}

image:
- Visual Studio 2017
Expand Down
5 changes: 3 additions & 2 deletions lib/include/spacewatcher-win.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
#include <string>
#include <memory>
#include <list>
#include <atomic>

#include <Windows.h>

class SpaceWatcherWin : public SpaceWatcher {
public:

SpaceWatcherWin();
~SpaceWatcherWin();
~SpaceWatcherWin() override;

bool beginWatch(const std::string& path) override;
void endWatch() override;
Expand All @@ -29,7 +30,7 @@ class SpaceWatcherWin : public SpaceWatcher {

const int watchBufferSize = 1024*48;

HANDLE watchedDir = INVALID_HANDLE_VALUE;
std::atomic<HANDLE> watchedDir;

std::unique_ptr<DWORD[]> watchBuffer;

Expand Down
21 changes: 12 additions & 9 deletions lib/include/spacewatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SpaceWatcher {
static std::unique_ptr<SpaceWatcher> getWatcher();

SpaceWatcher();
~SpaceWatcher();
virtual ~SpaceWatcher();

virtual bool beginWatch(const std::string& path) = 0;
virtual void endWatch() = 0;
Expand All @@ -49,21 +49,24 @@ class SpaceWatcher {

protected:

std::mutex eventsMtx;
std::list<std::unique_ptr<FileEvent>> eventQueue;

std::string watchedPath;

std::atomic<bool> runThread;
std::thread watchThread;

void initPlatform();

virtual void readEvents() = 0;

// start and stop thread are called from child classes
void startThread();
void stopThread();

void addEvent(std::unique_ptr<FileEvent> event);
private:

std::mutex eventsMtx;
std::list<std::unique_ptr<FileEvent>> eventQueue;


std::atomic<bool> runThread;
std::thread watchThread;

void watcherRun();
};

Expand Down
4 changes: 3 additions & 1 deletion lib/src/spacewatcher-win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@

#include <iostream>

SpaceWatcherWin::SpaceWatcherWin()
SpaceWatcherWin::SpaceWatcherWin() : watchedDir(INVALID_HANDLE_VALUE)
{
watchBuffer=std::unique_ptr<DWORD[]>(new DWORD[watchBufferSize]);
startThread();
}
SpaceWatcherWin::~SpaceWatcherWin()
{
// endWatch() is virtual so we can't call it in destructor
_endWatch();
stopThread();
}

bool SpaceWatcherWin::beginWatch(const std::string& path)
Expand Down
21 changes: 18 additions & 3 deletions lib/src/spacewatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,29 @@

#include <iostream>

SpaceWatcher::SpaceWatcher() : runThread(true)
SpaceWatcher::SpaceWatcher() : runThread(false)
{

}

SpaceWatcher::~SpaceWatcher()
{
}

void SpaceWatcher::startThread()
{
if(runThread)
return;
runThread = true;
//Start thread after everything is initialized
watchThread=std::thread(&SpaceWatcher::watcherRun, this);
}

SpaceWatcher::~SpaceWatcher()
void SpaceWatcher::stopThread()
{
if(!runThread)
return;

runThread = false;
watchThread.join();
}
Expand All @@ -38,7 +52,8 @@ void SpaceWatcher::watcherRun()
std::cout << "Start watcher thread\n";
while(runThread)
{
readEvents();
if(isWatching())
readEvents();
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
std::cout << "Stop watcher thread\n";
Expand Down

0 comments on commit 7bdb707

Please sign in to comment.