Skip to content

Fix memory leaks in SetFont/ResolveFontDescription #1340

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

Merged
merged 2 commits into from
Jan 10, 2019

Conversation

zbjornson
Copy link
Collaborator

Also about 55% faster and 9 fewer LOC.

Fixes #1202

image

  • Have you updated CHANGELOG.md?

@zbjornson zbjornson mentioned this pull request Dec 22, 2018
@chearon
Copy link
Collaborator

chearon commented Dec 26, 2018

Will review soon, do you know which reference wasn't being freed?

pango_font_description_set_family_static(ret, resolved_families->str);

g_strfreev(families);
g_string_free(resolved_families, false);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forget exactly, but this might have been one leak (perhaps only when setting the font to a value that has already been used before). I don't think resolved_families's character data is ever free'd.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. The false is because due to pango_font_description_set_family_static, I wanted to share the same pointer, but forgot to free it somewhere.


for (; it != _font_face_list.end(); ++it) {
if (g_ascii_strcasecmp(families[i], pango_font_description_get_family(it->user_desc)) == 0) {
char *name = g_strdup(pango_font_description_get_family(it->sys_desc));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I think this was the other. g_hash_table_destroy doesn't do anything to free its contained data unless the table is created with key/value_destroy_funcs.

src/Canvas.cc Outdated

std::vector<FontFace> Canvas::_font_face_list = _init_font_face_list();

std::vector<FontFace> Canvas::_font_face_list;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little worried I made _init_font_face_list for some esoteric reason but it doesn't look necessary. That said do we even need the green line?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was wondering if at some point _init_font_face_list was being called more than once, but couldn't find evidence of that.

The green line is necessary (static members have to be initialized in the source and not the header so they're not redefined).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I don't really see a reason why it needs to be a member of Canvas; it could probably just be in the source file unless you know of a reason it might be needed externally?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be in the source file yeah, as long as there's one list per process since that's how system fonts get registered. Will merge as-is or with that change if you want it.

Copy link
Collaborator Author

@zbjornson zbjornson Jan 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved it to the source file (since I messed up the newline after it originally 😛). Should be ready to go!

If you and @LinusU are preparing to release, #1337 fixes an important regression and should be merged as well.

g_string_free(renamed_families, true);
if (resolved_families.size()) resolved_families += ',';
resolved_families += renamed_families.size() ? renamed_families : family;
first = false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job preserving all of the existing logic 👍

pango_font_description_set_family_static(ret, resolved_families->str);

g_strfreev(families);
g_string_free(resolved_families, false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. The false is because due to pango_font_description_set_family_static, I wanted to share the same pointer, but forgot to free it somewhere.

Moves the AdjustExternalMemory calls to always be adjacent to cairo_image_surface_create/destroy so they can't be used incorrectly.

See Automattic#1202 (comment)
Copy link
Collaborator

@LinusU LinusU left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't had time to look too closely, but seems really nice! 👏

@LinusU LinusU mentioned this pull request Jan 10, 2019
@chearon chearon merged commit af58a74 into Automattic:master Jan 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants