diff --git a/Libraries/LibGfx/Font.cpp b/Libraries/LibGfx/Font.cpp index c2a01d5b62..8fb431b1d2 100644 --- a/Libraries/LibGfx/Font.cpp +++ b/Libraries/LibGfx/Font.cpp @@ -103,30 +103,28 @@ Font& Font::default_bold_font() NonnullRefPtr Font::clone() const { size_t bytes_per_glyph = sizeof(u32) * glyph_height(); - // FIXME: This is leaked! - auto* new_rows = static_cast(kmalloc(bytes_per_glyph * m_glyph_count)); + auto* new_rows = static_cast(malloc(bytes_per_glyph * m_glyph_count)); memcpy(new_rows, m_rows, bytes_per_glyph * m_glyph_count); - auto* new_widths = static_cast(kmalloc(m_glyph_count)); + auto* new_widths = static_cast(malloc(m_glyph_count)); if (m_glyph_widths) memcpy(new_widths, m_glyph_widths, m_glyph_count); else memset(new_widths, m_glyph_width, m_glyph_count); - return adopt(*new Font(m_name, m_family, new_rows, new_widths, m_fixed_width, m_glyph_width, m_glyph_height, m_glyph_spacing, m_type, m_baseline, m_mean_line, m_presentation_size, m_weight)); + return adopt(*new Font(m_name, m_family, new_rows, new_widths, m_fixed_width, m_glyph_width, m_glyph_height, m_glyph_spacing, m_type, m_baseline, m_mean_line, m_presentation_size, m_weight, true)); } NonnullRefPtr Font::create(u8 glyph_height, u8 glyph_width, bool fixed, FontTypes type) { size_t bytes_per_glyph = sizeof(u32) * glyph_height; - // FIXME: This is leaked! size_t count = glyph_count_by_type(type); auto* new_rows = static_cast(malloc(bytes_per_glyph * count)); memset(new_rows, 0, bytes_per_glyph * count); auto* new_widths = static_cast(malloc(count)); memset(new_widths, glyph_width, count); - return adopt(*new Font("Untitled", "Untitled", new_rows, new_widths, fixed, glyph_width, glyph_height, 1, type, 0, 0, 0, 400)); + return adopt(*new Font("Untitled", "Untitled", new_rows, new_widths, fixed, glyph_width, glyph_height, 1, type, 0, 0, 0, 400, true)); } -Font::Font(String name, String family, unsigned* rows, u8* widths, bool is_fixed_width, u8 glyph_width, u8 glyph_height, u8 glyph_spacing, FontTypes type, u8 baseline, u8 mean_line, u8 presentation_size, u16 weight) +Font::Font(String name, String family, unsigned* rows, u8* widths, bool is_fixed_width, u8 glyph_width, u8 glyph_height, u8 glyph_spacing, FontTypes type, u8 baseline, u8 mean_line, u8 presentation_size, u16 weight, bool owns_arrays) : m_name(name) , m_family(family) , m_type(type) @@ -142,6 +140,7 @@ Font::Font(String name, String family, unsigned* rows, u8* widths, bool is_fixed , m_presentation_size(presentation_size) , m_weight(weight) , m_fixed_width(is_fixed_width) + , m_owns_arrays(owns_arrays) { update_x_height(); @@ -163,6 +162,10 @@ Font::Font(String name, String family, unsigned* rows, u8* widths, bool is_fixed Font::~Font() { + if (m_owns_arrays) { + free(m_glyph_widths); + free(m_rows); + } } RefPtr Font::load_from_memory(const u8* data) diff --git a/Libraries/LibGfx/Font.h b/Libraries/LibGfx/Font.h index d62b2bdb6c..7e7069ed49 100644 --- a/Libraries/LibGfx/Font.h +++ b/Libraries/LibGfx/Font.h @@ -154,7 +154,7 @@ public: String qualified_name() const; private: - Font(String name, String family, unsigned* rows, u8* widths, bool is_fixed_width, u8 glyph_width, u8 glyph_height, u8 glyph_spacing, FontTypes type, u8 baseline, u8 mean_line, u8 presentation_size, u16 weight); + Font(String name, String family, unsigned* rows, u8* widths, bool is_fixed_width, u8 glyph_width, u8 glyph_height, u8 glyph_spacing, FontTypes type, u8 baseline, u8 mean_line, u8 presentation_size, u16 weight, bool owns_arrays = false); static RefPtr load_from_memory(const u8*); static size_t glyph_count_by_type(FontTypes type); @@ -186,6 +186,7 @@ private: bool m_fixed_width { false }; bool m_boldface { false }; + bool m_owns_arrays { false }; }; }