Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: possible SIGILL and memory leak #204

Merged
merged 3 commits into from
Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 4 additions & 8 deletions fsevents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

#include "src/storage.cc"
namespace fse {
class FSEvents : public node::ObjectWrap {
class FSEvents : public Nan::ObjectWrap {
public:
FSEvents(const char *path, Nan::Callback *handler);
explicit FSEvents(const char *path);
~FSEvents();

// locking.cc
Expand All @@ -42,7 +42,6 @@ namespace fse {

// methods.cc - internal
Nan::AsyncResource async_resource;
Nan::Callback *handler;
void emitEvent(const char *path, UInt32 flags, UInt64 id);

// Common
Expand All @@ -60,18 +59,15 @@ namespace fse {

using namespace fse;

FSEvents::FSEvents(const char *path, Nan::Callback *handler)
: async_resource("fsevents:FSEvents"), handler(handler) {
FSEvents::FSEvents(const char *path)
: async_resource("fsevents:FSEvents"), lockStarted(false) {
CFStringRef dirs[] = { CFStringCreateWithCString(NULL, path, kCFStringEncodingUTF8) };
paths = CFArrayCreate(NULL, (const void **)&dirs, 1, NULL);
threadloop = NULL;
lockingStart();
}
FSEvents::~FSEvents() {
std::cout << "YIKES" << std::endl;
lockingStop();
delete handler;
handler = NULL;

CFRelease(paths);
}
Expand Down
16 changes: 9 additions & 7 deletions src/methods.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,30 @@
*/

void FSEvents::emitEvent(const char *path, UInt32 flags, UInt64 id) {
if (!handler) return;
Nan::HandleScope handle_scope;
v8::Local<v8::Object> object = handle();
v8::Local<v8::Value> key = Nan::New<v8::String>("handler").ToLocalChecked();
Nan::Callback handler(Nan::To<v8::Function>(Nan::Get(object, key).ToLocalChecked()).ToLocalChecked());
v8::Local<v8::Value> argv[] = {
Nan::New<v8::String>(path).ToLocalChecked(),
Nan::New<v8::Number>(flags),
Nan::New<v8::Number>(id)
};
handler->Call(3, argv, &async_resource);
handler.Call(3, argv, &async_resource);
}

NAN_METHOD(FSEvents::New) {
Nan::Utf8String *path = new Nan::Utf8String(info[0]);
Nan::Callback *callback = new Nan::Callback(info[1].As<v8::Function>());
Nan::Utf8String path(info[0]);

FSEvents *fse = new FSEvents(**path, callback);
FSEvents *fse = new FSEvents(*path);
fse->Wrap(info.This());
Nan::Set(info.This(), Nan::New<v8::String>("handler").ToLocalChecked(), info[1]);

info.GetReturnValue().Set(info.This());
}

NAN_METHOD(FSEvents::Stop) {
FSEvents* fse = node::ObjectWrap::Unwrap<FSEvents>(info.This());
FSEvents* fse = Nan::ObjectWrap::Unwrap<FSEvents>(info.This());

fse->threadStop();
fse->asyncStop();
Expand All @@ -34,7 +36,7 @@ NAN_METHOD(FSEvents::Stop) {
}

NAN_METHOD(FSEvents::Start) {
FSEvents* fse = node::ObjectWrap::Unwrap<FSEvents>(info.This());
FSEvents* fse = Nan::ObjectWrap::Unwrap<FSEvents>(info.This());
fse->asyncStart();
fse->threadStart();

Expand Down