Possible crash between Weak Pointer IsValid check and calling Pin()

This is a very hard to reproduce bug, and the description that was given to us claimed: “had a blueprint stopped on a breakpoint. then pressed the stop button”. (We are on 4.6, with some changes ported over from 4.7 and 4.8)

Fatal error! 
Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x00000000 
UE4Editor-Engine.dll!FSceneViewport::OnMouseLeave() 
UE4Editor-Slate.dll!SViewport::OnMouseLeave() 
UE4Editor-Slate.dll!FSlateApplication::ProcessMouseMoveEvent() 
UE4Editor-Slate.dll!FSlateApplication::OnMouseMove() 
UE4Editor-Core.dll!FWindowsApplication::ProcessDeferredMessage() 
UE4Editor-Core.dll!FWindowsApplication::DeferMessage() 
UE4Editor-Core.dll!FWindowsApplication::ProcessMessage() 
UE4Editor-Core.dll!FWindowsApplication::AppWndProc() 
USER32.dll!UnknownFunction (0x00007ff96732250d) 
USER32.dll!UnknownFunction (0x00007ff967322367) 
UE4Editor-Core.dll!FWindowsPlatformMisc::PumpMessages() 
UE4Editor-Core.dll!FGenericPlatformMisc::PeriodicallyPumpMessages() 
UE4Editor-Engine.dll!UWPFAssetManager::GetData() 
UE4Editor-Engine.dll!WPFAssetManagerInterface::GetData() 
UE4Editor-WPFRenderer.dll!FileResourceLoader::Run() 
UE4Editor-WPFRenderer.dll!LoadableObject::LoadData() 
[...]

The code of the OnMouseLeave functions shows:

	if (ViewportInterface.IsValid())
	{
		ViewportInterface.Pin()->OnMouseLeave(MouseEvent);
	}

We believe that the ViewportInterface somehow got lost in between the IsValid and Pin call. This same structure happens through the whole file, except on one other location, where the Pin is called before doing the IsValid check (this occurs in the OnPaint function).

Are we correct to assume that all IsValid checks should only be called after the Pin function is called? In which case, the whole file could do with some rewriting…

Hi artofcode,

Are you seeing this happen in just a single Blueprint with a breakpoint on a specific node, or does it happen in multiple Blueprints and with different nodes? I tried adding breakpoints to a few nodes in a new 4.6 project and did not have any problems with stopping PIE while execution is paused by a breakpoint.

One of our designers had it happen ‘randomly’ and we’ve been unable to reproduce it whatsoever. From what I can tell it only happened once.

Considering that the IsValid call and the Pin call are both very close to each other, I can imagine that if there is a to go wrong, the will be very small (but possible nonetheless).

We’ve updated the code in that file for each IsValid - Pin call to Pin first and IsValid afterwards.

OnMouse will look like this:

	TSharedPtr<ISlateViewport> PinnedInterface = ViewportInterface.Pin();
	if (PinnedInterface.IsValid())
	{
		PinnedInterface->OnMouseLeave(MouseEvent);
	}

All other functions have been updated accordingly, just to be on the safe side.

Hi artofcode,

It appears that the suggested code that you provided may indeed be a safer alternative in this case. I have submitted a report about this issue to have the code in this file investigated to see what we can do to reduce the possibility of any further crashes (UE-20580).