Skip to content

Commit

Permalink
fix memory leak in Canvas constructor
Browse files Browse the repository at this point in the history
Don't recreate the cairo surface when the canvas is constructed.

Fixes #922
  • Loading branch information
zbjornson committed Jun 27, 2017
1 parent 4f4f857 commit 5889663
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/Canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,6 @@ NAN_METHOD(Canvas::RegisterFont) {

Canvas::Canvas(Backend* backend) : ObjectWrap() {
_backend = backend;
this->backend()->createSurface();
}

/*
Expand Down
4 changes: 2 additions & 2 deletions src/backend/ImageBackend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ ImageBackend::~ImageBackend()

cairo_surface_t* ImageBackend::createSurface()
{
assert(!this->surface);
this->surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
assert(this->surface);
Nan::AdjustExternalMemory(4 * width * height);
Nan::AdjustExternalMemory(4 * width * height);

return this->surface;
}
Expand All @@ -35,7 +36,6 @@ cairo_surface_t* ImageBackend::recreateSurface()
return createSurface();
}


Nan::Persistent<FunctionTemplate> ImageBackend::constructor;

void ImageBackend::Initialize(Handle<Object> target)
Expand Down
4 changes: 2 additions & 2 deletions src/backend/PdfBackend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ using namespace v8;
PdfBackend::PdfBackend(int width, int height)
: Backend("pdf", width, height)
{
_closure = malloc(sizeof(closure_t));
assert(_closure);
createSurface();
}

Expand All @@ -28,8 +30,6 @@ PdfBackend::~PdfBackend()

cairo_surface_t* PdfBackend::createSurface()
{
_closure = malloc(sizeof(closure_t));
assert(_closure);
cairo_status_t status = closure_init((closure_t*)_closure, this->canvas, 0, PNG_NO_FILTERS);
assert(status == CAIRO_STATUS_SUCCESS);

Expand Down
1 change: 0 additions & 1 deletion src/backend/SvgBackend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ SvgBackend::SvgBackend(int width, int height)
{
_closure = malloc(sizeof(closure_t));
assert(_closure);

createSurface();
}

Expand Down

0 comments on commit 5889663

Please sign in to comment.