Fix potential resource leaks in buffer tests
authorKristof Kosztyo <kkosztyo.u-szeged@samsung.com>
Fri, 22 Jan 2016 10:33:06 +0000 (11:33 +0100)
committerKristof Kosztyo <kkosztyo.u-szeged@samsung.com>
Fri, 22 Jan 2016 10:34:07 +0000 (11:34 +0100)
Fixes #46

external/vulkancts/modules/vulkan/api/vktApiBufferTests.cpp
external/vulkancts/modules/vulkan/api/vktApiBufferViewCreateTests.cpp

index 7f98e8bf258ffa910aa9b06c900606078c825979..55a2745a23d501ec2370ede8f65cbd363072f3d1 100644 (file)
@@ -40,6 +40,7 @@
 #include "tcuTestLog.hpp"
 #include "vkPrograms.hpp"
 #include "vkQueryUtil.hpp"
+#include "vkRefUtil.hpp"
 #include "vktTestCase.hpp"
 
 namespace vkt
@@ -102,9 +103,9 @@ private:
 {
        const VkDevice                  vkDevice                        = m_context.getDevice();
        const DeviceInterface&  vk                                      = m_context.getDeviceInterface();
-       VkBuffer                                testBuffer;
+       Move<VkBuffer>                  testBuffer;
        VkMemoryRequirements    memReqs;
-       VkDeviceMemory                  memory;
+       Move<VkDeviceMemory>    memory;
        const deUint32                  queueFamilyIndex        = m_context.getUniversalQueueFamilyIndex();
 
        // Create buffer
@@ -121,10 +122,16 @@ private:
                        &queueFamilyIndex,
                };
 
-               if (vk.createBuffer(vkDevice, &bufferParams, (const VkAllocationCallbacks*)DE_NULL, &testBuffer) != VK_SUCCESS)
-                       return tcu::TestStatus::fail("Buffer creation failed! (requested memory size: " + de::toString(size) + ")");
+               try
+               {
+                       testBuffer = createBuffer(vk, vkDevice, &bufferParams, (const VkAllocationCallbacks*)DE_NULL);
+               }
+               catch (const vk::Error& error)
+               {
+                       return tcu::TestStatus::fail("Buffer creation failed! (requested memory size: " + de::toString(size) + ", Error code: " + de::toString(error.getMessage()) + ")");
+               }
 
-               vk.getBufferMemoryRequirements(vkDevice, testBuffer, &memReqs);
+               vk.getBufferMemoryRequirements(vkDevice, *testBuffer, &memReqs);
 
                if (size > memReqs.size)
                {
@@ -144,9 +151,14 @@ private:
                        0                                                                               //      deUint32                memoryTypeIndex
                };
 
-               if (vk.allocateMemory(vkDevice, &memAlloc, (const VkAllocationCallbacks*)DE_NULL, &memory) != VK_SUCCESS)
-                       return tcu::TestStatus::fail("Alloc memory failed! (requested memory size: " + de::toString(size) + ")");
-
+               try
+               {
+                       memory = allocateMemory(vk, vkDevice, &memAlloc, (const VkAllocationCallbacks*)DE_NULL);
+               }
+               catch (const vk::Error& error)
+               {
+                       return tcu::TestStatus::fail("Alloc memory failed! (requested memory size: " + de::toString(size) + ", Error code: " + de::toString(error.getMessage()) + ")");
+               }
 
                if ((m_testCase.flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT) ||
                        (m_testCase.flags & VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT) ||
@@ -160,14 +172,14 @@ private:
                        {
                                0,                                                                              // VkDeviceSize                                                         resourceOffset;
                                memReqs.size,                                                   // VkDeviceSize                                                         size;
-                               memory,                                                                 // VkDeviceMemory                                                       memory;
+                               *memory,                                                                // VkDeviceMemory                                                       memory;
                                0,                                                                              // VkDeviceSize                                                         memoryOffset;
                                0                                                                               // VkSparseMemoryBindFlags                                      flags;
                        };
 
                        const VkSparseBufferMemoryBindInfo      sparseBufferMemoryBindInfo      =
                        {
-                               testBuffer,                                                             // VkBuffer                                                                     buffer;
+                               *testBuffer,                                                    // VkBuffer                                                                     buffer;
                                1u,                                                                             // deUint32                                                                     bindCount;
                                &sparseMemoryBind                                               // const VkSparseMemoryBind*                            pBinds;
                        };
@@ -191,13 +203,10 @@ private:
                        if (vk.queueBindSparse(queue, 1, &bindSparseInfo, DE_NULL) != VK_SUCCESS)
                                return tcu::TestStatus::fail("Bind sparse buffer memory failed! (requested memory size: " + de::toString(size) + ")");
                } else
