Skip to content

Commit bfd5d50

Browse files
committed
cache: fix crash on release build
1 parent c75d183 commit bfd5d50

4 files changed

Lines changed: 21 additions & 30 deletions

File tree

include/cache.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ struct cache_entry_t
2121
toml::table tbl = {};
2222
};
2323

24-
extern std::unordered_map<CacheFilesEnum, cache_entry_t> g_cache_files;
25-
2624
class Cache
2725
{
2826
public:
@@ -84,8 +82,14 @@ class Cache
8482
}
8583
}
8684

85+
std::unordered_map<CacheFilesEnum, cache_entry_t>& GetEntries() { return m_cache_entries; }
86+
8787
private:
8888
std::string m_cache_dir_path;
89+
90+
std::unordered_map<CacheFilesEnum, cache_entry_t> m_cache_entries = {
91+
{ CacheFilesEnum::Colors, { "colors.toml", "cache.default-color-picker-color" } }
92+
};
8993
};
9094

9195
extern std::unique_ptr<Cache> g_cache;

include/screenshot_tool.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class ScreenshotTool
135135
ScreenshotTool()
136136
: m_io(dummy),
137137
m_inputs{ g_config->File.ocr_path, g_config->File.ocr_model, {}, "", {}, "", "" },
138-
m_current_color(rgba_t(Cache::GetValue(g_cache_files[CacheFilesEnum::Colors], 0xFF0000FF)))
138+
m_current_color(rgba_t(Cache::GetValue(g_cache->GetEntries()[CacheFilesEnum::Colors], 0xFF0000FF)))
139139
{}
140140

141141
Result<> Start();

src/cache.cpp

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,23 @@
88
#include "toml++/toml.hpp"
99
#include "util.hpp"
1010

11-
std::unordered_map<CacheFilesEnum, cache_entry_t> g_cache_files = {
12-
{ CacheFilesEnum::Colors, { "colors.toml", "cache.default-color-picker-color" } }
13-
};
14-
15-
static fs::path old_pwd;
16-
static void cd(const fs::path& path)
11+
struct CdGuard
1712
{
18-
if (!path.empty())
13+
fs::path saved;
14+
CdGuard(const fs::path& p) : saved(fs::current_path())
1915
{
20-
old_pwd = fs::current_path();
21-
fs::current_path(path);
16+
if (!p.empty())
17+
fs::current_path(p);
2218
}
23-
}
24-
25-
static void cd_back()
26-
{
27-
if (!old_pwd.empty())
28-
fs::current_path(old_pwd);
29-
}
19+
~CdGuard() { fs::current_path(saved); }
20+
};
3021

3122
// https://github.com/hyprwm/Hyprland/blob/2d2a5bebff72c73cd27db3b9e954b8fa2a7623e8/hyprpm/src/core/DataState.cpp#L24
3223
static bool write_cache(const std::string& str, const std::string& to)
3324
{
3425
// create temp file in a safe temp root
35-
const fs::path& temp_state = (fs::temp_directory_path() / ".temp-state");
36-
std::ofstream of(temp_state, std::ios::trunc);
26+
const fs::path temp_state = (fs::temp_directory_path() / ".temp-state");
27+
std::ofstream of(temp_state, std::ios::trunc);
3728
if (!of.good())
3829
return false;
3930

@@ -52,12 +43,12 @@ Cache::Cache(const std::string& cache_dir) : m_cache_dir_path(cache_dir)
5243
}
5344

5445
for (int i = 0; i < int(idx(CacheFilesEnum::COUNT)); ++i)
55-
LoadCacheFile(g_cache_files[enum_<CacheFilesEnum>(i)]);
46+
LoadCacheFile(m_cache_entries[enum_<CacheFilesEnum>(i)]);
5647
}
5748

5849
Cache::~Cache()
5950
{
60-
for (const auto& [_, cache] : g_cache_files)
51+
for (const auto& [_, cache] : m_cache_entries)
6152
GenerateCacheFile(cache);
6253
}
6354

@@ -66,7 +57,7 @@ Result<> Cache::LoadCacheFile(cache_entry_t& entry)
6657
// Since the filename (default.theme-file) will be likely
6758
// related to relative path of the config directory, let's
6859
// snapshot and switch to that directory.
69-
cd(m_cache_dir_path);
60+
CdGuard guard(m_cache_dir_path);
7061

7162
if (fs::exists(entry.file_path))
7263
{
@@ -76,8 +67,6 @@ Result<> Cache::LoadCacheFile(cache_entry_t& entry)
7667
}
7768
catch (const toml::parse_error& err)
7869
{
79-
// Snap back
80-
cd_back();
8170
return Err(
8271
fmt::format("Parsing cache file '{}' failed:\n"
8372
"{}\n"
@@ -89,13 +78,12 @@ Result<> Cache::LoadCacheFile(cache_entry_t& entry)
8978
}
9079
}
9180

92-
cd_back();
9381
return Ok();
9482
}
9583

9684
void Cache::GenerateCacheFile(const cache_entry_t& entry)
9785
{
98-
cd(m_cache_dir_path);
86+
CdGuard guard(m_cache_dir_path);
9987

10088
std::stringstream ss;
10189
ss << "# AUTO-GENERATED FILE. DO NOT EDIT THIS FILE.\n";
@@ -104,5 +92,4 @@ void Cache::GenerateCacheFile(const cache_entry_t& entry)
10492

10593
if (!write_cache(ss.str(), entry.file_path))
10694
error("Failed to write cache entry at path '{}'", entry.file_path);
107-
cd_back();
10895
}

src/screenshot_tool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,7 @@ void ScreenshotTool::DrawBarDecodeTools()
13851385

13861386
void ScreenshotTool::DrawAnnotationToolbar()
13871387
{
1388-
cache_entry_t& cache_entry = g_cache_files[CacheFilesEnum::Colors];
1388+
cache_entry_t& cache_entry = g_cache->GetEntries()[CacheFilesEnum::Colors];
13891389
static int item_picker = 0;
13901390
static constexpr const char* color_pickers[2] = { "Bar - Square", "Wheel - Triangle" };
13911391

0 commit comments

Comments
 (0)