layers: #471 swapchin layer to use find() for map lookups

This fixes a long-standing (day-1) defect in the swapchain layer (perhaps
cloned from another layer at the time?).  Several unordered maps are used to
keep track of things.  The layer was using the [] operator for both adding a
new entry to a map, and for looking up entries that should already be in a map.
This latter usage is where the bug is.  If a handle is passed in that hasn't
been seen before, the use of the [] operator will add a new entry to the
map--one that hasn't been initialized.  The new EnableWsiBeforeUse test caused
a crash in the swapchain layer when it used VK_NULL_HANDLE.

The new code uses the find() element-lookup function of the unordere map class,
and compares this to end().  If the found value is equal to end() (i.e. it
currently isn't in the map), NULL is used for the pointer (the desired behavior
in the swapchain layer).

Two new macros were used in order to keep the code minimal and consistent in
its use of find()/end().
diff --git a/layers/swapchain.cpp b/layers/swapchain.cpp
index 998e3eb..67a2f2b 100644
--- a/layers/swapchain.cpp
+++ b/layers/swapchain.cpp
@@ -85,7 +85,11 @@
     pDisp->QueuePresentKHR = (PFN_vkQueuePresentKHR)gpa(device, "vkQueuePresentKHR");
     pDisp->GetDeviceQueue = (PFN_vkGetDeviceQueue)gpa(device, "vkGetDeviceQueue");
 
