Skip to content

Commit

Permalink
fix: some more memory leaks (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
DjDeveloperr authored Jan 10, 2023
1 parent dc6dcc0 commit a700bd1
Showing 1 changed file with 48 additions and 9 deletions.
57 changes: 48 additions & 9 deletions native/src/context2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@

extern sk_sp<skia::textlayout::FontCollection> fontCollection;

void free_style(Style* style) {
if (style->shader != nullptr) {
style->shader.~sk_sp();
}
}

void free_font(Font* font) {
free(font->family);
free(font);
}

Style clone_style(Style* style) {
Style new_style;
new_style.type = style->type;
if (style->type == kStyleColor) {
new_style.color = style->color;
} else if (style->type == kStyleShader) {
new_style.shader = sk_sp(style->shader);
}
return new_style;
}

sk_context_state* create_default_state() {
sk_context_state* state = new sk_context_state();
state->paint = new SkPaint();
Expand Down Expand Up @@ -53,10 +75,8 @@ sk_context_state* clone_context_state(sk_context_state* state) {
new_state->lineDash = state->lineDash;
new_state->globalAlpha = state->globalAlpha;
new_state->lineDashOffset = state->lineDashOffset;
new_state->fillStyle = state->fillStyle;
if (new_state->fillStyle.shader) new_state->fillStyle.shader = sk_sp(new_state->fillStyle.shader);
new_state->strokeStyle = state->strokeStyle;
if (new_state->strokeStyle.shader) new_state->strokeStyle.shader = sk_sp(new_state->strokeStyle.shader);
new_state->fillStyle = clone_style(&state->fillStyle);
new_state->strokeStyle = clone_style(&state->strokeStyle);
new_state->shadowColor = state->shadowColor;
new_state->transform = new SkMatrix(*state->transform);
new_state->imageSmoothingEnabled = state->imageSmoothingEnabled;
Expand All @@ -78,12 +98,12 @@ sk_context_state* clone_context_state(sk_context_state* state) {
}

void free_context_state(sk_context_state* state) {
if (state->fillStyle.shader) state->fillStyle.shader.~sk_sp();
if (state->strokeStyle.shader) state->strokeStyle.shader.~sk_sp();
free_style(&state->fillStyle);
free_style(&state->strokeStyle);
if (state->filter.get() != nullptr) state->filter.~sk_sp();
delete state->paint;
delete state->transform;
free(state->font);
free_font(state->font);
}

// Utility
Expand Down Expand Up @@ -215,6 +235,7 @@ void applyShadowOffsetMatrix(sk_context* context) {
shadowOffset->preTranslate(shadowOffsetX, shadowOffsetY);
canvas->concat(*shadowOffset);
canvas->concat(cts);
delete shadowOffset;
}

extern "C" {
Expand Down Expand Up @@ -242,8 +263,10 @@ extern "C" {
applyShadowOffsetMatrix(context);
canvas->drawRect(rect, *shadowPaint);
canvas->restore();
delete shadowPaint;
}
canvas->drawRect(rect, *fillPaint);
delete fillPaint;
}

// Context.strokeRect()
Expand All @@ -257,8 +280,10 @@ extern "C" {
applyShadowOffsetMatrix(context);
canvas->drawRect(rect, *shadowPaint);
canvas->restore();
delete shadowPaint;
}
canvas->drawRect(rect, *strokePaint);
delete strokePaint;
}

/// Drawing text
Expand Down Expand Up @@ -446,10 +471,11 @@ extern "C" {
shadowPaint
);
context->canvas->restore();
delete shadowPaint;
if (res == 0) return 0;
}
}
return sk_context_text_base(
auto res = sk_context_text_base(
context,
text,
textLen,
Expand All @@ -460,6 +486,8 @@ extern "C" {
out_metrics,
paint
);
delete paint;
return res;
}

// Context.fillText() implementation in JS using sk_context_test
Expand Down Expand Up @@ -575,7 +603,7 @@ extern "C" {
int variant,
int stretch
) {
free(context->state->font);
free_font(context->state->font);
context->state->font = new Font();
context->state->font->family = strdup(family);
context->state->font->size = size;
Expand Down Expand Up @@ -657,6 +685,7 @@ extern "C" {
int sk_context_set_fill_style(sk_context* context, char* style) {
auto color = CSSColorParser::parse(std::string(style));
if (color) {
free_style(&context->state->fillStyle);
auto val = color.value();
context->state->fillStyle = Style();
context->state->fillStyle.type = kStyleColor;
Expand All @@ -667,12 +696,14 @@ extern "C" {
}

void sk_context_set_fill_style_gradient(sk_context* context, sk_gradient* gradient) {
free_style(&context->state->fillStyle);
context->state->fillStyle = Style();
context->state->fillStyle.type = kStyleShader;
context->state->fillStyle.shader = sk_gradient_to_shader(gradient, context->state->transform);
}

void sk_context_set_fill_style_pattern(sk_context* context, sk_pattern* pattern) {
free_style(&context->state->fillStyle);
context->state->fillStyle = Style();
context->state->fillStyle.type = kStyleShader;
context->state->fillStyle.shader = sk_pattern_to_shader(pattern);
Expand All @@ -684,6 +715,7 @@ extern "C" {
int sk_context_set_stroke_style(sk_context* context, char* style) {
auto color = CSSColorParser::parse(std::string(style));
if (color) {
free_style(&context->state->strokeStyle);
auto val = color.value();
context->state->strokeStyle = Style();
context->state->strokeStyle.type = kStyleColor;
Expand All @@ -694,12 +726,14 @@ extern "C" {
}

void sk_context_set_stroke_style_gradient(sk_context* context, sk_gradient* gradient) {
free_style(&context->state->strokeStyle);
context->state->strokeStyle = Style();
context->state->strokeStyle.type = kStyleShader;
context->state->strokeStyle.shader = sk_gradient_to_shader(gradient, context->state->transform);
}

void sk_context_set_stroke_style_pattern(sk_context* context, sk_pattern* pattern) {
free_style(&context->state->strokeStyle);
context->state->strokeStyle = Style();
context->state->strokeStyle.type = kStyleShader;
context->state->strokeStyle.shader = sk_pattern_to_shader(pattern);
Expand Down Expand Up @@ -831,8 +865,10 @@ extern "C" {
applyShadowOffsetMatrix(context);
canvas->drawPath(*path, *shadowPaint);
canvas->restore();
delete shadowPaint;
}
canvas->drawPath(*path, *paint);
delete paint;
}

// Context.stroke()
Expand All @@ -846,8 +882,10 @@ extern "C" {
applyShadowOffsetMatrix(context);
canvas->drawPath(*path, *shadowPaint);
canvas->restore();
delete shadowPaint;
}
canvas->drawPath(*path, *strokePaint);
delete strokePaint;
}

// TODO?: Context.drawFocusIfNeeded() (should we support it?)
Expand Down Expand Up @@ -1080,6 +1118,7 @@ extern "C" {
shadowPaint,
SkCanvas::kFast_SrcRectConstraint
);
delete shadowPaint;
}

context->canvas->drawImageRect(
Expand Down

0 comments on commit a700bd1

Please sign in to comment.