Improved logic for selecting a platform to better deal with exceptions in context initialization

This involved some rearrangement of memory allocation to avoid memory leaks