From 59c72c95b5fdd756d4b0134fad32f7a3f4daa2aa Mon Sep 17 00:00:00 2001 From: skal Date: Tue, 3 Feb 2026 08:42:12 +0100 Subject: fix(assets): Resolve static initialization order fiasco Replaces the global array with which wraps a local static array. This ensures the asset table is initialized on first use, preventing crashes when other globals (like shader strings) try to access assets during dynamic initialization. --- tools/asset_packer.cc | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'tools/asset_packer.cc') diff --git a/tools/asset_packer.cc b/tools/asset_packer.cc index d86e29b..2de57d0 100644 --- a/tools/asset_packer.cc +++ b/tools/asset_packer.cc @@ -86,7 +86,7 @@ int main(int argc, char* argv[]) { assets_data_cc_file, "// This file is auto-generated by asset_packer.cc. Do not edit.\n\n"); fprintf(assets_data_cc_file, "#include \"util/asset_manager.h\"\n"); - fprintf(assets_data_cc_file, "#include \"%s\"\n\n", + fprintf(assets_data_cc_file, "#include \"%s\"\n", generated_header_name.c_str()); // Forward declare procedural functions for AssetRecord initialization @@ -196,6 +196,10 @@ int main(int argc, char* argv[]) { fprintf(assets_h_file, "#include \"util/asset_manager.h\"\n"); // Include here AFTER enum // definition + fprintf(assets_h_file, "\n// Accessors to avoid static initialization order issues\n"); + fprintf(assets_h_file, "const struct AssetRecord* GetAssetRecordTable();\n"); + fprintf(assets_h_file, "size_t GetAssetCount();\n"); + std::fclose(assets_h_file); for (const auto& info : asset_build_infos) { @@ -238,7 +242,8 @@ int main(int argc, char* argv[]) { } } - fprintf(assets_data_cc_file, "extern const AssetRecord g_assets[] = {\n"); + fprintf(assets_data_cc_file, "const AssetRecord* GetAssetRecordTable() {\n"); + fprintf(assets_data_cc_file, " static const AssetRecord assets[] = {\n"); for (const auto& info : asset_build_infos) { fprintf(assets_data_cc_file, " { "); if (info.is_procedural) { @@ -251,10 +256,13 @@ int main(int argc, char* argv[]) { } fprintf(assets_data_cc_file, " },\n"); } - fprintf(assets_data_cc_file, "};\n"); - fprintf(assets_data_cc_file, - "extern const size_t g_assets_count = sizeof(g_assets) / " - "sizeof(g_assets[0]);\n"); + fprintf(assets_data_cc_file, " };\n"); + fprintf(assets_data_cc_file, " return assets;\n"); + fprintf(assets_data_cc_file, "}\n\n"); + + fprintf(assets_data_cc_file, "size_t GetAssetCount() {\n"); + fprintf(assets_data_cc_file, " return %zu;\n", asset_build_infos.size()); + fprintf(assets_data_cc_file, "}\n\n"); std::fclose(assets_data_cc_file); -- cgit v1.2.3