[fix] fix system_chinese_font_ usage to avoid dangling font pointer after closing stream window

This commit is contained in:
dijunkun
2025-12-01 23:03:45 +08:00
parent 280e011ae4
commit b50f386713
4 changed files with 43 additions and 21 deletions

View File

@@ -681,7 +681,7 @@ int Render::CreateMainWindow() {
// for window region action
SDL_SetWindowHitTest(main_window_, HitTestCallback, this);
SetupFontAndStyle();
SetupFontAndStyle(true);
ImGuiStyle& style = ImGui::GetStyle();
style.ScaleAllSizes(dpi_scale_);
@@ -750,7 +750,7 @@ int Render::CreateStreamWindow() {
// for window region action
SDL_SetWindowHitTest(stream_window_, HitTestCallback, this);
SetupFontAndStyle();
SetupFontAndStyle(false);
SDL_SetRenderScale(stream_renderer_, dpi_scale_, dpi_scale_);
@@ -793,7 +793,7 @@ int Render::DestroyStreamWindow() {
return 0;
}
int Render::SetupFontAndStyle() {
int Render::SetupFontAndStyle(bool main_window) {
float font_size = 32.0f;
// Setup Dear ImGui style
@@ -815,7 +815,11 @@ int Render::SetupFontAndStyle() {
// Load system Chinese font as fallback
config.MergeMode = false;
config.FontDataOwnedByAtlas = false;
system_chinese_font_ = nullptr;
if (main_window) {
main_windows_system_chinese_font_ = nullptr;
} else {
stream_windows_system_chinese_font_ = nullptr;
}
#if defined(_WIN32)
// Windows: Try Microsoft YaHei (微软雅黑) first, then SimSun (宋体)
@@ -841,21 +845,38 @@ int Render::SetupFontAndStyle() {
std::ifstream font_file(font_paths[i], std::ios::binary);
if (font_file.good()) {
font_file.close();
system_chinese_font_ =
if (main_window) {
main_windows_system_chinese_font_ =
io.Fonts->AddFontFromFileTTF(font_paths[i], font_size, &config,
io.Fonts->GetGlyphRangesChineseFull());
if (system_chinese_font_ != nullptr) {
if (main_windows_system_chinese_font_ != nullptr) {
LOG_INFO("Loaded system Chinese font: {}", font_paths[i]);
break;
}
} else {
stream_windows_system_chinese_font_ =
io.Fonts->AddFontFromFileTTF(font_paths[i], font_size, &config,
io.Fonts->GetGlyphRangesChineseFull());
if (stream_windows_system_chinese_font_ != nullptr) {
LOG_INFO("Loaded system Chinese font: {}", font_paths[i]);
break;
}
}
}
}
// If no system font found, use default font
if (system_chinese_font_ == nullptr) {
system_chinese_font_ = io.Fonts->AddFontDefault(&config);
if (main_window) {
if (main_windows_system_chinese_font_ == nullptr) {
main_windows_system_chinese_font_ = io.Fonts->AddFontDefault(&config);
LOG_WARN("System Chinese font not found, using default font");
}
} else {
if (stream_windows_system_chinese_font_ == nullptr) {
stream_windows_system_chinese_font_ = io.Fonts->AddFontDefault(&config);
LOG_WARN("System Chinese font not found, using default font");
}
}
ImGui::StyleColorsLight();

View File

@@ -180,7 +180,7 @@ class Render {
int DestroyMainWindow();
int CreateStreamWindow();
int DestroyStreamWindow();
int SetupFontAndStyle();
int SetupFontAndStyle(bool main_window);
int DestroyMainWindowContext();
int DestroyStreamWindowContext();
int DrawMainWindow();
@@ -313,7 +313,8 @@ class Render {
SDL_Window* main_window_ = nullptr;
SDL_Renderer* main_renderer_ = nullptr;
ImGuiContext* main_ctx_ = nullptr;
ImFont* system_chinese_font_ = nullptr; // System Chinese font for fallback
ImFont* main_windows_system_chinese_font_ = nullptr;
ImFont* stream_windows_system_chinese_font_ = nullptr;
bool exit_ = false;
const int sdl_refresh_ms_ = 16; // ~60 FPS
#if _WIN32

View File

@@ -138,8 +138,8 @@ int Render::RequestPermissionWindow() {
ImGui::SetWindowFontScale(0.3f);
// use system font
if (system_chinese_font_ != nullptr) {
ImGui::PushFont(system_chinese_font_);
if (main_windows_system_chinese_font_ != nullptr) {
ImGui::PushFont(main_windows_system_chinese_font_);
}
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + ImGui::GetTextLineHeight() + 5.0f);
@@ -191,7 +191,7 @@ int Render::RequestPermissionWindow() {
ImGui::SetWindowFontScale(0.45f);
// pop system font
if (system_chinese_font_ != nullptr) {
if (main_windows_system_chinese_font_ != nullptr) {
ImGui::PopFont();
}

View File

@@ -111,8 +111,8 @@ int Render::UpdateNotificationWindow() {
float scrollable_height =
window_height - UPDATE_NOTIFICATION_RESERVED_HEIGHT;
if (system_chinese_font_ != nullptr) {
ImGui::PushFont(system_chinese_font_);
if (main_windows_system_chinese_font_ != nullptr) {
ImGui::PushFont(main_windows_system_chinese_font_);
}
// scrollable content area
ImGui::SetCursorPosX(window_width * 0.05f);
@@ -163,7 +163,7 @@ int Render::UpdateNotificationWindow() {
ImGui::EndChild();
// pop system font
if (system_chinese_font_ != nullptr) {
if (main_windows_system_chinese_font_ != nullptr) {
ImGui::PopFont();
}