Adding Instances for multiple Instanced Static Meshes cause a crash

So, I have a matrix/2 dimensional array of Instanced Static Meshes that I use to create a prism of items and then I use a resize rectangle to adjust the amount of items to be placed in the prism. So lets say if I have 5 items in depth and 5 items in height, moving the slider sideways create 5x5 of these items by simply adding an instance to the appropriate local transform. However on high amount of these, the editor crashes with the error:

Access violation - code c0000005 (first/second chance not available)

""

PhysX3PROFILE_x64
PhysX3PROFILE_x64
PhysX3PROFILE_x64
PhysX3PROFILE_x64
UE4Editor_Engine!TGraphTask<FPhysXTask>::ExecuteTask() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\public\async\taskgraphinterfaces.h:779]
UE4Editor_Core!FTaskThread::ProcessTasks() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\private\async\taskgraph.cpp:539]
UE4Editor_Core!FTaskThread::ProcessTasksUntilQuit() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\private\async\taskgraph.cpp:340]
UE4Editor_Core!FTaskThread::Run() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\private\async\taskgraph.cpp:690]
UE4Editor_Core!FRunnableThreadWin::Run() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\private\windows\windowsrunnablethread.cpp:74]

The crash is rather random, sometimes it requires me to move the slider rapidly, however at times, it simply crashes on the first move. This crash only occurs when I add instances, not when removing them and decreasing the said depth and height (Thus the amount of Instanced Static Meshes) decrease the possibility of the crash, but still remains even when there are only 1 Instanced Static Mesh.

As this occurs when I am adding them, this isnt likely to be an issue of giving it a wrong reference, anything null etc.

After a bit of experimenting on it, I realized that it is due to pending removal of the instances. Simply when I remove an instance and quickly try to add one, this increases the possibility, simply leading me to the conclusion that the pending removals interfere with the next instance to be added.

Version: 4.10.1-2791327+++depot+UE4-Releases+4.10

Another input I can give on this is the crash only occurs when I add instances after removing some. Simply if I keep adding instances no matter how big my matrix is causes no problems. When I remove them, there again are no problems, but when I try to add instances again, that is where the problem occurs. If the randomness allows me and that way if I can add to the extent that I had previously added the maximum, then I do not have the crash. For better understanding:

Create 10, no crash

Delete until 1, no crash

Create until 10, possibility to crash

Create more after 10, no crash

I might try to debug the entire engine to bring more data on the board, but it might take some time.

In cases like this it’s generally a good idea to post your source code. It might be a typo or logic error.

Hi ,

  • Does this occur in a clean, blank project with no additional content or is it limited to one project?
  • What are the exact steps you are taking to reproduce this on your end?
  • Can you post the full callstack with machine ID?
  • Can you post your crash logs here so I can take a look, they can be found at \Unreal Projects\PROJECTNAME\saved\logs.

I would like to thank you for making me doubt my code, it was indeed the problem so this no longer is a bug report. It is related to my code and I could narrow down the problem to this extent:

void AProductGroup::CullXRow(FProductRowX &RowToDestroy)
{
	// Determine if should add or remove products // This one removes
	if (RowToDestroy.Products->GetInstanceCount() > ProductPlacementX)
	{
		for (int i = RowToDestroy.Products->GetInstanceCount() - 1; i >= ProductPlacementX ; i--)
		{
			UE_LOG(LogTemp, Warning, TEXT("Destroyed Product: %d"), i);
			RowToDestroy.Products->RemoveInstance(i);
		}
	}
	// This one adds
	else if (RowToDestroy.Products->GetInstanceCount() < ProductPlacementX)
	{
		FVector meshOrigin;
		FVector meshExtent;
		float sphereRadius;

		UKismetSystemLibrary::GetComponentBounds(ArrowMesh, meshOrigin, meshExtent, sphereRadius);

		for (int i = RowToDestroy.Products->GetInstanceCount(); i < ProductPlacementX; i++)
		{
			int asdf = RowToDestroy.Products->AddInstance(FTransform(FVector(meshExtent.X + (((meshExtent * 2).X + GapBetweenProducts)*i), 0, 0)));
			UE_LOG(LogTemp, Warning, TEXT("Created Product: %d"), asdf);
		}
	}
}

This code adds or removes a new instance depending on the variable ProductPlacementX. If lets say, there should be 4 items and it has 3, it adds a new instance. And this method is called here for every other dimensions I have: To be continued

// Event to be called when X amount changes. Automatically adds or removes an extra products on all rows of all planes
void AProductGroup::XAmountChanged()
{
// If there are any items in depth
if (ProductRows.Num() > 0)
{
// If the first depth layer has any rows
if (ProductRows[0].Products.Num() > 0)
{
// Iterate through all layers
for (int i = ProductRows.Num() - 1; i >= 0; i–)
{
//If the Y row contains an item
if (ProductRows[i].Products.Num() > 0)
{
// Iterate through all X rows
for (int j = ProductRows[i].Products.Num() - 1; j >= 0; j–)
{
CullXRow(ProductRows[i].Products[j]);
}
}
}
}
}
}

And with the replication, it exactly occurs on this case:
If an instance is added, deleted, added again, deleted again, trying to create it once again crashes. And this only occurs when there are multiple InstancedStaticMeshes. Having a matrix with dimensions 1x1 and repeating the process does not crash.

