From 605ff8fb7c709cb4d1d7dad67d77ca9678e5abbf Mon Sep 17 00:00:00 2001 From: Mike Morris Date: Sat, 2 Jul 2016 01:00:09 -0400 Subject: [PATCH] [node] Bus Error 10 --- platform/node/src/node_map.cpp | 6 ++--- platform/node/src/node_request.cpp | 38 +++++++++++++++++++++--------- platform/node/src/node_request.hpp | 2 ++ 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/platform/node/src/node_map.cpp b/platform/node/src/node_map.cpp index bfa4aa3124f..d06e2e4afe8 100644 --- a/platform/node/src/node_map.cpp +++ b/platform/node/src/node_map.cpp @@ -740,10 +740,10 @@ NodeMap::~NodeMap() { std::unique_ptr NodeMap::request(const mbgl::Resource& resource, Callback callback_) { Nan::HandleScope scope; - auto worker = new NodeRequestWorker(handle()->GetInternalField(1)->ToObject(), resource, callback_); - worker->Execute(); + NodeRequestWorker worker(handle()->GetInternalField(1)->ToObject(), resource, callback_); + worker.Execute(); - return std::make_unique(worker); + return std::make_unique(&worker); } } // namespace node_mbgl diff --git a/platform/node/src/node_request.cpp b/platform/node/src/node_request.cpp index 816d59d025c..b8b2390810a 100644 --- a/platform/node/src/node_request.cpp +++ b/platform/node/src/node_request.cpp @@ -3,6 +3,7 @@ #include #include +#include namespace node_mbgl { @@ -13,9 +14,20 @@ NodeRequestWorker::NodeRequestWorker( : AsyncWorker(nullptr), nodeMapHandle(nodeMapHandle_), resource(resource_), - fileSourceCallback(std::make_unique(fileSourceCallback_)) {} + fileSourceCallback(std::make_unique(fileSourceCallback_)) { + Nan::HandleScope scope; + + auto fn = Nan::New(handleCallback); + + // Bind a reference to this object on the callback function + fn->SetHiddenValue(Nan::New("worker").ToLocalChecked(), Nan::New(this)); -NodeRequestWorker::~NodeRequestWorker() {} + callback.Reset(fn); +} + +NodeRequestWorker::~NodeRequestWorker() { + std::cout << "~NodeRequestWorker" << std::endl; +} void NodeRequestWorker::Execute() { Nan::HandleScope scope; @@ -25,20 +37,17 @@ void NodeRequestWorker::Execute() { Nan::Set(req, Nan::New("url").ToLocalChecked(), Nan::New(resource.url).ToLocalChecked()); Nan::Set(req, Nan::New("kind").ToLocalChecked(), Nan::New(resource.kind)); - auto callback = Nan::New(handleCallback); - - // Bind a reference to this object on the callback function - callback->SetHiddenValue(Nan::New("worker").ToLocalChecked(), Nan::New(this)); - v8::Local argv[] = { req, - callback + Nan::New(callback) }; Nan::MakeCallback(nodeMapHandle, "request", 2, argv); } void NodeRequestWorker::Destroy() { + std::cout << "Destroy" << !fileSourceCallback << std::endl; + // When this object gets destroyed, make sure that the // AsyncRequest can no longer attempt to remove the callback function // this object was holding (it can't be fired anymore). @@ -48,8 +57,10 @@ void NodeRequestWorker::Destroy() { } void NodeRequestWorker::WorkComplete() { + std::cout << "WorkComplete" << !fileSourceCallback << std::endl; + // If callback has already been called, no-op - if (!fileSourceCallback) return; + if (!fileSourceCallback) return Destroy(); ErrorMessage() ? HandleErrorCallback() : HandleOKCallback(); } @@ -144,8 +155,13 @@ void NodeRequestWorker::HandleErrorCallback() { cb = nullptr; } -NodeRequestWorker::NodeRequest::NodeRequest() {} -NodeRequestWorker::NodeRequest::~NodeRequest() {} +NodeRequestWorker::NodeRequest::NodeRequest() { + std::cout << "NodeRequest" << std::endl; +} + +NodeRequestWorker::NodeRequest::~NodeRequest() { + std::cout << "~NodeRequest" << std::endl; +} Nan::Persistent NodeRequestWorker::NodeRequest::constructor; diff --git a/platform/node/src/node_request.hpp b/platform/node/src/node_request.hpp index 421e69b6538..658e747ac07 100644 --- a/platform/node/src/node_request.hpp +++ b/platform/node/src/node_request.hpp @@ -49,6 +49,8 @@ class NodeRequestWorker : public Nan::AsyncWorker { const mbgl::Resource& resource; std::unique_ptr fileSourceCallback; NodeAsyncRequest* asyncRequest = nullptr; + + Nan::Persistent callback; mbgl::Response response; static Nan::Persistent handleCallback;