Skip to content

Commit 985aec0

Browse files
Releases ThreadSafeFunction callbacks on stop to prevent event loop from persisting.
1 parent b3308a9 commit 985aec0

File tree

2 files changed

+4
-9
lines changed

2 files changed

+4
-9
lines changed

includes/NSFW.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class NSFW : public Napi::ObjectWrap<NSFW> {
2626
std::string mPath;
2727
std::thread mPollThread;
2828
std::atomic<bool> mRunning;
29-
std::atomic<bool> mFinalizing;
3029
std::condition_variable mWaitPoolEvents;
3130
std::mutex mRunningLock;
3231
std::vector<std::string> mExcludedPaths;

src/NSFW.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ NSFW::NSFW(const Napi::CallbackInfo &info):
9999

100100
NSFW::~NSFW() {
101101
if (mRunning) {
102-
mFinalizing = true;
103102
{
104103
std::lock_guard<std::mutex> lock(mRunningLock);
105104
mRunning = false;
@@ -213,6 +212,10 @@ void NSFW::StopWorker::Execute() {
213212
mNSFW->mWaitPoolEvents.notify_one();
214213
mNSFW->mPollThread.join();
215214

215+
// Release ThreadSafeFunction callbacks to prevent keeping event loop alive
216+
mNSFW->mErrorCallback.Release();
217+
mNSFW->mEventCallback.Release();
218+
216219
std::lock_guard<std::mutex> lock(mNSFW->mInterfaceLock);
217220
mNSFW->mInterface.reset(nullptr);
218221
mNSFW->mQueue->clear();
@@ -443,13 +446,6 @@ void NSFW::pollForEvents() {
443446
}
444447
);
445448
}
446-
447-
// If we are destroying NFSW object (destructor) we cannot release the thread safe functions at this point
448-
// or we get a segfault
449-
if (!mFinalizing) {
450-
mErrorCallback.Release();
451-
mEventCallback.Release();
452-
}
453449
}
454450

455451
Napi::Value NSFW::ExcludedPaths() {

0 commit comments

Comments
 (0)