loader: use callbacks from CreateInstance

If an application links a VkDebugReportCreateInfo
structure to the InstanceCreateInfo structure the
loader will log the callback for the duration of
the CreateInstance call. This allows the app
to catch any loader issues detected at CreateInstance
time.
diff --git a/loader/trampoline.c b/loader/trampoline.c
index 58a3d2f..3c32d97 100644
--- a/loader/trampoline.c
+++ b/loader/trampoline.c
@@ -70,15 +70,20 @@
         ptr_instance->alloc_callbacks = *pAllocator;
     }
 
-    while (!pNext) {
+    /*
+     * Look for a debug report create info structure
+     * and setup a callback if found.
+     */
+    while (pNext) {
         if (((VkInstanceCreateInfo *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_LUNARG) {
             instance_callback = (VkDebugReportCallbackLUNARG) ptr_instance;
             if (util_CreateDebugReportCallback(ptr_instance, pNext, pAllocator, instance_callback)) {
-                free(ptr_instance);
+                loader_heap_free(ptr_instance, ptr_instance);
                 loader_platform_thread_unlock_mutex(&loader_lock);
                 return VK_ERROR_OUT_OF_HOST_MEMORY;
             }
         }
+        pNext = (void *) ((VkInstanceCreateInfo *)pNext)->pNext;
     }
 
     /* Due to implicit layers need to get layer list even if
@@ -96,6 +101,8 @@
                                      pCreateInfo->ppEnabledLayerNames,
                                      &ptr_instance->instance_layer_list);
         if (res != VK_SUCCESS) {
+            util_DestroyDebugReportCallback(ptr_instance, instance_callback, pAllocator);
+            loader_heap_free(ptr_instance, ptr_instance);
             loader_platform_thread_unlock_mutex(&loader_lock);
             return res;
         }
@@ -120,6 +127,7 @@
         loader_scanned_icd_clear(ptr_instance, &ptr_instance->icd_libs);
         loader_destroy_generic_list(ptr_instance, (struct loader_generic_list *)
                                     &ptr_instance->ext_list);
+        util_DestroyDebugReportCallback(ptr_instance, instance_callback, pAllocator);
         loader_platform_thread_unlock_mutex(&loader_lock);
         loader_heap_free(ptr_instance, ptr_instance);
         return res;
@@ -138,6 +146,7 @@
                                  &ptr_instance->icd_libs);
         loader_destroy_generic_list(ptr_instance, (struct loader_generic_list *)
                                 &ptr_instance->ext_list);
+        util_DestroyDebugReportCallback(ptr_instance, instance_callback, pAllocator);
         loader_platform_thread_unlock_mutex(&loader_lock);
         loader_heap_free(ptr_instance, ptr_instance);
         return VK_ERROR_OUT_OF_HOST_MEMORY;
@@ -160,6 +169,7 @@
         loader_destroy_generic_list(ptr_instance, (struct loader_generic_list *)
                                 &ptr_instance->ext_list);
         loader.instances = ptr_instance->next;
+        util_DestroyDebugReportCallback(ptr_instance, instance_callback, pAllocator);
         loader_platform_thread_unlock_mutex(&loader_lock);
         loader_heap_free(ptr_instance, ptr_instance->disp);
         loader_heap_free(ptr_instance, ptr_instance);
@@ -183,6 +193,9 @@
      */
     loader_activate_instance_layer_extensions(ptr_instance);
 
+    /* Remove temporary debug_report callback */
+    util_DestroyDebugReportCallback(ptr_instance, instance_callback, pAllocator);
+
     loader_platform_thread_unlock_mutex(&loader_lock);
     return res;
 }
@@ -197,6 +210,8 @@
 
     loader_platform_thread_lock_mutex(&loader_lock);
 
+    /* TODO: Do we need a temporary callback here to catch cleanup issues? */
+
     ptr_instance = loader_get_instance(instance);
     disp->DestroyInstance(instance, pAllocator);