Rapidly Creating Worlds Crashes Editor

I am creating a UWorld to run my tests in to avoid contamination by ■■■■■ that exist in the editor. I am doing this as follows:

UWorld* World = UWorld::CreateWorld(EWorldType::None, false);

// do some stuff with the world like call SpawnActor

// End of the test:
// Destroy everything I made in the world
GEngine->DestroyWorldContext(World);
World->DestroyWorld(false);

However, doing this in quick succession (i.e. if I have a complex automation test, creating a world for each parameter). After about 30 tests (i.e. creating and destroying 30 worlds), Unreal bails out with the following error:

66524-unrealerror_spawningworlds.png

Perhaps there is a better way to do this. If there are some best practises or some example tests I could be following, that would be useful.

As a work around, putting a small pause at the end of each test (e.g. with ADD_LATENT_AUTOMATION_COMMAND(FWaitLatentCommand(0.01f));) avoids the crash (though massively slows down the test).

A (better than pausing) work around. Create one world for testing whenever you run your first test and keep reusing that one. I wrote a function like this that I could use whenever I wanted the world. Not ideal as have to remember to clear out the world at the end of the test.

FName FSingleCarTestData::TestingWorldName = "World_TEST";

UWorld* FAutomatedTestingFunctions::GetTestingWorld()
{
	for (const FWorldContext& WorldContext : GEngine->GetWorldContexts())
	{
		if (WorldContext.World()->GetFName() == FSingleCarTestData::TestingWorldName)
		{
			return WorldContext.World();
		}
	}

	UWorld* NewWorld = UWorld::CreateWorld(EWorldType::None, true, FSingleCarTestData::TestingWorldName);
	FWorldContext& NewContext = GEngine->CreateNewWorldContext(EWorldType::None);
	NewContext.SetCurrentWorld(NewWorld);

	return NewWorld;
}

Hi ,

The error message you were running into is intentional. Creating, destroying, and recreating UWorlds in short periods of time can eat up a lot of memory, and is not recommended. In this case, your thought of re-using a single UWorld for your tests is probably the best option.

If you have not already done so, please also take a look at the Automation System documentation. This page mentions some specific source code files that have example tests included with, and used by, the Editor.