diff --git a/Source/Engine/Content/Storage/ContentStorageManager.cpp b/Source/Engine/Content/Storage/ContentStorageManager.cpp index 61e73a3f2..47bdcd3b2 100644 --- a/Source/Engine/Content/Storage/ContentStorageManager.cpp +++ b/Source/Engine/Content/Storage/ContentStorageManager.cpp @@ -58,8 +58,8 @@ FlaxStorageReference ContentStorageManager::GetStorage(const StringView& path, b Locker.Lock(); // Try fast lookup - FlaxStorage* result; - if (!StorageMap.TryGet(path, result)) + FlaxStorage* storage; + if (!StorageMap.TryGet(path, storage)) { // Detect storage type and create object const bool isPackage = path.EndsWith(StringView(PACKAGE_FILES_EXTENSION)); @@ -67,39 +67,42 @@ FlaxStorageReference ContentStorageManager::GetStorage(const StringView& path, b { auto package = New(path); Packages.Add(package); - result = package; + storage = package; } else { auto file = New(path); Files.Add(file); - result = file; + storage = file; } // Register storage container - StorageMap.Add(path, result); + StorageMap.Add(path, storage); } + // Build reference (before releasing the lock so ContentStorageSystem::Job won't delete it when running from async thread) + FlaxStorageReference result(storage); + Locker.Unlock(); if (loadIt) { // Initialize storage container - result->LockChunks(); - const bool loadFailed = result->Load(); - result->UnlockChunks(); + storage->LockChunks(); + const bool loadFailed = storage->Load(); + storage->UnlockChunks(); if (loadFailed) { LOG(Error, "Failed to load {0}.", path); Locker.Lock(); StorageMap.Remove(path); - if (result->IsPackage()) - Packages.Remove((FlaxPackage*)result); + if (storage->IsPackage()) + Packages.Remove((FlaxPackage*)storage); else - Files.Remove((FlaxFile*)result); + Files.Remove((FlaxFile*)storage); Locker.Unlock(); - Delete(result); - return nullptr; + result = nullptr; + Delete(storage); } } diff --git a/Source/Engine/Content/Storage/FlaxStorageReference.h b/Source/Engine/Content/Storage/FlaxStorageReference.h index 03f4d0c8d..e4a5df079 100644 --- a/Source/Engine/Content/Storage/FlaxStorageReference.h +++ b/Source/Engine/Content/Storage/FlaxStorageReference.h @@ -58,17 +58,17 @@ public: return _storage != nullptr; } - FORCE_INLINE bool operator ==(const FlaxStorageReference& other) const + FORCE_INLINE bool operator==(const FlaxStorageReference& other) const { return _storage == other._storage; } - FORCE_INLINE bool operator !=(const FlaxStorageReference& other) const + FORCE_INLINE bool operator!=(const FlaxStorageReference& other) const { return _storage != other._storage; } - FORCE_INLINE FlaxStorage* operator ->() const + FORCE_INLINE FlaxStorage* operator->() const { return _storage; }