-                       if (vk.bindBufferMemory(vkDevice, testBuffer, memory, 0) != VK_SUCCESS)
+                       if (vk.bindBufferMemory(vkDevice, *testBuffer, *memory, 0) != VK_SUCCESS)
                                return tcu::TestStatus::fail("Bind buffer memory failed! (requested memory size: " + de::toString(size) + ")");
        }
 
-       vk.freeMemory(vkDevice, memory, (const VkAllocationCallbacks*)DE_NULL);
-       vk.destroyBuffer(vkDevice, testBuffer, (const VkAllocationCallbacks*)DE_NULL);
-
        return tcu::TestStatus::pass("Buffer test");
 }
 
index 9c14d79f83dff1ba85c22d6c17d5e4983026b796..4e494962a287eb319a777d2d002875e8f8d25739 100644 (file)
@@ -39,6 +39,7 @@
 #include "gluVarType.hpp"
 #include "tcuTestLog.hpp"
 #include "vkPrograms.hpp"
+#include "vkRefUtil.hpp"
 #include "vktTestCase.hpp"
 
 namespace vkt
@@ -107,7 +108,7 @@ tcu::TestStatus BufferViewTestInstance::iterate (void)
        const DeviceInterface&          vk                                              = m_context.getDeviceInterface();
        const deUint32                          queueFamilyIndex                = m_context.getUniversalQueueFamilyIndex();
        const VkDeviceSize                      size                                    = 3 * 5 * 7 * 64;
-       VkBuffer                                        testBuffer;
+       Move<VkBuffer>                          testBuffer;
        VkMemoryRequirements            memReqs;
        VkFormatProperties                      properties;
        const VkBufferCreateInfo        bufferParams =
@@ -126,10 +127,16 @@ tcu::TestStatus BufferViewTestInstance::iterate (void)
        if (!(properties.bufferFeatures & m_testCase.features))
                TCU_THROW(NotSupportedError, "Format not supported");
 
-       if (vk.createBuffer(vkDevice, &bufferParams, (const VkAllocationCallbacks*)DE_NULL, &testBuffer) != VK_SUCCESS)
-               return tcu::TestStatus::fail("Buffer creation failed!");
+       try
+       {
+               testBuffer = createBuffer(vk, vkDevice, &bufferParams, (const VkAllocationCallbacks*)DE_NULL);
+       }
+       catch (const vk::Error& error)
+       {
+               return tcu::TestStatus::fail("Buffer creation failed! (Error code: " + de::toString(error.getMessage()) + ")");
+       }
 
-       vk.getBufferMemoryRequirements(vkDevice, testBuffer, &memReqs);
+       vk.getBufferMemoryRequirements(vkDevice, *testBuffer, &memReqs);
 
        if (size > memReqs.size)
        {
@@ -138,7 +145,7 @@ tcu::TestStatus BufferViewTestInstance::iterate (void)
                return tcu::TestStatus::fail(errorMsg.str());
        }
 
