What does FMemoryWriter and FMemoryReader do?

hi guys ! i’m new to cpp and now working on Archive.h class and i want to know what exactly does FMemoryWriter and FMemoryReader do !? and how these works !?

Thanks Alot

1 Like

FMemoryReader and FMemoryWriter can wrap a byte array (TArray<uint8>) to give it an FArchive compatible interface. We use it to serialize and de-serialize UObjects, UStructs and basic core types from/to byte buffers. The usage is very simple: given an existing byte array you create a memory reader or writer object, passing the array into their constructors:

TArray<uint8> Buffer;
int64 Value = 42;

FMemoryReader Reader(Buffer);
Reader << Value; // De-serialize an int64 from the buffer into a variable

FMemoryWriter Writer(Buffer);
Writer << Value; // Serialize an int64 variable into the buffer.

Both the reader and the writer constructors have an optional flag to determine whether the buffer is intended to be persistent, i.e. written to disk, for example. If this flag is true, they will automatically make sure that the correct byte ordering is used on the corresponding platform. This allows you to de-/serialize the same multi-byte value on platforms with different Endianness.

Thank You So MUCH !
but i still have one more problem :
i’m using something like this for serialize my actors :

struct FMySaveGameArchive : public FObjectAndNameAsStringProxyArchive
 {
     FMySaveGameArchive(FArchive& InInnerArchive, bool bInLoadIfFindFails)
         : FObjectAndNameAsStringProxyArchive(InInnerArchive, bInLoadIfFindFails)
     {
         ArIsSaveGame = true;
     }
 };
 
 USTRUCT()
 struct FActorRecord
 {
     GENERATED_USTRUCT_BODY()
 
     UPROPERTY(SaveGame)
     UClass*        Class;
 
     UPROPERTY(SaveGame)
     FTransform    Transform;
 
     UPROPERTY(SaveGame)
     FName        Name;
 
     UPROPERTY(SaveGame)
     TArray<uint8> ActorData;
 };
 
 void SaveActor(FActorRecord& ActorRecord, AActor* Actor)
 {
     //Save actor
     ActorRecord.Class = Actor->GetClass();
     ActorRecord.Transform = Actor->GetTransform();
     ActorRecord.Name = Actor->GetFName();
 
     FMemoryWriter MemoryWriter(ActorRecord.ActorData, true);
 

     FMySaveGameArchive Ar(MemoryWriter, false);
 

     Actor->Serialize(Ar);
 

 }

everything work correctly but about 2 min after saving actor my editor stop working and crashes

plz help me

What’s the callstack?

in this example its using an actor and Actor Record but in the my codes i’m using an array of Actors and an array of records like this:

void AMyTestActor::SaveTheGame(TArray<FActorRecord>& Records, TArray<AActor*> Actors)
{
	int32 leng = Actors.Num();
	
	for (int32 i = 0; i < leng; i++)
	{
		Records.Emplace();
		FMemoryWriter MemoryWriter(Records[i].ActorData, false);

		
		// use a wrapper archive that converts FNames and UObject*'s to strings that can be read back in
		FMySaveGameArchive Ar(MemoryWriter, false);
		
		// serialize the object
		Actors[i]->Serialize(Ar);
		
	}

}

this function implement in blueprint and also gets the array of Actors in blueprint

I know this post is quite old but it appears to be a common garbage collection error.
Your pointers aren’t all initialized in your struct it seems. Create an initialize method in FActorRecord and set

Class = AActor::StaticClass()

If you never assign a value to a pointer, when garbage collection hits that value, things will EXPLODE. So, always initialize pointers in structs. I’ve spent many an hour when I started C++ trying to find what was causing crashes, and I sometimes still forget to assign a value to them. Nullptr should do the trick also, if you don’t have a default value for the pointer variable.

Cheers,

Jacob