Download

Dynamic replicated components causing crash since 4.7

I’ve posted this in the AnswerHub but I’d also like to turn to you in case someone may know if I’m doing something incorrectly. I have an actor that spawns replicated static mesh components in the BeginPlay function. In a multiplayer session, these components appear correctly on all clients. Changing the mesh during the game is also reflected on all clients. However, once the parent actor is removed, remote clients crash when cleaning up the subobjects. The crash didn’t happen in 4.6, but does in 4.7. It crashes in DataChannel::CleanUp in DataChannel.cpp line 1443:



// Destroy any sub-objects we created
	for ( int32 i = 0; i < CreateSubObjects.Num(); i++ )
	{
		if ( CreateSubObjects*.IsValid() )
		{
			Actor->OnSubobjectDestroyFromReplication( CreateSubObjects*.Get() );

			// Crashes on this line:
			CreateSubObjects*->MarkPendingKill();
		}
	}


I found another topic about it here but the conclusion there is don’t test using PIE, which I’m not satisfied with. I’ve reproduced the issue in a clean project that you can download here if you’re interested. Here is the piece of code that is enough to cause the crash:



ARepActor::ARepActor()
{
	bReplicates = true;
}

void ARepActor::BeginPlay()
{
	Super::BeginPlay();
	if (HasAuthority() && DisplayMesh)
	{
		// Dynamically spawn some replicated subobjects
		for (int i = 0; i < 3; ++i)
		{
			UStaticMeshComponent * RepMesh = NewObject<UStaticMeshComponent>(this);
			RepMesh->SetIsReplicated(true);
			RepMesh->SetRelativeLocation(FVector(0, 0, (i + 1) * 200));
			RepMesh->AttachTo(GetRootComponent());
			RepMesh->RegisterComponent();
			RepMesh->SetStaticMesh(DisplayMesh);
		}

		// Start a timer to destroy self after 2 seconds
		FTimerHandle DestroyHandle;
		GetWorldTimerManager().SetTimer(DestroyHandle, this, &ARepActor::DestroyMe, 2.f, false);
	}
}

void ARepActor::DestroyMe()
{
	Destroy();
}


Does anyone see anything useful? Maybe I’m using an incorrect method of spawning replicated components? Is anyone else having issues with using replicated components that are spawned after play begins? Thanks guys. :slight_smile: