I think this fixes bug #378513. The bug occurred (during session restore) when the code tries to cast to a WebFrameEngine, but since the main frame has already been deleted, this fails.
Using this code, the MainWidget removes the main frame from the frame manager before it's deleted. I'm not sure this fully fixes the original problem, but I do not have crashes anymore with this diff. Note: I only tested it on the Applications/17.4 branch, since master required compiling lots of packages, so you may want to test it on master first.