DrawDebug crashes Engine from EvaluateBoneTransforms in an AnimNode

Hello,

I am trying to draw some debug lines from my custom AnimNode but the engine crashes after a few draws.

void FAnimNode_BShoulders::EvaluateBoneTransforms(
	USkeletalMeshComponent	*SkelComp,
	FCSPose<FCompactPose>	&MeshBases,
	TArray<FBoneTransform>	&OutBoneTransforms)
{

// Some more code here 


DrawDebugDirectionalArrow(
				SkelComp->GetWorld(),
				ClavicleTransform.GetLocation(),
				HandEffectorLoc,
				5.0f,
				FColor::Yellow
			);

			DrawDebugDirectionalArrow(
				SkelComp->GetWorld(),
				ClavicleTransform.GetLocation(),
				HandLoc,
				5.0f,
				FColor::Cyan
			);

}

I usually get a crash in SparseArray.h

	FSparseArrayAllocationInfo AllocateIndex(int32 Index)
	{
		check(Index >= 0);
		check(Index < GetMaxIndex());
		check(!AllocationFlags[Index]);

at check AllocationFlags.

What is the correct way of performing debug draw. I was looking at AnimNode_Fabrik.cpp and there do something which looks quite similar to what I am doing, so I am not sure what am I doing wrong.

I have also tried various combinations of switches for persistence of lines and their lifetimes, but no luck …

Best,
darkZ

I have noticed that using an ASync task on a game thread I don’t get a crash … I am still curious as to why does Fabrik node doesn’t need to do this.

Old issue but I just had the same issue in 4.20.3 and found a solution:

Override the following function and put the debug drawing code there:

virtual void GatherDebugData(FNodeDebugData& DebugData) override;

And when the game is running, type into console

showdebug animation

which will make the engine call that GatherDebugData function on active animation nodes.

Quick Explanation:
I don’t know how the code was in 4.12 but in 4.20.3 several main functions in AnimNode are named with _AnyThread postfix which implies they can be called from any thread. And since the debug draw crashes after awhile, sometimes sooner sometimes later, it feels like a threading issue.

Meanwhile the documentation (FAnimNode_Base::GatherDebugData | Unreal Engine Documentation) says that GatherDebugData is called from Game Thread, so it’s safe to draw debug etc. Another bonus is that the function provides you an UWorld to use with debug drawing via:

DebugData.AnimInstance->GetWorld()