Skip to content

Commit af58a74

Browse files
zbjornsonchearon
authored andcommitted
fix incorrect external memory reporting
Moves the AdjustExternalMemory calls to always be adjacent to cairo_image_surface_create/destroy so they can't be used incorrectly. See #1202 (comment)
1 parent c45b81b commit af58a74

File tree

4 files changed

+16
-36
lines changed

4 files changed

+16
-36
lines changed

src/backend/Backend.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#include "Backend.h"
22

3-
43
Backend::Backend(string name, int width, int height)
54
: name(name)
65
, width(width)
76
, height(height)
8-
, surface(NULL)
9-
, canvas(NULL)
107
{}
118

129
Backend::~Backend()

src/backend/Backend.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class Backend : public Nan::ObjectWrap
2525
protected:
2626
int width;
2727
int height;
28-
cairo_surface_t* surface;
29-
Canvas* canvas;
28+
cairo_surface_t* surface = nullptr;
29+
Canvas* canvas = nullptr;
3030

3131
Backend(string name, int width, int height);
3232
static void init(const Nan::FunctionCallbackInfo<v8::Value> &info);
@@ -41,7 +41,7 @@ class Backend : public Nan::ObjectWrap
4141
virtual cairo_surface_t* recreateSurface();
4242

4343
DLL_PUBLIC cairo_surface_t* getSurface();
44-
void destroySurface();
44+
virtual void destroySurface();
4545

4646
DLL_PUBLIC string getName();
4747

src/backend/ImageBackend.cc

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@ ImageBackend::ImageBackend(int width, int height)
66
: Backend("image", width, height)
77
{}
88

9-
ImageBackend::~ImageBackend()
10-
{
11-
if (surface) {
12-
destroySurface();
13-
Nan::AdjustExternalMemory(-approxBytesPerPixel() * width * height);
14-
}
15-
}
16-
179
Backend *ImageBackend::construct(int width, int height){
1810
return new ImageBackend(width, height);
1911
}
@@ -39,27 +31,20 @@ int32_t ImageBackend::approxBytesPerPixel() {
3931
}
4032
}
4133

42-
cairo_surface_t* ImageBackend::createSurface()
43-
{
44-
assert(!this->surface);
45-
this->surface = cairo_image_surface_create(this->format, width, height);
46-
assert(this->surface);
34+
cairo_surface_t* ImageBackend::createSurface() {
35+
assert(!surface);
36+
surface = cairo_image_surface_create(format, width, height);
37+
assert(surface);
4738
Nan::AdjustExternalMemory(approxBytesPerPixel() * width * height);
48-
49-
return this->surface;
39+
return surface;
5040
}
5141

52-
cairo_surface_t* ImageBackend::recreateSurface()
53-
{
54-
// Re-surface
55-
if (this->surface) {
56-
int old_width = cairo_image_surface_get_width(this->surface);
57-
int old_height = cairo_image_surface_get_height(this->surface);
58-
this->destroySurface();
59-
Nan::AdjustExternalMemory(-approxBytesPerPixel() * old_width * old_height);
60-
}
61-
62-
return createSurface();
42+
void ImageBackend::destroySurface() {
43+
if (surface) {
44+
cairo_surface_destroy(surface);
45+
surface = nullptr;
46+
Nan::AdjustExternalMemory(-approxBytesPerPixel() * width * height);
47+
}
6348
}
6449

6550
cairo_format_t ImageBackend::getFormat() {
@@ -72,8 +57,7 @@ void ImageBackend::setFormat(cairo_format_t _format) {
7257

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

75-
void ImageBackend::Initialize(Handle<Object> target)
76-
{
60+
void ImageBackend::Initialize(Handle<Object> target) {
7761
Nan::HandleScope scope;
7862

7963
Local<FunctionTemplate> ctor = Nan::New<FunctionTemplate>(ImageBackend::New);

src/backend/ImageBackend.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ class ImageBackend : public Backend
1111
{
1212
private:
1313
cairo_surface_t* createSurface();
14-
cairo_surface_t* recreateSurface();
14+
void destroySurface();
1515
cairo_format_t format = DEFAULT_FORMAT;
1616

1717
public:
1818
ImageBackend(int width, int height);
19-
~ImageBackend();
2019
static Backend *construct(int width, int height);
2120

2221
cairo_format_t getFormat();

0 commit comments

Comments
 (0)