Skip to content

Commit 4075599

Browse files
committed
FIX avoid memory leak in FontRegistry
- use SiD based font ids
1 parent 4b06be2 commit 4075599

File tree

8 files changed

+36
-48
lines changed

8 files changed

+36
-48
lines changed

src/text/FontAtlas.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ namespace text
8383
m_atlasSize == o.m_atlasSize;
8484
}
8585

86-
bool FontAtlas::valid()
86+
bool FontAtlas::valid() const
8787
{
8888
return m_fontHandle && m_fontHandle->valid();
8989
}

src/text/FontAtlas.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ namespace text
3232

3333
bool operator==(const FontAtlas& o) const noexcept;
3434

35-
bool valid();
35+
bool valid() const;
3636

3737
void prepare();
3838

3939
void update();
4040

41-
FontHandle* getFont() {
41+
FontHandle* getFont() const
42+
{
4243
return m_fontHandle.get();
4344
}
4445

src/text/FontHandle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace text
4646
//ftgl::texture_font_delete(m_font);
4747
}
4848

49-
bool FontHandle::valid()
49+
bool FontHandle::valid() const
5050
{
5151
return m_font != nullptr;
5252
}

src/text/FontHandle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace text
1616
FontHandle(FontHandle&& o) noexcept;
1717
~FontHandle();
1818

19-
bool valid();
19+
bool valid() const;
2020

2121
void create(
2222
const std::string& fullPath,

src/text/FontRegistry.cpp

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#include "FontRegistry.h"
22

3+
#include <fmt/format.h>
4+
35
#include "engine/UpdateContext.h"
46

7+
#include "ki/sid.h"
8+
59
#include "kigl/GLState.h"
610

711
namespace
@@ -60,46 +64,35 @@ namespace text {
6064
{
6165
std::shared_lock lock(m_lock);
6266

63-
for (auto& font : m_fonts) {
67+
for (auto& [fontId, font] : m_fonts) {
6468
font.prepare();
6569
font.update();
6670
}
6771
}
6872

69-
//text::FontAtlas* FontRegistry::modifyFont(text::font_id id)
70-
//{
71-
// if (id < 1) return nullptr;
72-
//
73-
// std::shared_lock lock(m_lock);
74-
// assert(id > 0 && id <= m_fonts.size());
75-
//
76-
// return &m_fonts[id - 1];
77-
//}
78-
7973
text::font_id FontRegistry::registerFont(
8074
text::FontAtlas&& src)
8175
{
8276
std::unique_lock lock(m_lock);
8377

84-
text::font_id fontId = findFont(src);
85-
if (!fontId) {
86-
auto& font = m_fonts.emplace_back(std::move(src));
87-
font.m_id = static_cast<text::font_id>(m_fonts.size());
88-
fontId = font.m_id;
89-
}
78+
text::font_id fontId;
79+
{
80+
std::string key = fmt::format(
81+
"{}_{}_{}",
82+
src.m_name,
83+
src.m_fontPath,
84+
src.m_fontSize);
9085

91-
return fontId;
92-
}
86+
fontId = SID(key);
87+
}
9388

94-
text::font_id FontRegistry::findFont(
95-
const text::FontAtlas& src) const noexcept
96-
{
97-
const auto& it = std::find_if(
98-
m_fonts.begin(), m_fonts.end(),
99-
[&src](const auto& font) {
100-
return font == src;
101-
});
89+
const auto& it = m_fonts.find(fontId);
90+
if (it == m_fonts.end())
91+
{
92+
src.m_id = fontId;
93+
m_fonts.insert({ fontId, std::move(src) });
94+
}
10295

103-
return it != m_fonts.end() ? it->m_id : 0;
96+
return fontId;
10497
}
10598
}

src/text/FontRegistry.h

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <vector>
4+
#include <unordered_map>
45
#include <shared_mutex>
56

67
#include "text/FontAtlas.h"
@@ -23,32 +24,24 @@ namespace text {
2324

2425
void updateRT(const UpdateContext& ctx);
2526

26-
text::FontAtlas* getDefaultFontAtlas() noexcept
27+
const text::FontAtlas* getDefaultFontAtlas() const noexcept
2728
{
2829
return getFontAtlas(m_defaultFontId);
2930
}
3031

31-
text::FontAtlas* getFontAtlas(text::font_id id) noexcept
32+
const text::FontAtlas* getFontAtlas(text::font_id id) const noexcept
3233
{
33-
if (id < 1) return nullptr;
34-
35-
std::shared_lock lock(m_lock);
36-
if (id < 1 || id > m_fonts.size()) return nullptr;
37-
38-
return m_fonts[id - 1].valid() ? & m_fonts[id - 1] : nullptr;
34+
const auto& it = m_fonts.find(id);
35+
return it != m_fonts.end() ? &it->second : nullptr;
3936
}
4037

4138
text::font_id registerFont(
4239
text::FontAtlas&& src);
4340

44-
private:
45-
text::font_id findFont(
46-
const text::FontAtlas& src) const noexcept;
47-
4841
private:
4942
mutable std::shared_mutex m_lock{};
5043

51-
std::vector<text::FontAtlas> m_fonts;
44+
std::unordered_map<text::font_id, text::FontAtlas> m_fonts;
5245

5346
text::font_id m_defaultFontId{ 0 };
5447
};

src/text/TextDraw.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace
4545
//
4646
void addText(
4747
mesh::TextMesh* mesh,
48-
text::FontAtlas* fontAtlas,
48+
const text::FontAtlas* fontAtlas,
4949
std::string_view text,
5050
const glm::vec2& pivot,
5151
text::Align alignHorizontal,

src/text/size.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44

55
namespace text
66
{
7-
typedef uint8_t font_id;
7+
// SID
8+
typedef uint32_t font_id;
89
}

0 commit comments

Comments
 (0)