# Automation driver sequence played in an automation test freezes the engine when garbage collecting

**URL:** <https://forums.unrealengine.com/t/automation-driver-sequence-played-in-an-automation-test-freezes-the-engine-when-garbage-collecting/787858>\
**Category:** Programming & Scripting\
**Tags:** automated-testing, Automation-Driver, unreal-engine, question, CPP\
**Created:** [March 10, 2023, 12:44pm UTC](https://forums.unrealengine.com/t/automation-driver-sequence-played-in-an-automation-test-freezes-the-engine-when-garbage-collecting/787858 "2023-03-10T12:44:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![christoffersch](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@christoffersch](https://forums.unrealengine.com/u/christoffersch)\
**Post date:** [March 10, 2023, 12:44pm UTC](https://forums.unrealengine.com/t/automation-driver-sequence-played-in-an-automation-test-freezes-the-engine-when-garbage-collecting/787858/1 "2023-03-10T12:44:33Z")

</div>

I’m struggling with setting up an automation test using the automation framework with latent commands that uses the automation driver to simulate input.

The test is supposed to go like this

1. Spawn in enemy actor X
2. Shoot with weapon and check if damage has been dealt
3. Repeat with next enemy actor until all are checked

This works fine for around 30 seconds, but then the engine will completely freeze with no error or nothing, I have to quit it manually. I know it probably has something do with garbage collection because of the timing, and because Unreal Insights consistently shows that it freezes almost right after GC’ing every time.  
It also must have something to do with input simulation using the automation driver, because if I remove that section, no freeze happens.

At first I thought it was my custom wrapper around the automation driver that would get GC’ed, but then I tried doing it the ‘standard way’ (without my wrapper object) and it keeps happening - so not sure what’s going on.

This should be all the relevant code:

```auto
ADD_LATENT_AUTOMATION_COMMAND(FEngineWaitLatentCommand(1.5f));
ADD_LATENT_AUTOMATION_COMMAND(FLatentGenericFunction( [=] () mutable
{
	/* SOME CODE HERE FOR SPAWNING ENEMY AND AIMING AT IT */
        
	// Simulate shooting with automation driver (this will freeze the engine - leaving it out solves it, so it's gotta be because of it)
	AsyncTask(ENamedThreads::GameThread, [=] () mutable
	{
		FAutomationDriverPtr Driver;
		FDriverSequencePtr Sequence;
		
		IAutomationDriverModule::Get().Enable();
		Driver = IAutomationDriverModule::Get().CreateDriver();
		Sequence = Driver->CreateSequence();

		Sequence->Actions().Press(EKeys::LeftMouseButton);
		Sequence->Actions().Release(EKeys::LeftMouseButton);
		Sequence->Actions().Press(EKeys::LeftMouseButton);
		Sequence->Actions().Release(EKeys::LeftMouseButton);

		AsyncTask(ENamedThreads::AnyHiPriThreadNormalTask, [=]
		{
			Sequence->Perform();

			AsyncTask(ENamedThreads::GameThread, [=]
			{
				IAutomationDriverModule::Get().Disable(); 
			});
		});
	});
	
	FTimerDelegate TimerCallback;
	TimerCallback.BindLambda( [=]
	{
		// Destroy enemy when we're done with it
		Enemy->Destroy();
	});

	FTimerHandle Handle;
	World->GetTimerManager().SetTimer(Handle, TimerCallback, 2.35f, false);
}));
ADD_LATENT_AUTOMATION_COMMAND(FEngineWaitLatentCommand(2.35f));

```

That code block gets repeated X times in `RunTest()` to set up latent commands for each enemy.

Here’s `FLatentGenericFunction`, in case that should matter:

```auto
class FLatentGenericFunction : public IAutomationLatentCommand
{

public:
	FLatentGenericFunction(TFunction<void()>&& InFunction)
		: Function(MoveTemp(InFunction)) { }
	
	virtual bool Update() override
	{
		Function();
		return true;
	}
	
protected:
	
	TFunction<void()> Function;
};

```

Does anyone have an idea of what could be going on?  
Not sure if I am misunderstanding the way latent commands work and all the lambda stuff, or if there might be a bug in the automation driver.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![sgoncharriot](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/sgoncharriot/32/88714_2.png) [@sgoncharriot](https://forums.unrealengine.com/u/sgoncharriot)\
**Post date:** [March 16, 2023, 11:33pm UTC](https://forums.unrealengine.com/t/automation-driver-sequence-played-in-an-automation-test-freezes-the-engine-when-garbage-collecting/787858/2 "2023-03-16T23:33:34Z")

</div>

This is not an answer, but maybe more context for someone …

Mine locks up in:

```auto
FAsyncAutomationDriver::~FAsyncAutomationDriver()
{
	Application->SetFakeModifierKeys(FModifierKeysState());
}

```

because Application was already GCed.

… I should also add that it only happens if I use `Driver->CreateSequence();` … if I don’t use input sequence everything gets cleaned up fine.

---

<div class="post-metadata">

**Author:** ![christoffersch](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@christoffersch](https://forums.unrealengine.com/u/christoffersch)\
**Post date:** [March 21, 2023, 10:37am UTC](https://forums.unrealengine.com/t/automation-driver-sequence-played-in-an-automation-test-freezes-the-engine-when-garbage-collecting/787858/3 "2023-03-21T10:37:51Z")

</div>

Thanks for taking a look. Interesting find. Will keep updated if I find a solution
