IVGCVSW-6317 Cleaning up BackendRegistry following negative tests.

* Deregister backends in BackendHintTest.
* Deregister backend in ThrowBackendUnavailableException
* Clean up dynamic backends when an exception is thrown from
  RuntimeImpl::RuntimeImpl.
* Use CHECK_THROWS_AS_MESSAGE syntax in ClCustomAllocatorTests.

Signed-off-by: Colm Donelan <Colm.Donelan@arm.com>
Change-Id: I0e6e5413dd074b5fcfc9515c85cb8d40a4a0b73c
diff --git a/src/armnn/Runtime.cpp b/src/armnn/Runtime.cpp
index 824a2b0..bbcbb9f 100644
--- a/src/armnn/Runtime.cpp
+++ b/src/armnn/Runtime.cpp
@@ -265,7 +265,6 @@
 {
     const auto start_time = armnn::GetTimeNow();
     ARMNN_LOG(info) << "ArmNN v" << ARMNN_VERSION << "\n";
-
     if ( options.m_ProfilingOptions.m_TimelineEnabled && !options.m_ProfilingOptions.m_EnableProfiling )
     {
         throw RuntimeException(
@@ -282,13 +281,18 @@
         // Store backend contexts for the supported ones
         try {
             auto factoryFun = BackendRegistryInstance().GetFactory(id);
+            ARMNN_ASSERT(factoryFun != nullptr);
             auto backend = factoryFun();
+            ARMNN_ASSERT(backend != nullptr);
             ARMNN_ASSERT(backend.get() != nullptr);
 
             auto customAllocatorMapIterator = options.m_CustomAllocatorMap.find(id);
             if (customAllocatorMapIterator != options.m_CustomAllocatorMap.end() &&
                 customAllocatorMapIterator->second == nullptr)
             {
+                // We need to manually clean up  the dynamic backends before throwing an exception.
+                DynamicBackendUtils::DeregisterDynamicBackends(m_DeviceSpec.GetDynamicBackends());
+                m_DeviceSpec.ClearDynamicBackends();
                 throw armnn::Exception("Allocator associated with id " + id.Get() + " is null");
             }