Fixed on-close crashes coming from SHResourceManager

This commit is contained in:
Kah Wei 2023-01-29 18:51:31 +08:00
parent eab2f2d54a
commit 6af4933b52
6 changed files with 43 additions and 10 deletions

View File

@ -239,7 +239,13 @@ namespace Sandbox
SDL_Quit(); SDL_Quit();
#endif #endif
// Unload scenes
SHSceneManager::Exit(); SHSceneManager::Exit();
// Free all remaining resources
SHResourceManager::UnloadAll();
// Shut down engine
SHSystemManager::Exit(); SHSystemManager::Exit();
SHAssetManager::Exit(); SHAssetManager::Exit();
} }

View File

@ -424,6 +424,10 @@ namespace SHADE
const auto& MATRICES = animator->GetBoneMatrices(); const auto& MATRICES = animator->GetBoneMatrices();
boneMatrixData.insert(boneMatrixData.end(), MATRICES.cbegin(), MATRICES.cend()); boneMatrixData.insert(boneMatrixData.end(), MATRICES.cbegin(), MATRICES.cend());
} }
else
{
// TODO: Populate with empty matrices
}
// We don't have to account for missing animators or reset indices as the renderable list are not updated at this point // We don't have to account for missing animators or reset indices as the renderable list are not updated at this point
} }

View File

@ -176,12 +176,14 @@ namespace SHADE
/* Type Definition */ /* Type Definition */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
using LibraryDeleter = std::function<void()>; using LibraryDeleter = std::function<void()>;
using LibraryClearer = std::function<void()>;
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Data Members */ /* Data Members */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
std::unordered_map<std::type_index, void*> resourceLibs; // TODO: Replace with compile time map std::unordered_map<std::type_index, void*> resourceLibs; // TODO: Replace with compile time map
std::vector<LibraryDeleter> deleters; std::vector<LibraryDeleter> deleters;
std::unordered_map<std::type_index, LibraryClearer> clearers;
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Helper Functions */ /* Helper Functions */

View File

@ -23,7 +23,8 @@ namespace SHADE
SHResourceLibrary<SHRigNode> SHResourceManager::rigNodeStore; SHResourceLibrary<SHRigNode> SHResourceManager::rigNodeStore;
std::unordered_map<std::type_index, std::unordered_map<AssetID, Handle<void>>> SHResourceManager::handlesMap; std::unordered_map<std::type_index, std::unordered_map<AssetID, Handle<void>>> SHResourceManager::handlesMap;
std::unordered_map<std::type_index, SHResourceManager::HandleAssetMap> SHResourceManager::assetIdMap; std::unordered_map<std::type_index, SHResourceManager::HandleAssetMap> SHResourceManager::assetIdMap;
std::unordered_map<std::type_index, std::function<void(AssetID)>> SHResourceManager::typedFreeFuncMap; std::unordered_map<std::type_index, SHResourceManager::AssetFreeFunc> SHResourceManager::typedFreeFuncMap;
std::unordered_map<std::type_index, SHResourceManager::LibraryClearer> SHResourceManager::typedFreeAllFuncMap;
std::vector<AssetID> SHResourceManager::loadedAssetData; std::vector<AssetID> SHResourceManager::loadedAssetData;
bool SHResourceManager::textureChanged = false; bool SHResourceManager::textureChanged = false;
bool SHResourceManager::meshChanged = false; bool SHResourceManager::meshChanged = false;
@ -92,6 +93,14 @@ namespace SHADE
loadedAssetData.clear(); loadedAssetData.clear();
} }
void SHResourceManager::UnloadAll()
{
for (auto func : typedFreeAllFuncMap)
{
func.second();
}
}
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Query Functions */ /* Query Functions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/

View File

@ -111,6 +111,10 @@ namespace SHADE
/// Needs to be called to finalise all changes to loads, unless at runtime. /// Needs to be called to finalise all changes to loads, unless at runtime.
/// </summary> /// </summary>
static void FinaliseChanges(); static void FinaliseChanges();
/// <summary>
/// Unloads all loaded resources.
/// </summary>
static void UnloadAll();
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Query Functions */ /* Query Functions */
@ -172,6 +176,8 @@ namespace SHADE
using HandleAssetMap = std::unordered_map<Handle<void>, AssetID>; using HandleAssetMap = std::unordered_map<Handle<void>, AssetID>;
using AssetHandleMapRef = std::reference_wrapper<AssetHandleMap>; using AssetHandleMapRef = std::reference_wrapper<AssetHandleMap>;
using HandleAssetMapRef = std::reference_wrapper<HandleAssetMap>; using HandleAssetMapRef = std::reference_wrapper<HandleAssetMap>;
using AssetFreeFunc = std::function<void(AssetID)>;
using LibraryClearer = std::function<void()>;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Data Members */ /* Data Members */
@ -181,7 +187,8 @@ namespace SHADE
static SHResourceLibrary<SHRigNode> rigNodeStore; static SHResourceLibrary<SHRigNode> rigNodeStore;
static std::unordered_map<std::type_index, AssetHandleMap> handlesMap; static std::unordered_map<std::type_index, AssetHandleMap> handlesMap;
static std::unordered_map<std::type_index, HandleAssetMap> assetIdMap; static std::unordered_map<std::type_index, HandleAssetMap> assetIdMap;
static std::unordered_map<std::type_index, std::function<void(AssetID)>> typedFreeFuncMap; static std::unordered_map<std::type_index, AssetFreeFunc> typedFreeFuncMap;
static std::unordered_map<std::type_index, LibraryClearer> typedFreeAllFuncMap;
// Pointers to temp CPU resources // Pointers to temp CPU resources
static std::vector<AssetID> loadedAssetData; static std::vector<AssetID> loadedAssetData;
// Dirty Flags // Dirty Flags

View File

@ -177,14 +177,19 @@ namespace SHADE
{ {
handlesMap.emplace(TYPE, AssetHandleMap{}); handlesMap.emplace(TYPE, AssetHandleMap{});
assetIdMap.emplace(TYPE, HandleAssetMap{}); assetIdMap.emplace(TYPE, HandleAssetMap{});
typedFreeFuncMap.emplace typedFreeFuncMap[TYPE] = [TYPE](AssetID assetId)
( {
TYPE, static_cast<Handle<ResourceType>>(SHResourceManager::handlesMap[TYPE][assetId]).Free();
[TYPE](AssetID assetId) };
typedFreeAllFuncMap[TYPE] = [TYPE]()
{
auto& handlesMap = SHResourceManager::handlesMap[TYPE];
for (auto handle : handlesMap)
{ {
static_cast<Handle<ResourceType>>(SHResourceManager::handlesMap[TYPE][assetId]).Free(); static_cast<Handle<ResourceType>>(handle.second).Free();
} }
); handlesMap.clear();
};
} }
return std::make_pair(std::ref(handlesMap[TYPE]), std::ref(assetIdMap[TYPE])); return std::make_pair(std::ref(handlesMap[TYPE]), std::ref(assetIdMap[TYPE]));
} }