From 01eacfd6759b92f0490161cc6259509bc949eb0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 2 Nov 2013 23:37:44 +0100 Subject: [PATCH] Add static_assert for deletion of only complete types in ResourceManager. Borrowed from libstdc++'s std::unique_ptr implementation, on GCC it generates somehow different error than expected (complains about invalid application of sizeof() where it should (or not?) print the static_assert message. --- src/ResourceManager.h | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/ResourceManager.h b/src/ResourceManager.h index ab8c70659..9186f00a5 100644 --- a/src/ResourceManager.h +++ b/src/ResourceManager.h @@ -453,9 +453,14 @@ template ResourceManager*& ResourceManager:: namespace Implementation { +template void safeDelete(T* data) { + static_assert(sizeof(T) > 0, "Cannot delete pointer to incomplete type"); + delete data; +} + template ResourceManagerData::~ResourceManagerData() { /* Loaders are already deleted via freeLoaders() from ResourceManager */ - delete _fallback; + safeDelete(_fallback); } template std::size_t ResourceManagerData::referenceCount(const ResourceKey key) const { @@ -509,7 +514,7 @@ template void ResourceManagerData::set(const ResourceKey key, T* con /* If nothing is referencing reference-counted resource, we're done */ if(policy == ResourcePolicy::ReferenceCounted && (it == _data.end() || it->second.referenceCount == 0)) { Warning() << "ResourceManager: Reference-counted resource with key" << key << "isn't referenced from anywhere, deleting it immediately"; - delete data; + safeDelete(data); /* Delete also already present resource (it could be here because previous policy could be other than @@ -527,7 +532,7 @@ template void ResourceManagerData::set(const ResourceKey key, T* con #endif /* Replace previous data */ - delete it->second.data; + safeDelete(it->second.data); it->second.data = data; it->second.state = state; it->second.policy = policy; @@ -535,7 +540,7 @@ template void ResourceManagerData::set(const ResourceKey key, T* con } template void ResourceManagerData::setFallback(T* const data) { - delete _fallback; + safeDelete(_fallback); _fallback = data; } @@ -595,7 +600,7 @@ template struct ResourceManagerData::Data { template inline ResourceManagerData::Data::~Data() { CORRADE_ASSERT(referenceCount == 0, "ResourceManager: cleared/destroyed while data are still referenced", ); - delete data; + safeDelete(data); } }