Hello ,

Made a reply to TTaM’s post that it might be related to my end, there are the functions that create the issue, however here are what you ask:

MachineId:209466C2438367B1D0CEBD842511E3EE
EpicAccountId:d9d172a506bf4cce9cf8c7bfa20431cd

Access violation - code c0000005 (first/second chance not available)

""

PhysX3PROFILE_x64
PhysX3PROFILE_x64
PhysX3PROFILE_x64
PhysX3PROFILE_x64
UE4Editor_Engine!TGraphTask<FPhysXTask>::ExecuteTask() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\public\async\taskgraphinterfaces.h:779]
UE4Editor_Core!FTaskThread::ProcessTasks() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\private\async\taskgraph.cpp:539]
UE4Editor_Core!FTaskThread::ProcessTasksUntilQuit() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\private\async\taskgraph.cpp:340]
UE4Editor_Core!FTaskThread::Run() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\private\async\taskgraph.cpp:690]
UE4Editor_Core!FRunnableThreadWin::Run() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\private\windows\windowsrunnablethread.cpp:74]

Reproduction

On the functions I have posted as a reply to TTaM, and calling these functions with the function below should reproduce it as this crash ALWAYS occurs in the sequence of:
Adding X amount of items,
Removing Y amount of items (Y<X)
Adding any amount of items
Removing them back
Trying to add

In simple words, if an instance is created, removed, added, removed and trying to add again crashes. Only occurs when I use a multi dimension one. I tried to replicate it with two Instances that I created (Separate ones) and could not replicate it which makes me think its my fault.

ProductPlacementX = 5;
XAmountChanged();
ProductPlacementX = 1;
XAmountChanged();
ProductPlacementX =5;
XAmountChanged(); //Where the crash should occur.

To be continued

The issue occurs on a preexisting project and there are a lot of codes and blueprint setups to place into a new project, thus I avoided it.link text

Most of the logs are my debugging logs but the task graph finishes immediately without much data to look at.

I should also mention that these matrixes I am using are structs so that they can be used in blueprints.

Here are the definitions:

// A line of products

USTRUCT()
struct RAF_API FProductRowX
{
	GENERATED_USTRUCT_BODY()
public:
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Placement)
	UInstancedStaticMeshComponent* Products;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Placement)
	USceneComponent* Anchor;
};

// A plane of products

USTRUCT()
struct RAF_API FProductRowY
{
	GENERATED_USTRUCT_BODY()
public:
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Placement)
	TArray<FProductRowX> Products;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Placement)
	USceneComponent* Anchor;
};

And I keep an array of FProductRowY which represents the prism of items.

Edit: On my debugging logs, I read the inndexes completely fine so I could not locate ann index offset to occur to make me get to removal of an unexisting instance

Hey ,

I took a look at your code and I think the error you are running into is potentially an infinite loop error. In this code:

for (int j = ProductRows[i].Products.Num() - 1; j >= 0; j--)
                     {
                         CullXRow(ProductRows[i].Products[j]);
                     }
                 }

You are passing in information that when this is run:

void AProductGroup::CullXRow(FProductRowX &RowToDestroy)
{
// Determine if should add or remove products // This one removes
if (RowToDestroy.Products->GetInstanceCount() > ProductPlacementX)
{
for (int i = RowToDestroy.Products->GetInstanceCount() - 1; i >= ProductPlacementX ; i–)
{
UE_LOG(LogTemp, Warning, TEXT(“Destroyed Product: %d”), i);
RowToDestroy.Products->RemoveInstance(i);
}
}
// This one adds
else if (RowToDestroy.Products->GetInstanceCount() < ProductPlacementX)
{
FVector meshOrigin;
FVector meshExtent;
float sphereRadius;

     UKismetSystemLibrary::GetComponentBounds(ArrowMesh, meshOrigin, meshExtent, sphereRadius);

     for (int i = RowToDestroy.Products->GetInstanceCount(); i < ProductPlacementX; i++)
     {
         int asdf = RowToDestroy.Products->AddInstance(FTransform(FVector(meshExtent.X + (((meshExtent * 2).X + GapBetweenProducts)*i), 0, 0)));
         UE_LOG(LogTemp, Warning, TEXT("Created Product: %d"), asdf);
     }
 }

}

It is constantly changing. Because of how it is checking to see if j>=0 and looking for Row information that may be rapidly both adding and subtracting information, it may be running into an infinite loop, which would result in a crash.

I used to use the same code without Instanced static meshes. I simply was using StaticMeshComponents and the code worked without crashes.

However I could not locate where exactly the problem occurs as the i value will initially will have the instance count and on each iteration of the loop, getinstancecount would not affect anything. Can you pinpoint the location for the possible cause for the infinite loop? I mean I am sure about the correctness of the amount of function calls (Maybe except for the time of crash), however since the crash occurs always on the 2nd time I try to recreate an instance, that seems so systematic to me and totally unrelated to randomness.

This is an issue for me as well, I am getting the same error working with instanced static meshes, but only sometimes and I can reproduce it by adding and removing instances very quickly. I am currently developing a grid based level editor, and it is very annoying to get a crash while working on a map. Any updates on potential solutions besides not using instanced static meshes?

My workaround is to destroy the Instance Component and add it back instead of removing the instances created by the component and adding them back. It might not work for your case but it does the trick for me.