From 8902550242757d9e2344d80c4e95da13d42a076b Mon Sep 17 00:00:00 2001 From: Artem Pavlenko Date: Wed, 13 Nov 2019 14:01:06 +0000 Subject: Call `Nan::AdjustExternalMemory` to ensure allocated memory is registred with GC (ref #15915) --- platform/node/src/node_map.cpp | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/platform/node/src/node_map.cpp b/platform/node/src/node_map.cpp index 5ce201da2e..9e80f9ee99 100644 --- a/platform/node/src/node_map.cpp +++ b/platform/node/src/node_map.cpp @@ -539,18 +539,14 @@ void NodeMap::renderFinished() { request->runInAsyncScope(target, callback, 1, argv); } else if (img.data) { + auto image_size = img.bytes(); v8::Local pixels = Nan::NewBuffer( - reinterpret_cast(img.data.get()), img.bytes(), - // Retain the data until the buffer is deleted. - [](char *, void * hint) { - delete [] reinterpret_cast(hint); - }, - img.data.get() - ).ToLocalChecked(); - if (!pixels.IsEmpty()) { - img.data.release(); - } - + reinterpret_cast(img.data.release()), image_size, + [](char* buf, void*) { + delete [] buf; + }, nullptr + ).ToLocalChecked(); + Nan::AdjustExternalMemory(image_size); v8::Local argv[] = { Nan::Null(), pixels @@ -592,7 +588,7 @@ void NodeMap::release() { uv_close(reinterpret_cast(async), [] (uv_handle_t *h) { delete reinterpret_cast(h); }); - + map.reset(); frontend.reset(); } @@ -620,7 +616,7 @@ void NodeMap::Cancel(const Nan::FunctionCallbackInfo& info) { void NodeMap::cancel() { auto style = map->getStyle().getJSON(); - + // Reset map explicitly as it resets the renderer frontend map.reset(); @@ -789,7 +785,7 @@ void NodeMap::AddImage(const Nan::FunctionCallbackInfo& info) { float pixelRatio = Nan::Get(optionObject, Nan::New("pixelRatio").ToLocalChecked()).ToLocalChecked()->NumberValue(); auto imageBuffer = Nan::To(info[1]).ToLocalChecked()->ToObject(); - + char * imageDataBuffer = node::Buffer::Data(imageBuffer); size_t imageLength = node::Buffer::Length(imageBuffer); @@ -799,7 +795,7 @@ void NodeMap::AddImage(const Nan::FunctionCallbackInfo& info) { std::unique_ptr data = std::make_unique(imageLength); std::copy(imageDataBuffer, imageDataBuffer + imageLength, data.get()); - + mbgl::UnassociatedImage cImage({ imageWidth, imageHeight}, std::move(data)); mbgl::PremultipliedImage cPremultipliedImage = mbgl::util::premultiply(std::move(cImage)); nodeMap->map->getStyle().addImage(std::make_unique(*Nan::Utf8String(info[0]), std::move(cPremultipliedImage), pixelRatio, sdf)); @@ -822,25 +818,25 @@ void NodeMap::RemoveImage(const Nan::FunctionCallbackInfo& info) { nodeMap->map->getStyle().removeImage(*Nan::Utf8String(info[0])); } - + void NodeMap::SetLayerZoomRange(const Nan::FunctionCallbackInfo& info) { using namespace mbgl::style; auto nodeMap = Nan::ObjectWrap::Unwrap(info.Holder()); if (!nodeMap->map) return Nan::ThrowError(releasedMessage()); - + if (info.Length() != 3) { return Nan::ThrowTypeError("Three arguments required"); } - + if (!info[0]->IsString()) { return Nan::ThrowTypeError("First argument must be a string"); } - + if (!info[1]->IsNumber() || !info[2]->IsNumber()) { return Nan::ThrowTypeError("Second and third arguments must be numbers"); } - + mbgl::style::Layer* layer = nodeMap->map->getStyle().getLayer(*Nan::Utf8String(info[0])); if (!layer) { return Nan::ThrowTypeError("layer not found"); @@ -1351,9 +1347,9 @@ void NodeMap::QueryRenderedFeatures(const Nan::FunctionCallbackInfo& if (!info[1]->IsObject()) { return Nan::ThrowTypeError("options argument must be an object"); } - + auto options = Nan::To(info[1]).ToLocalChecked(); - + //Check if layers is set. If provided, it must be an array of strings if (Nan::Has(options, Nan::New("layers").ToLocalChecked()).FromJust()) { auto layersOption = Nan::Get(options, Nan::New("layers").ToLocalChecked()).ToLocalChecked(); @@ -1367,7 +1363,7 @@ void NodeMap::QueryRenderedFeatures(const Nan::FunctionCallbackInfo& } queryOptions.layerIDs = layersVec; } - + //Check if filter is provided. If set it must be a valid Filter object if (Nan::Has(options, Nan::New("filter").ToLocalChecked()).FromJust()) { auto filterOption = Nan::Get(options, Nan::New("filter").ToLocalChecked()).ToLocalChecked(); -- cgit v1.2.1