-       VkDeviceMemory                          memory;
+       Move<VkDeviceMemory>            memory;
        const VkMemoryAllocateInfo      memAlloc                                =
        {
                VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,         //      VkStructureType         sType
@@ -149,53 +156,64 @@ tcu::TestStatus BufferViewTestInstance::iterate (void)
 
        {
                // Create buffer view.
-               VkBufferView                                    bufferView;
+               Move<VkBufferView>                              bufferView;
                const VkBufferViewCreateInfo    bufferViewCreateInfo    =
                {
                        VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO,      //      VkStructureType         sType;
                        NULL,                                                                           //      const void*                     pNext;
                        (VkBufferViewCreateFlags)0,
-                       testBuffer,                                                                     //      VkBuffer                        buffer;
+                       *testBuffer,                                                            //      VkBuffer                        buffer;
                        m_testCase.format,                                                      //      VkFormat                        format;
                        m_testCase.offset,                                                      //      VkDeviceSize            offset;
                        m_testCase.range,                                                       //      VkDeviceSize            range;
                };
 
-               if (vk.allocateMemory(vkDevice, &memAlloc, (const VkAllocationCallbacks*)DE_NULL, &memory) != VK_SUCCESS)
-                       return tcu::TestStatus::fail("Alloc memory failed!");
+               try
+               {
+                       memory = allocateMemory(vk, vkDevice, &memAlloc, (const VkAllocationCallbacks*)DE_NULL);
+               }
+               catch (const vk::Error& error)
+               {
+                       return tcu::TestStatus::fail("Alloc memory failed! (Error code: " + de::toString(error.getMessage()) + ")");
+               }
 
-               if (vk.bindBufferMemory(vkDevice, testBuffer, memory, 0) != VK_SUCCESS)
+               if (vk.bindBufferMemory(vkDevice, *testBuffer, *memory, 0) != VK_SUCCESS)
                        return tcu::TestStatus::fail("Bind buffer memory failed!");
 
-               if (vk.createBufferView(vkDevice, &bufferViewCreateInfo, (const VkAllocationCallbacks*)DE_NULL, &bufferView) != VK_SUCCESS)
-                       return tcu::TestStatus::fail("Buffer View creation failed!");
-
-               vk.destroyBufferView(vkDevice, bufferView, (const VkAllocationCallbacks*)DE_NULL);
+               try
+               {
+                       bufferView = createBufferView(vk, vkDevice, &bufferViewCreateInfo, (const VkAllocationCallbacks*)DE_NULL);
+               }
+               catch (const vk::Error& error)
+               {
+                       return tcu::TestStatus::fail("Buffer View creation failed! (Error code: " + de::toString(error.getMessage()) + ")");
+               }
        }
 
        // Testing complete view size.
        {
-               VkBufferView                    completeBufferView;
+               Move<VkBufferView>              completeBufferView;
                VkBufferViewCreateInfo  completeBufferViewCreateInfo    =
                {
                        VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO,      //      VkStructureType         sType;
                        NULL,                                                                           //      const void*                     pNext;
                        (VkBufferViewCreateFlags)0,
-                       testBuffer,                                                                     //      VkBuffer                        buffer;
+                       *testBuffer,                                                            //      VkBuffer                        buffer;
                        m_testCase.format,                                                      //      VkFormat                        format;
                        m_testCase.offset,                                                      //      VkDeviceSize            offset;
                        size,                                                                           //      VkDeviceSize            range;
                };
 
-               if (vk.createBufferView(vkDevice, &completeBufferViewCreateInfo, (const VkAllocationCallbacks*)DE_NULL, &completeBufferView) != VK_SUCCESS)
-                       return tcu::TestStatus::fail("Buffer View creation failed!");
-
-               vk.destroyBufferView(vkDevice, completeBufferView, (const VkAllocationCallbacks*)DE_NULL);
+               try
+               {
+                       completeBufferView = createBufferView(vk, vkDevice, &completeBufferViewCreateInfo, (const VkAllocationCallbacks*)DE_NULL);
+               }
+               catch (const vk::Error& error)
+               {
+                       return tcu::TestStatus::fail("Buffer View creation failed! (Error code: " + de::toString(error.getMessage()) + ")");
+               }
        }
 
-       vk.freeMemory(vkDevice, memory, (const VkAllocationCallbacks*)DE_NULL);
-       vk.destroyBuffer(vkDevice, testBuffer, (const VkAllocationCallbacks*)DE_NULL);
-
        return tcu::TestStatus::pass("BufferView test");
 }