Unreal Editor 4.7.6 Crash After Spawning Actor

I tried spawning a class with the following code (MyCharacter extends Character) :

//In MyCharacter.h
...
TSharedPtr<class ATurret> SelectedTurret;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay)
TArray<TSubclassOf<class ATurret>> Turrets;
...

//In MyCharacter.cpp

...
//Note I bound this func to an Axis where
//1 key is 1.0
//2 key is 2.0
//etc..
void AMyCharacter::Select(float toSelect)
{
	if (toSelect <= 0 || toSelect > Turrets.Max()) return;

	UE_LOG(LogTemp, Warning, TEXT("Selected"));

	auto turret = Turrets[(int)toSelect-1];

	if (turret != nullptr)
	{
		UE_LOG(LogTemp, Warning, TEXT("Turret class not null"));

		if (SelectedTurret.Get() != nullptr)
		{
			UE_LOG(LogTemp, Warning, TEXT("Selected turret not null, deleting"));

			GetWorld()->DestroyActor(SelectedTurret.Get());
			SelectedTurret.Reset();
		}

		UE_LOG(LogTemp, Warning, TEXT("Spawning turret"));
		SelectedTurret = MakeShareable(GetWorld()->SpawnActor<ATurret>(turret));
	}
}
...

A few notes

  1. The above function is bound to an Axis instead of Actions
  2. The turrets array is filled in using the editor
  3. The crash does not happen if the project is run via building and debugging through VS, it only happens when using the Epic Launcher to start the project

The editor then crashes when I

  1. Hit play and press ‘1’ to run the above code
  2. Press ‘Escape’ to stop playing

Crash log

MachineId:D1D5678E474DDCAFCF43ABBC29755774
EpicAccountId:d6198ce327c540459ffa7fdc7c8e7551

Unknown exception - code 00000001 (first/second chance not available)

Fatal error: [File:D:\BuildFarm\buildmachine_++depot+UE4-Releases+4.7\Engine\Source\Runtime\CoreUObject\Private\UObject\Obj.cpp] [Line: 570] 
Turret None.None:None.Turret_8 failed to route BeginDestroy

KERNELBASE + 42141 bytes
UE4Editor_Core!FOutputDeviceWindowsError::Serialize() + 292 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\core\private\windows\windowsplatformoutputdevices.cpp:95]
UE4Editor_Core!FMsg::Logf__VA() + 463 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\core\private\misc\outputdevice.cpp:531]
UE4Editor_CoreUObject!UObject::ConditionalBeginDestroy() + 649 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\coreuobject\private\uobject\obj.cpp:570]
UE4Editor_CoreUObject!CollectGarbage() + 1886 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\coreuobject\private\uobject\garbagecollection.cpp:1152]
UE4Editor_UnrealEd!UEditorEngine::EndPlayMap() + 3487 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\editor\unrealed\private\playlevel.cpp:236]
UE4Editor_UnrealEd!UEditorEngine::Tick() + 804 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\editor\unrealed\private\editor.cpp:966]
UE4Editor_UnrealEd!UUnrealEdEngine::Tick() + 22 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\editor\unrealed\private\unrealedengine.cpp:347]
UE4Editor!FEngineLoop::Tick() + 4179 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\launch\private\launchengineloop.cpp:2257]
UE4Editor!GuardedMain() + 1404 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\launch\private\launch.cpp:142]
UE4Editor!GuardedMainWrapper() + 26 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\launch\private\windows\launchwindows.cpp:126]
UE4Editor!WinMain() + 249 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\launch\private\windows\launchwindows.cpp:202]
UE4Editor!__tmainCRTStartup() + 329 bytes [f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c:618]

I am unsure if I am not using TSharedPtr correctly or if it is an engine bug.
I noticed that there is no problem if the following line is commented out:

//in .cpp
SelectedTurret = MakeShareable(GetWorld()->SpawnActor<ATurret>(turret));

Any help would be appreciated, thank you.

EDIT:
I quickly tried changing the binding to an Action binding instead of an Axis (bound several functions and called select with 1,2,3 , etc…). This did not fix anything.

After some tinkering I seem to have fixed it.

The problem was UE was trying to destroy the object spawned in

 SelectedTurret = MakeShareable(GetWorld()->SpawnActor<ATurret>(turret));

after destroying the character that was holding it, causing the pointer to go out of scope and be garbage collected.

The solution was to override BeginDestroy on the character and tell the engine to destroy the pointer there.

//in .h
virtual void BeginDestroy() override;

//in .cpp
void ABangBangCharacter::BeginDestroy()
{
	Super::BeginDestroy();

	if (SelectedTurret.IsValid())
	{
		SelectedTurret->Destroy();
		SelectedTurret.Reset();
	}
}