-    SwpPhysicalDevice *pPhysicalDevice = &my_instance_data->physicalDeviceMap[physicalDevice];
+    SwpPhysicalDevice *pPhysicalDevice = NULL;
+    {
+        auto it = my_instance_data->physicalDeviceMap.find(physicalDevice);
+        pPhysicalDevice = (it == my_instance_data->physicalDeviceMap.end()) ? NULL : &it->second;
+    }
     if (pPhysicalDevice) {
         my_device_data->deviceMap[device].pPhysicalDevice = pPhysicalDevice;
         pPhysicalDevice->pDevice = &my_device_data->deviceMap[device];
@@ -290,7 +294,11 @@
 VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) {
     dispatch_key key = get_dispatch_key(instance);
     layer_data *my_data = get_my_data_ptr(key, layer_data_map);
-    SwpInstance *pInstance = &(my_data->instanceMap[instance]);
+    SwpInstance *pInstance = NULL;
+    {
+        auto it = my_data->instanceMap.find(instance);
+        pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
+    }
 
     // Call down the call chain:
     my_data->instance_dispatch_table->DestroyInstance(instance, pAllocator);
@@ -375,7 +383,11 @@
 
     // Record the result of this query:
     std::lock_guard<std::mutex> lock(global_lock);
-    SwpPhysicalDevice *pPhysicalDevice = &my_data->physicalDeviceMap[physicalDevice];
+    SwpPhysicalDevice *pPhysicalDevice = NULL;
+    {
+        auto it = my_data->physicalDeviceMap.find(physicalDevice);
+        pPhysicalDevice = (it == my_data->physicalDeviceMap.end()) ? NULL : &it->second;
+    }
     if (pPhysicalDevice && pQueueFamilyPropertyCount && !pQueueFamilyProperties) {
         pPhysicalDevice->gotQueueFamilyPropertyCount = true;
         pPhysicalDevice->numOfQueueFamilies = *pQueueFamilyPropertyCount;
@@ -390,7 +402,11 @@
     bool skipCall = false;
     layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
     std::unique_lock<std::mutex> lock(global_lock);
-    SwpInstance *pInstance = &(my_data->instanceMap[instance]);
+    SwpInstance *pInstance = NULL;
+    {
+        auto it = my_data->instanceMap.find(instance);
+        pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
+    }
 
     // Validate that the platform extension was enabled:
     if (pInstance && !pInstance->androidSurfaceExtensionEnabled) {
@@ -418,7 +434,10 @@
         lock.lock();
 
         // Obtain this pointer again after locking:
-        pInstance = &(my_data->instanceMap[instance]);
+        {
+            auto it = my_data->instanceMap.find(instance);
+            pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
+        }
         if ((result == VK_SUCCESS) && pInstance && pSurface) {
             // Record the VkSurfaceKHR returned by the ICD:
             my_data->surfaceMap[*pSurface].surface = *pSurface;
@@ -443,7 +462,11 @@
     bool skipCall = false;
     layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
     std::unique_lock<std::mutex> lock(global_lock);
-    SwpInstance *pInstance = &(my_data->instanceMap[instance]);
+    SwpInstance *pInstance = NULL;
+    {
+        auto it = my_data->instanceMap.find(instance);
+        pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
+    }
 
     // Validate that the platform extension was enabled:
     if (pInstance && !pInstance->mirSurfaceExtensionEnabled) {
@@ -471,7 +494,10 @@
         lock.lock();
 
         // Obtain this pointer again after locking:
-        pInstance = &(my_data->instanceMap[instance]);
+        {
+            auto it = my_data->instanceMap.find(instance);
+            pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
+        }
         if ((result == VK_SUCCESS) && pInstance && pSurface) {
             // Record the VkSurfaceKHR returned by the ICD:
             my_data->surfaceMap[*pSurface].surface = *pSurface;
@@ -494,7 +520,11 @@
     bool skipCall = false;
     layer_data *my_data = get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map);
     std::unique_lock<std::mutex> lock(global_lock);
-    SwpPhysicalDevice *pPhysicalDevice = &my_data->physicalDeviceMap[physicalDevice];
+    SwpPhysicalDevice *pPhysicalDevice = NULL;
+    {
+        auto it = my_data->physicalDeviceMap.find(physicalDevice);
+        pPhysicalDevice = (it == my_data->physicalDeviceMap.end()) ? NULL : &it->second;
+    }
 
     // Validate that the platform extension was enabled:
     if (pPhysicalDevice && pPhysicalDevice->pInstance && !pPhysicalDevice->pInstance->mirSurfaceExtensionEnabled) {
@@ -527,7 +557,11 @@
     bool skipCall = false;
     layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
     std::unique_lock<std::mutex> lock(global_lock);
-    SwpInstance *pInstance = &(my_data->instanceMap[instance]);
+    SwpInstance *pInstance = NULL;
+    {
+        auto it = my_data->instanceMap.find(instance);
+        pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
+    }
 
     // Validate that the platform extension was enabled:
     if (pInstance && !pInstance->waylandSurfaceExtensionEnabled) {
@@ -555,7 +589,10 @@
         lock.lock();
 
         // Obtain this pointer again after locking:
-        pInstance = &(my_data->instanceMap[instance]);
+        {
+            auto it = my_data->instanceMap.find(instance);
+            pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
+        }
         if ((result == VK_SUCCESS) && pInstance && pSurface) {
             // Record the VkSurfaceKHR returned by the ICD:
             my_data->surfaceMap[*pSurface].surface = *pSurface;
@@ -578,7 +615,11 @@
     bool skipCall = false;
     layer_data *my_data = get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map);
     std::unique_lock<std::mutex> lock(global_lock);
-    SwpPhysicalDevice *pPhysicalDevice = &my_data->physicalDeviceMap[physicalDevice];
+    SwpPhysicalDevice *pPhysicalDevice = NULL;
+    {
+        auto it = my_data->physicalDeviceMap.find(physicalDevice);
+        pPhysicalDevice = (it == my_data->physicalDeviceMap.end()) ? NULL : &it->second;
+    }
 
     // Validate that the platform extension was enabled:
     if (pPhysicalDevice && pPhysicalDevice->pInstance && !pPhysicalDevice->pInstance->waylandSurfaceExtensionEnabled) {
@@ -611,7 +652,11 @@
     bool skipCall = false;
     layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
     std::unique_lock<std::mutex> lock(global_lock);
-    SwpInstance *pInstance = &(my_data->instanceMap[instance]);
+    SwpInstance *pInstance = NULL;
+    {
+        auto it = my_data->instanceMap.find(instance);
+        pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
+    }
 
     // Validate that the platform extension was enabled:
     if (pInstance && !pInstance->win32SurfaceExtensionEnabled) {
@@ -639,7 +684,10 @@
         lock.lock();
 
         // Obtain this pointer again after locking:
-        pInstance = &(my_data->instanceMap[instance]);
+        {
+            auto it = my_data->instanceMap.find(instance);
+            pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
+        }
         if ((result == VK_SUCCESS) && pInstance && pSurface) {
             // Record the VkSurfaceKHR returned by the ICD:
             my_data->surfaceMap[*pSurface].surface = *pSurface;
@@ -661,7 +709,11 @@
     bool skipCall = false;
     layer_data *my_data = get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map);
     std::unique_lock<std::mutex> lock(global_lock);
-    SwpPhysicalDevice *pPhysicalDevice = &my_data->physicalDeviceMap[physicalDevice];
+    SwpPhysicalDevice *pPhysicalDevice = NULL;
+    {
+        auto it = my_data->physicalDeviceMap.find(physicalDevice);
+        pPhysicalDevice = (it == my_data->physicalDeviceMap.end()) ? NULL : &it->second;
+    }
 
     // Validate that the platform extension was enabled:
     if (pPhysicalDevice && pPhysicalDevice->pInstance && !pPhysicalDevice->pInstance->win32SurfaceExtensionEnabled) {
@@ -693,7 +745,11 @@
     bool skipCall = false;
     layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
     std::unique_lock<std::mutex> lock(global_lock);
-    SwpInstance *pInstance = &(my_data->instanceMap[instance]);
+    SwpInstance *pInstance = NULL;
+    {
+        auto it = my_data->instanceMap.find(instance);
+        pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
+    }
 
     // Validate that the platform extension was enabled:
     if (pInstance && !pInstance->xcbSurfaceExtensionEnabled) {
@@ -721,7 +777,10 @@
         lock.lock();
 
         // Obtain this pointer again after locking:
-        pInstance = &(my_data->instanceMap[instance]);
+        {
+            auto it = my_data->instanceMap.find(instance);
+            pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
+        }
         if ((result == VK_SUCCESS) && pInstance && pSurface) {
             // Record the VkSurfaceKHR returned by the ICD:
             my_data->surfaceMap[*pSurface].surface = *pSurface;
@@ -744,7 +803,11 @@
     bool skipCall = false;
     layer_data *my_data = get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map);
     std::unique_lock<std::mutex> lock(global_lock);
-    SwpPhysicalDevice *pPhysicalDevice = &my_data->physicalDeviceMap[physicalDevice];
+    SwpPhysicalDevice *pPhysicalDevice = NULL;
+    {
+        auto it = my_data->physicalDeviceMap.find(physicalDevice);
+        pPhysicalDevice = (it == my_data->physicalDeviceMap.end()) ? NULL : &it->second;
+    }
 
     // Validate that the platform extension was enabled:
     if (pPhysicalDevice && pPhysicalDevice->pInstance && !pPhysicalDevice->pInstance->xcbSurfaceExtensionEnabled) {
@@ -777,7 +840,11 @@
     bool skipCall = false;
     layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
     std::unique_lock<std::mutex> lock(global_lock);
-    SwpInstance *pInstance = &(my_data->instanceMap[instance]);
+    SwpInstance *pInstance = NULL;
+    {
+        auto it = my_data->instanceMap.find(instance);
+        pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
+    }
 
     // Validate that the platform extension was enabled:
     if (pInstance && !pInstance->xlibSurfaceExtensionEnabled) {
@@ -805,7 +872,10 @@
         lock.lock();
 
         // Obtain this pointer again after locking:
-        pInstance = &(my_data->instanceMap[instance]);
+        {
+            auto it = my_data->instanceMap.find(instance);
+            pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
+        }
         if ((result == VK_SUCCESS) && pInstance && pSurface) {
             // Record the VkSurfaceKHR returned by the ICD:
             my_data->surfaceMap[*pSurface].surface = *pSurface;
@@ -828,7 +898,11 @@
     bool skipCall = false;
     layer_data *my_data = get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map);
     std::unique_lock<std::mutex> lock(global_lock);
-    SwpPhysicalDevice *pPhysicalDevice = &my_data->physicalDeviceMap[physicalDevice];
+    SwpPhysicalDevice *pPhysicalDevice = NULL;
+    {
+        auto it = my_data->physicalDeviceMap.find(physicalDevice);
+        pPhysicalDevice = (it == my_data->physicalDeviceMap.end()) ? NULL : &it->second;
+    }
 
     // Validate that the platform extension was enabled:
     if (pPhysicalDevice && pPhysicalDevice->pInstance && !pPhysicalDevice->pInstance->xlibSurfaceExtensionEnabled) {
@@ -858,8 +932,16 @@
     bool skipCall = false;
     layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
     std::unique_lock<std::mutex> lock(global_lock);
-    SwpSurface *pSurface = &my_data->surfaceMap[surface];
-    SwpInstance *pInstance = &(my_data->instanceMap[instance]);
+    SwpSurface *pSurface = NULL;
+    {
+        auto it = my_data->surfaceMap.find(surface);
+        pSurface = (it == my_data->surfaceMap.end()) ? NULL : &it->second;
+    }
+    SwpInstance *pInstance = NULL;
+    {
+        auto it = my_data->instanceMap.find(instance);
+        pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
+    }
 
     // Validate that the platform extension was enabled:
     if (pInstance && !pInstance->surfaceExtensionEnabled) {
@@ -918,7 +1000,11 @@
     result = my_data->instance_dispatch_table->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
 
     std::lock_guard<std::mutex> lock(global_lock);
-    SwpInstance *pInstance = &(my_data->instanceMap[instance]);
+    SwpInstance *pInstance = NULL;
+    {
+        auto it = my_data->instanceMap.find(instance);
+        pInstance = (it == my_data->instanceMap.end()) ? NULL : &it->second;
+    }
     if ((result == VK_SUCCESS) && pInstance && pPhysicalDevices && (*pPhysicalDeviceCount > 0)) {
         // Record the VkPhysicalDevices returned by the ICD:
         for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++) {
@@ -984,7 +1070,11 @@
 
     // Do some internal cleanup:
     std::lock_guard<std::mutex> lock(global_lock);
-    SwpDevice *pDevice = &my_data->deviceMap[device];
+    SwpDevice *pDevice = NULL;
+    {
+        auto it = my_data->deviceMap.find(device);
+        pDevice = (it == my_data->deviceMap.end()) ? NULL : &it->second;
+    }
     if (pDevice) {
         // Delete the SwpDevice associated with this device:
         if (pDevice->pPhysicalDevice) {
@@ -1022,7 +1112,11 @@
     bool skipCall = false;
     layer_data *my_data = get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map);
     std::unique_lock<std::mutex> lock(global_lock);
-    SwpPhysicalDevice *pPhysicalDevice = &my_data->physicalDeviceMap[physicalDevice];
+    SwpPhysicalDevice *pPhysicalDevice = NULL;
+    {
+        auto it = my_data->physicalDeviceMap.find(physicalDevice);
+        pPhysicalDevice = (it == my_data->physicalDeviceMap.end()) ? NULL : &it->second;
+    }
 
     // Validate that the surface extension was enabled:
     if (pPhysicalDevice && pPhysicalDevice->pInstance && !pPhysicalDevice->pInstance->surfaceExtensionEnabled) {
@@ -1054,7 +1148,10 @@
         lock.lock();
 
         // Obtain this pointer again after locking:
-        pPhysicalDevice = &my_data->physicalDeviceMap[physicalDevice];
+        {
+            auto it = my_data->physicalDeviceMap.find(physicalDevice);
+            pPhysicalDevice = (it == my_data->physicalDeviceMap.end()) ? NULL : &it->second;
+        }
         if ((result == VK_SUCCESS) && pSupported && pPhysicalDevice) {
             // Record the result of this query:
             SwpInstance *pInstance = pPhysicalDevice->pInstance;
@@ -1087,7 +1184,11 @@
     bool skipCall = false;
     layer_data *my_data = get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map);
     std::unique_lock<std::mutex> lock(global_lock);
-    SwpPhysicalDevice *pPhysicalDevice = &my_data->physicalDeviceMap[physicalDevice];
+    SwpPhysicalDevice *pPhysicalDevice = NULL;
+    {
+        auto it = my_data->physicalDeviceMap.find(physicalDevice);
+        pPhysicalDevice = (it == my_data->physicalDeviceMap.end()) ? NULL : &it->second;
+    }
 
     // Validate that the surface extension was enabled:
     if (pPhysicalDevice && pPhysicalDevice->pInstance && !pPhysicalDevice->pInstance->surfaceExtensionEnabled) {
@@ -1108,7 +1209,10 @@
         lock.lock();
 
         // Obtain this pointer again after locking:
-        pPhysicalDevice = &my_data->physicalDeviceMap[physicalDevice];
+        {
+            auto it = my_data->physicalDeviceMap.find(physicalDevice);
+            pPhysicalDevice = (it == my_data->physicalDeviceMap.end()) ? NULL : &it->second;
+        }
         if ((result == VK_SUCCESS) && pPhysicalDevice) {
             // Record the result of this query:
             pPhysicalDevice->gotSurfaceCapabilities = true;
@@ -1127,7 +1231,11 @@
     bool skipCall = false;
     layer_data *my_data = get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map);
     std::unique_lock<std::mutex> lock(global_lock);
-    SwpPhysicalDevice *pPhysicalDevice = &my_data->physicalDeviceMap[physicalDevice];
+    SwpPhysicalDevice *pPhysicalDevice = NULL;
+    {
+        auto it = my_data->physicalDeviceMap.find(physicalDevice);
+        pPhysicalDevice = (it == my_data->physicalDeviceMap.end()) ? NULL : &it->second;
+    }
 
     // Validate that the surface extension was enabled:
     if (pPhysicalDevice && pPhysicalDevice->pInstance && !pPhysicalDevice->pInstance->surfaceExtensionEnabled) {
@@ -1165,7 +1273,10 @@
         lock.lock();
 
         // Obtain this pointer again after locking:
-        pPhysicalDevice = &my_data->physicalDeviceMap[physicalDevice];
+        {
+            auto it = my_data->physicalDeviceMap.find(physicalDevice);
+            pPhysicalDevice = (it == my_data->physicalDeviceMap.end()) ? NULL : &it->second;
+        }
         if ((result == VK_SUCCESS) && pPhysicalDevice && !pSurfaceFormats && pSurfaceFormatCount) {
             // Record the result of this preliminary query:
             pPhysicalDevice->surfaceFormatCount = *pSurfaceFormatCount;
@@ -1195,7 +1306,11 @@
     bool skipCall = false;
     layer_data *my_data = get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map);
     std::unique_lock<std::mutex> lock(global_lock);
-    SwpPhysicalDevice *pPhysicalDevice = &my_data->physicalDeviceMap[physicalDevice];
+    SwpPhysicalDevice *pPhysicalDevice = NULL;
+    {
+        auto it = my_data->physicalDeviceMap.find(physicalDevice);
+        pPhysicalDevice = (it == my_data->physicalDeviceMap.end()) ? NULL : &it->second;
+    }
 
     // Validate that the surface extension was enabled:
     if (pPhysicalDevice && pPhysicalDevice->pInstance && !pPhysicalDevice->pInstance->surfaceExtensionEnabled) {
@@ -1233,7 +1348,10 @@
         lock.lock();
 
         // Obtain this pointer again after locking:
-        pPhysicalDevice = &my_data->physicalDeviceMap[physicalDevice];
+        {
+            auto it = my_data->physicalDeviceMap.find(physicalDevice);
+            pPhysicalDevice = (it == my_data->physicalDeviceMap.end()) ? NULL : &it->second;
+        }
         if ((result == VK_SUCCESS) && pPhysicalDevice && !pPresentModes && pPresentModeCount) {
             // Record the result of this preliminary query:
             pPhysicalDevice->presentModeCount = *pPresentModeCount;
@@ -1265,7 +1383,11 @@
     bool skipCall = false;
     layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
     char fn[] = "vkCreateSwapchainKHR";
-    SwpDevice *pDevice = &my_data->deviceMap[device];
+    SwpDevice *pDevice = NULL;
+    {
+        auto it = my_data->deviceMap.find(device);
+        pDevice = (it == my_data->deviceMap.end()) ? NULL : &it->second;
+    }
 
     // Validate that the swapchain extension was enabled:
     if (pDevice && !pDevice->swapchainExtensionEnabled) {
@@ -1552,7 +1674,11 @@
 
     // Validate pCreateInfo->oldSwapchain:
     if (pCreateInfo && pCreateInfo->oldSwapchain) {
-        SwpSwapchain *pOldSwapchain = &my_data->swapchainMap[pCreateInfo->oldSwapchain];
+        SwpSwapchain *pOldSwapchain = NULL;
+        {
+          auto it = my_data->swapchainMap.find(pCreateInfo->oldSwapchain);
+          pOldSwapchain = (it == my_data->swapchainMap.end()) ? NULL : &it->second;
+        }
         if (pOldSwapchain) {
             if (device != pOldSwapchain->pDevice->device) {
                 skipCall |= LOG_ERROR(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, device, "VkDevice",
@@ -1593,7 +1719,11 @@
 
         if (result == VK_SUCCESS) {
             // Remember the swapchain's handle, and link it to the device:
-            SwpDevice *pDevice = &my_data->deviceMap[device];
+            SwpDevice *pDevice = NULL;
+            {
+                auto it = my_data->deviceMap.find(device);
+                pDevice = (it == my_data->deviceMap.end()) ? NULL : &it->second;
+            }
 
             my_data->swapchainMap[*pSwapchain].swapchain = *pSwapchain;
             if (pDevice) {
@@ -1628,7 +1758,11 @@
     bool skipCall = false;
     layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
     std::unique_lock<std::mutex> lock(global_lock);
-    SwpDevice *pDevice = &my_data->deviceMap[device];
+    SwpDevice *pDevice = NULL;
+    {
+        auto it = my_data->deviceMap.find(device);
+        pDevice = (it == my_data->deviceMap.end()) ? NULL : &it->second;
+    }
 
     // Validate that the swapchain extension was enabled:
     if (pDevice && !pDevice->swapchainExtensionEnabled) {
@@ -1638,7 +1772,11 @@
     }
 
     // Regardless of skipCall value, do some internal cleanup:
-    SwpSwapchain *pSwapchain = &my_data->swapchainMap[swapchain];
+    SwpSwapchain *pSwapchain = NULL;
+    {
+        auto it = my_data->swapchainMap.find(swapchain);
+        pSwapchain = (it == my_data->swapchainMap.end()) ? NULL : &it->second;
+    }
     if (pSwapchain) {
         // Delete the SwpSwapchain associated with this swapchain:
         if (pSwapchain->pDevice) {
@@ -1678,7 +1816,11 @@
     bool skipCall = false;
     layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
     std::unique_lock<std::mutex> lock(global_lock);
-    SwpDevice *pDevice = &my_data->deviceMap[device];
+    SwpDevice *pDevice = NULL;
+    {
+        auto it = my_data->deviceMap.find(device);
+        pDevice = (it == my_data->deviceMap.end()) ? NULL : &it->second;
+    }
 
     // Validate that the swapchain extension was enabled:
     if (pDevice && !pDevice->swapchainExtensionEnabled) {
@@ -1686,7 +1828,11 @@
                               "%s() called even though the %s extension was not enabled for this VkDevice.", __FUNCTION__,
                               VK_KHR_SWAPCHAIN_EXTENSION_NAME);
     }
-    SwpSwapchain *pSwapchain = &my_data->swapchainMap[swapchain];
+    SwpSwapchain *pSwapchain = NULL;
+    {
+        auto it = my_data->swapchainMap.find(swapchain);
+        pSwapchain = (it == my_data->swapchainMap.end()) ? NULL : &it->second;
+    }
     if (!pSwapchainImageCount) {
         skipCall |= LOG_ERROR_NULL_POINTER(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, device, "pSwapchainImageCount");
     } else if (pSwapchain && pSwapchainImages) {
@@ -1715,7 +1861,10 @@
         lock.lock();
 
         // Obtain this pointer again after locking:
-        pSwapchain = &my_data->swapchainMap[swapchain];
+        {
+            auto it = my_data->swapchainMap.find(swapchain);
+            pSwapchain = (it == my_data->swapchainMap.end()) ? NULL : &it->second;
+        }
         if ((result == VK_SUCCESS) && pSwapchain && !pSwapchainImages && pSwapchainImageCount) {
             // Record the result of this preliminary query:
             pSwapchain->imageCount = *pSwapchainImageCount;
@@ -1752,7 +1901,11 @@
     bool skipCall = false;
     layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
     std::unique_lock<std::mutex> lock(global_lock);
-    SwpDevice *pDevice = &my_data->deviceMap[device];
+    SwpDevice *pDevice = NULL;
+    {
+        auto it = my_data->deviceMap.find(device);
+        pDevice = (it == my_data->deviceMap.end()) ? NULL : &it->second;
+    }
 
     // Validate that the swapchain extension was enabled:
     if (pDevice && !pDevice->swapchainExtensionEnabled) {
@@ -1765,7 +1918,11 @@
                               "%s() called with both the semaphore and fence parameters set to "
                               "VK_NULL_HANDLE (at least one should be used).", __FUNCTION__);
     }
-    SwpSwapchain *pSwapchain = &my_data->swapchainMap[swapchain];
+    SwpSwapchain *pSwapchain = NULL;
+    {
+        auto it = my_data->swapchainMap.find(swapchain);
+        pSwapchain = (it == my_data->swapchainMap.end()) ? NULL : &it->second;
+    }
     SwpPhysicalDevice *pPhysicalDevice = pDevice->pPhysicalDevice;
     if (pSwapchain && pPhysicalDevice && pPhysicalDevice->gotSurfaceCapabilities) {
         // Look to see if the application has already acquired the maximum
@@ -1806,7 +1963,10 @@
         lock.lock();
 
         // Obtain this pointer again after locking:
-        pSwapchain = &my_data->swapchainMap[swapchain];
+        {
+            auto it = my_data->swapchainMap.find(swapchain);
+            pSwapchain = (it == my_data->swapchainMap.end()) ? NULL : &it->second;
+        }
         if (((result == VK_SUCCESS) || (result == VK_SUBOPTIMAL_KHR)) && pSwapchain) {
             // Change the state of the image (now acquired by the application):
             pSwapchain->images[*pImageIndex].acquiredByApp = true;
@@ -1854,7 +2014,11 @@
     std::unique_lock<std::mutex> lock(global_lock);
     for (uint32_t i = 0; pPresentInfo && (i < pPresentInfo->swapchainCount); i++) {
         uint32_t index = pPresentInfo->pImageIndices[i];
-        SwpSwapchain *pSwapchain = &my_data->swapchainMap[pPresentInfo->pSwapchains[i]];
+        SwpSwapchain *pSwapchain = NULL;
+        {
+            auto it = my_data->swapchainMap.find(pPresentInfo->pSwapchains[i]);
+            pSwapchain = (it == my_data->swapchainMap.end()) ? NULL : &it->second;
+        }
         if (pSwapchain) {
             if (!pSwapchain->pDevice->swapchainExtensionEnabled) {
                 skipCall |= LOG_ERROR(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, pSwapchain->pDevice, "VkDevice",
@@ -1877,7 +2041,11 @@
                                           __FUNCTION__, index);
                 }
             }
-            SwpQueue *pQueue = &my_data->queueMap[queue];
+            SwpQueue *pQueue = NULL;
+            {
+                auto it = my_data->queueMap.find(queue);
+                pQueue = (it == my_data->queueMap.end()) ? NULL : &it->second;
+            }
             SwpSurface *pSurface = pSwapchain->pSurface;
             if (pQueue && pSurface && pSurface->numQueueFamilyIndexSupport) {
                 uint32_t queueFamilyIndex = pQueue->queueFamilyIndex;
@@ -1907,7 +2075,11 @@
         if (pPresentInfo && ((result == VK_SUCCESS) || (result == VK_SUBOPTIMAL_KHR))) {
             for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
                 int index = pPresentInfo->pImageIndices[i];
-                SwpSwapchain *pSwapchain = &my_data->swapchainMap[pPresentInfo->pSwapchains[i]];
+                SwpSwapchain *pSwapchain = NULL;
+                {
+                    auto it = my_data->swapchainMap.find(pPresentInfo->pSwapchains[i]);
+                    pSwapchain = (it == my_data->swapchainMap.end()) ? NULL : &it->second;
+                }
                 if (pSwapchain) {
                     // Change the state of the image (no longer acquired by the
                     // application):
@@ -1931,7 +2103,11 @@
 
         // Remember the queue's handle, and link it to the device:
         std::lock_guard<std::mutex> lock(global_lock);
-        SwpDevice *pDevice = &my_data->deviceMap[device];
+        SwpDevice *pDevice = NULL;
+        {
+            auto it = my_data->deviceMap.find(device);
+            pDevice = (it == my_data->deviceMap.end()) ? NULL : &it->second;
+        }
         my_data->queueMap[&pQueue].queue = *pQueue;
         if (pDevice) {
             pDevice->queues[*pQueue] = &my_data->queueMap[*pQueue];