Skip to content

Commit b735b38

Browse files
committed
Fixed worker task accesing garbage memory
Fix #61
1 parent 53db555 commit b735b38

2 files changed

Lines changed: 21 additions & 9 deletions

File tree

Source/SaveExtension/Private/Serialization/SlotDataTask_Saver.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,12 @@ void USlotDataTask_Saver::SerializeWorld()
172172
const UWorld* World = GetWorld();
173173
SELog(Preset, "World '" + World->GetName() + "'", FColor::Green, false, 1);
174174

175-
BakeAllFilters();
176-
177175
const TArray<ULevelStreaming*>& Levels = World->GetStreamingLevels();
176+
PrepareAllLevels(Levels);
178177

179178
// Threads available + 1 (Synchronous Thread)
180179
const int32 NumberOfThreads = FMath::Max(1, FPlatformMisc::NumberOfWorkerThreadsToSpawn() + 1);
181180
const int32 TasksPerLevel = FMath::Max(1, FMath::RoundToInt(float(NumberOfThreads) / (Levels.Num() + 1)));
182-
183181
Tasks.Reserve(NumberOfThreads);
184182

185183
SerializeLevelSync(World->GetCurrentLevel(), TasksPerLevel);
@@ -194,6 +192,20 @@ void USlotDataTask_Saver::SerializeWorld()
194192
RunScheduledTasks();
195193
}
196194

195+
void USlotDataTask_Saver::PrepareAllLevels(const TArray<ULevelStreaming*>& Levels)
196+
{
197+
BakeAllFilters();
198+
199+
// Create the sub-level records if non existent
200+
for (const ULevelStreaming* Level : Levels)
201+
{
202+
if (Level->IsLevelLoaded())
203+
{
204+
SlotData->SubLevels.AddUnique({ *Level });
205+
}
206+
}
207+
}
208+
197209
void USlotDataTask_Saver::SerializeLevelSync(const ULevel* Level, int32 AssignedTasks, const ULevelStreaming* StreamingLevel)
198210
{
199211
TRACE_CPUPROFILER_EVENT_SCOPE(USlotDataTask_Saver::SerializeLevelSync);
@@ -207,14 +219,11 @@ void USlotDataTask_Saver::SerializeLevelSync(const ULevel* Level, int32 Assigned
207219
const FName LevelName = StreamingLevel ? StreamingLevel->GetWorldAssetPackageFName() : FPersistentLevelRecord::PersistentName;
208220
SELog(Preset, "Level '" + LevelName.ToString() + "'", FColor::Green, false, 1);
209221

210-
211222
// Find level record. By default, main level
212223
FLevelRecord* LevelRecord = &SlotData->MainLevel;
213224
if (StreamingLevel)
214225
{
215-
// Find or create the sub-level
216-
const int32 Index = SlotData->SubLevels.AddUnique({ *StreamingLevel });
217-
LevelRecord = &SlotData->SubLevels[Index];
226+
LevelRecord = FindLevelRecord(StreamingLevel);
218227
}
219228
check(LevelRecord);
220229

Source/SaveExtension/Public/Serialization/SlotDataTask_Saver.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,13 @@ class USlotDataTask_Saver : public USlotDataTask
8484
protected:
8585

8686
/** BEGIN Serialization */
87-
void SerializeLevelSync(const ULevel* Level, int32 AssignedThreads, const ULevelStreaming* StreamingLevel = nullptr);
88-
8987
/** Serializes all world actors. */
9088
void SerializeWorld();
89+
90+
void PrepareAllLevels(const TArray<ULevelStreaming*>& Levels);
91+
92+
void SerializeLevelSync(const ULevel* Level, int32 AssignedThreads, const ULevelStreaming* StreamingLevel = nullptr);
93+
9194
/** END Serialization */
9295

9396
void RunScheduledTasks();

0 commit comments

Comments
 (0)