AttachRootComponentToActor crashes multiplayer game

In Singleplayer mode it works fine but when I execute:
PickupItem->AttachRootComponentToActor(this, “Socket_Root”, EAttachLocation::SnapToTarget);
while in multiplayer, the game crashes and triggers a breakpoint at the following line in SceneComponent.cpp:

// Make sure parent points to us if we're registered
		checkf(!bRegistered || AttachParent->AttachChildren.Contains(this), TEXT("Attempt to detach SceneComponent '%s' owned by '%s' from AttachParent '%s' while not attached."), *GetName(), (GetOwner() ? *GetOwner()->GetName() : TEXT("Unowned")), *AttachParent->GetName());

Maybe I’m too tired but I can’t find the problem.

EDIT: The Problem still persists in 4.8 and the following error pops up:

Script Stack:
Actor.OnRep_AttachmentReplication

Assertion failed: !bRegistered || AttachParent->AttachChildren.Contains(this) [File:D:\BuildFarm\buildmachine_++depot+UE4-Releases+4.8\Engine\Source\Runtime\Engine\Private\Components\SceneComponent.cpp] [Line: 1335] 
Attempt to detach SceneComponent 'Environment Mesh' owned by 'Shabby_Pants_C_0' from AttachParent 'CharacterMesh0' while not attached.

It happens when I spawn a player and instantly attach a bunch of gear-items to him as soon as he logs onto the server. When the game is already in progress, the very same command doesn’t cause any issues.

I narrowed it down a little bit by writing a function in my pickup-class. The “AttachRootComponentTo”-Function inside this function is causing the crash.

void ASomPickup::AttachToCompSocket(USceneComponent* Comp, FName Socket)
{
	//Disable Physics
	this->EnvironmentMeshComponent->SetSimulatePhysics(false);

	//Disable Collision
	this->EnvironmentMeshComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision);

	//Attach Pickup to Socket
	this->AttachRootComponentTo(Comp, Socket, EAttachLocation::SnapToTarget);
}

As I said. In singleplayer it works fine. In multiplayer the game crashes. Does anybody have a suggestion?

Hey -

I am investigating your crash and would like to ask for some additional information. In multiplayer does the crash occur as soon as you begin playing or when when the lines of code you posted are called (when you attempt the attach)? Could you post the log files from the crash as well as the code for the class where you are trying to call AttachRootComponentTo()?

Cheers

Hey . Thanks for answering.

The crash doesn’t happen until I call the function (from my Character class). But the function gets called immediately after spawning the character.

Hey -

It’s most likely that comp is NULL when the call happens which is what is causing the breakpoint to trigger. Adding in a check() to ensure that it is not null (and handling it if it is) should sold the issue.

Cheers

Hey ,

thanks again for your answer and your efforts regarding this matter.

I wrote a check at the beginning of the function:

if (Comp == NULL)
	{
		print("Comp == NULL!");
		return;
	}

…but it is never being called. After that I wrote the following:

if (Comp != NULL)
		print(Comp->GetName());
	else
		print("Comp is Null");

But it is always printing “CharacterMesh0”… so the component is never NULL :frowning:

Regards

Hi ,

I had some questions for you regarding this issue.

  • Just to confirm, this is only occurring when you are testing your game in multiplayer, and only at the very beginning of the game, correct?
  • Is this happening in PIE, or in a packaged version of your game?
  • Could you provide the full crash report that is shown when the crash happens? I tried to find a submitted report matching the error you provided but was unable to do so.
  • Are you able to reproduce the crash in a new test project?
  • Would it be possible to see the code for your pickup and character classes?

Hey . Thank you very much for your answer.

  • Yes, it is only happening in multiplayer and only at the beginning of the game.
  • The error occurs in PIE as well as in the packaged version.
  • Here is the crash report:
    link text

It would be a little complicated to reproduce this in a new project, because there are a lot of classes working together:

The Networking-Class receives a string from a SQL-database and sends it to the Character-class (“LoadAppearance_Complete”) to cut it into chunks and set the equipment of the character. Then the GameMode iterates through all the players, synching each one to the other with a function inside the Character-class.

The Character-class has a number of functions which are calling the “Attach”-Function inside the Pickup-Class so it is hard to say which function triggers the error.

But nevertheless, here is the entire code of my project. Consider that some events are triggering blueprint “code”. I hope you can help me.

[link text][2]

UPDATE: It seems that the cause isn’t any of my functions but the native Attachment Replication of UE4.

The following scenario appears to trigger the crash: There is already a player on the server and has attached some items to his body. The crash happens while another player joins the server.

This is the call stack:

Hi ,

I have a project setup that I think is close to what you have when you see this crash occur. I have two sockets on a player character, and on begin play I attach an instance of an actor class to each socket using the AttachToCompSocket code you provided above. Unfortunately I did not see a crash when I tested this in a few different multiplayer tests, so I am still missing something. Would it be possible to see the entire code for your character class and the class you are attaching to the character?

Hey ,

I have sent you a PM with the code on the forums.

Thanks. I got it and built a project with your code. I’ll take a closer look at the code next week, and see if I can get the issue to happen for me.

Thanks a bunch, ! If you like, I can send you the whole project, too… so you don’t have to fiddle around with the “content-includes” in the constructors.

Did you by figure out what the issue is? I am having the exact same problem. Thanks!

Not yet. I’ve just sent my whole project to with a PM on the forums. Hopefully he is still reading this.

Hi everyone,

I apologize for the delay on this issue. I have been a bit swamped for the past couple weeks, and still have around 20 issues that I am looking into. This particular issue is one I will try to prioritize as best as I can.

I started looking into this issue in depth on Friday, and then some more today. I can see the crash happening, but have not yet determined why it is happening. I did have a couple questions for you:

  • I noticed that the repro steps involve starting with a male character, putting on the supplied clothing items, then joining as a female character that is already wearing the items. No crash occurred if I did not put on any of the clothing items before having the second character join. If you reverse the order that the characters join, does the crash still occur?
  • What happens if you start with a clothed female character and then have another clothed female character join?

Hey ,

thanks again for answering.

It is unlikely that the gender has something to do with the problem. The spawning character is always the same at first. Then it loads the appearance from the database (including gender, stats, clothing) and attaches the items to the pawn. But I tried two male characters with the same result.

I am trying to get it to work with blueprints now.

The reason I asked about changing the order in which characters join is because I am curious to find out if the character that is joining from the client (which is already clothed) is somehow trying to tell the server that it is wearing the same instances of clothing that the server’s character is wearing.

The callstack that I received did not contain any code from your project, so I am leaning towards this being something we are not properly handling in the source code, but I still need to narrow down further what is actually triggering the crash.

I tried it with two women… same result. I’m not sure if it could be the same pickup-instance. The character who is already on the server uses the pickup-actors which are already on the server. The character who joins later obtains a string from the database, which describes the path/folder, where the pickup-actor lies and spawns a new actor based on that string.

If one of this actors is a clothing items, the static mesh is set to invisible and the actor is attached to the characters root. This actor also has a skeletal mesh reference, which is the rigged clothing item. The character’s corresponding body part is being swapped with this skeletal mesh.

Maybe it is because I am attaching the items with the help of multicast, server and client functions and it interferes with the native attachment replication. Cause the engine seems to somehow try to detach the actors automatically from their parent-actors before attaching it to the next one.