瀏覽代碼

Fixing a bug in ResourcePointer

Panagiotis Christopoulos Charitos 11 年之前
父節點
當前提交
93f4bbd493

+ 1 - 1
include/anki/gl/GlCommon.h

@@ -28,7 +28,7 @@
 #	error "See file"
 #endif
 
-#define ANKI_QUEUE_DISABLE_ASYNC 0
+#define ANKI_QUEUE_DISABLE_ASYNC 1
 
 namespace anki {
 

+ 11 - 4
include/anki/resource/DummyRsrc.h

@@ -30,10 +30,17 @@ public:
 
 	void load(const CString& filename, ResourceInitializer& init)
 	{
-		m_alloc = init.m_alloc;
-		m_memory = m_alloc.allocate(128);
-		void* tempMem = init.m_tempAlloc.allocate(128);
-		(void)tempMem;
+		if(filename != "exception")
+		{
+			m_alloc = init.m_alloc;
+			m_memory = m_alloc.allocate(128);
+			void* tempMem = init.m_tempAlloc.allocate(128);
+			(void)tempMem;
+		}
+		else
+		{
+			throw ANKI_EXCEPTION("Dummy exception");
+		}
 	}
 
 private:

+ 18 - 5
include/anki/resource/ResourcePointer.inl.h

@@ -20,21 +20,33 @@ void ResourcePointer<T, TResourceManager>::load(
 
 	if(!found)
 	{
+		auto alloc = resources->_getAllocator();
+
 		// Allocate m_cb
 		U len = filename.getLength();
 		PtrSize alignment = alignof(ControlBlock);
 		m_cb = reinterpret_cast<ControlBlock*>(
-			resources->_getAllocator().allocate(
+			alloc.allocate(
 			sizeof(ControlBlock) + len, &alignment));
-		resources->_getAllocator().construct(m_cb);
 
-		m_cb->m_resources = resources;
+		// Construct
+		try
+		{
+			alloc.construct(m_cb);
+		}
+		catch(const std::exception& e)
+		{
+			alloc.deallocate(m_cb, 1);
+			m_cb = nullptr;
+			throw ANKI_EXCEPTION("Control block construction failed: %s", 
+				&filename[0]) << e;
+		}
 
 		// Populate the m_cb
 		try
 		{
 			ResourceInitializer init(
-				resources->_getAllocator(),
+				alloc,
 				resources->_getTempAllocator(),
 				*resources);
 
@@ -44,7 +56,8 @@ void ResourcePointer<T, TResourceManager>::load(
 		}
 		catch(const std::exception& e)
 		{
-			resources->_getAllocator().deleteInstance(m_cb);
+			alloc.deleteInstance(m_cb);
+			m_cb = nullptr;
 			throw ANKI_EXCEPTION("Loading failed: %s", &filename[0]) << e;
 		}
 

+ 7 - 0
include/anki/util/String.h

@@ -136,6 +136,13 @@ public:
 		return std::strcmp(m_ptr, b.m_ptr) == 0;
 	}
 
+	Bool operator!=(const CString& b) const noexcept
+	{
+		checkInit();
+		b.checkInit();
+		return std::strcmp(m_ptr, b.m_ptr) != 0;
+	}
+
 	Bool operator<(const CString& b) const noexcept
 	{
 		checkInit();

+ 18 - 0
tests/resource/ResourceManager.cpp

@@ -63,6 +63,24 @@ ANKI_TEST(Resource, ResourceManager)
 		ANKI_TEST_EXPECT_EQ(a.getReferenceCount(), refcount + 2);
 	}
 
+	// Exception
+	{
+		try
+		{
+			DummyResourcePointer a("exception", resources);
+		}
+		catch(...)
+		{}
+
+		DummyResourcePointer a;
+		try
+		{
+			a.load("exception", resources);
+		}
+		catch(...)
+		{}
+	}
+
 	// Delete
 	alloc.deleteInstance(resources);
 	alloc.deleteInstance(gl);