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 memory leak when img onerror/onload callbacks reference img #1003

Merged
merged 1 commit into from
Sep 19, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 12 additions & 93 deletions src/Image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ Image::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
SetProtoAccessor(proto, Nan::New("height").ToLocalChecked(), GetHeight, NULL, ctor);
SetProtoAccessor(proto, Nan::New("naturalWidth").ToLocalChecked(), GetNaturalWidth, NULL, ctor);
SetProtoAccessor(proto, Nan::New("naturalHeight").ToLocalChecked(), GetNaturalHeight, NULL, ctor);
SetProtoAccessor(proto, Nan::New("onload").ToLocalChecked(), GetOnload, SetOnload, ctor);
SetProtoAccessor(proto, Nan::New("onerror").ToLocalChecked(), GetOnerror, SetOnerror, ctor);
#if CAIRO_VERSION_MINOR >= 10
SetProtoAccessor(proto, Nan::New("dataMode").ToLocalChecked(), GetDataMode, SetDataMode, ctor);
ctor->Set(Nan::New("MODE_IMAGE").ToLocalChecked(), Nan::New<Number>(DATA_IMAGE));
Expand All @@ -74,6 +72,8 @@ NAN_METHOD(Image::New) {
Image *img = new Image;
img->data_mode = DATA_IMAGE;
img->Wrap(info.This());
info.This()->Set(Nan::New("onload").ToLocalChecked(), Nan::Null());
info.This()->Set(Nan::New("onerror").ToLocalChecked(), Nan::Null());
info.GetReturnValue().Set(info.This());
}

Expand Down Expand Up @@ -233,9 +233,17 @@ NAN_SETTER(Image::SetSource) {

// check status
if (status) {
img->error(Canvas::Error(status));
Local<Value> onerrorFn = info.This()->Get(Nan::New("onerror").ToLocalChecked());
if (onerrorFn->IsFunction()) {
Local<Value> argv[1] = { Canvas::Error(status) };
onerrorFn.As<Function>()->Call(Isolate::GetCurrent()->GetCurrentContext()->Global(), 1, argv);
}
} else {
img->loaded();
Local<Value> onloadFn = info.This()->Get(Nan::New("onload").ToLocalChecked());
if (onloadFn->IsFunction()) {
onloadFn.As<Function>()->Call(Isolate::GetCurrent()->GetCurrentContext()->Global(), 0, NULL);
}
}
}

Expand Down Expand Up @@ -305,66 +313,6 @@ Image::readPNG(void *c, uint8_t *data, unsigned int len) {
return CAIRO_STATUS_SUCCESS;
}

/*
* Get onload callback.
*/

NAN_GETTER(Image::GetOnload) {
Image *img = Nan::ObjectWrap::Unwrap<Image>(info.This());
if (img->onload) {
info.GetReturnValue().Set(img->onload->GetFunction());
} else {
info.GetReturnValue().SetNull();
}
}

/*
* Set onload callback.
*/

NAN_SETTER(Image::SetOnload) {
if (value->IsFunction()) {
Image *img = Nan::ObjectWrap::Unwrap<Image>(info.This());
img->onload = new Nan::Callback(value.As<Function>());
} else if (value->IsNull()) {
Image *img = Nan::ObjectWrap::Unwrap<Image>(info.This());
if (img->onload) {
delete img->onload;
}
img->onload = NULL;
}
}

/*
* Get onerror callback.
*/

NAN_GETTER(Image::GetOnerror) {
Image *img = Nan::ObjectWrap::Unwrap<Image>(info.This());
if (img->onerror) {
info.GetReturnValue().Set(img->onerror->GetFunction());
} else {
info.GetReturnValue().SetNull();
}
}

/*
* Set onerror callback.
*/

NAN_SETTER(Image::SetOnerror) {
if (value->IsFunction()) {
Image *img = Nan::ObjectWrap::Unwrap<Image>(info.This());
img->onerror = new Nan::Callback(value.As<Function>());
} else if (value->IsNull()) {
Image *img = Nan::ObjectWrap::Unwrap<Image>(info.This());
if (img->onerror) {
delete img->onerror;
}
img->onerror = NULL;
}
}

/*
* Initialize a new Image.
*/
Expand All @@ -377,8 +325,6 @@ Image::Image() {
width = height = 0;
naturalWidth = naturalHeight = 0;
state = DEFAULT;
onload = NULL;
onerror = NULL;
#ifdef HAVE_RSVG
_rsvg = NULL;
_is_svg = false;
Expand All @@ -392,16 +338,6 @@ Image::Image() {

Image::~Image() {
clearData();

if (onerror) {
delete onerror;
onerror = NULL;
}

if (onload) {
delete onload;
onload = NULL;
}
}

/*
Expand All @@ -418,7 +354,7 @@ Image::load() {
}

/*
* Invoke onload (when assigned) and assign dimensions.
* Set state, assign dimensions.
*/

void
Expand All @@ -430,23 +366,6 @@ Image::loaded() {
height = naturalHeight = cairo_image_surface_get_height(_surface);
_data_len = naturalHeight * cairo_image_surface_get_stride(_surface);
Nan::AdjustExternalMemory(_data_len);

if (onload != NULL) {
onload->Call(0, NULL);
}
}

/*
* Invoke onerror (when assigned) with the given err.
*/

void
Image::error(Local<Value> err) {
Nan::HandleScope scope;
if (onerror != NULL) {
Local<Value> argv[1] = { err };
onerror->Call(1, argv);
}
}

/*
Expand Down
6 changes: 0 additions & 6 deletions src/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,17 @@ class Image: public Nan::ObjectWrap {
char *filename;
int width, height;
int naturalWidth, naturalHeight;
Nan::Callback *onload;
Nan::Callback *onerror;
static Nan::Persistent<FunctionTemplate> constructor;
static void Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target);
static NAN_METHOD(New);
static NAN_GETTER(GetSource);
static NAN_GETTER(GetOnload);
static NAN_GETTER(GetOnerror);
static NAN_GETTER(GetComplete);
static NAN_GETTER(GetWidth);
static NAN_GETTER(GetHeight);
static NAN_GETTER(GetNaturalWidth);
static NAN_GETTER(GetNaturalHeight);
static NAN_GETTER(GetDataMode);
static NAN_SETTER(SetSource);
static NAN_SETTER(SetOnload);
static NAN_SETTER(SetOnerror);
static NAN_SETTER(SetDataMode);
static NAN_SETTER(SetWidth);
static NAN_SETTER(SetHeight);
Expand Down