Skip to content

Commit

Permalink
To address valgrind mismatched new/delete introduced a local DeleteHa…
Browse files Browse the repository at this point in the history
…ndler to delete the same type as new'd.
  • Loading branch information
robertosfield committed Oct 2, 2024
1 parent 3ca9de4 commit 6b183fb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
10 changes: 7 additions & 3 deletions include/vsg/vk/DeviceFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI

#include <map>
#include <vsg/vk/PhysicalDevice.h>
#include <vsg/io/Logger.h>

namespace vsg
{
Expand All @@ -36,14 +37,15 @@ namespace vsg
template<typename FeatureStruct, VkStructureType type>
FeatureStruct& get()
{
if (auto itr = _features.find(type); itr != _features.end()) return *reinterpret_cast<FeatureStruct*>(itr->second);
if (auto itr = _features.find(type); itr != _features.end()) return *reinterpret_cast<FeatureStruct*>(itr->second.first);

FeatureStruct* feature = new FeatureStruct{};

feature->sType = type;
feature->pNext = nullptr;

_features[type] = reinterpret_cast<FeatureHeader*>(feature);
_features[type].first = reinterpret_cast<FeatureHeader*>(feature);
_features[type].second = [](FeatureHeader* ptr) { delete reinterpret_cast<FeatureStruct*>(ptr); };

return *feature;
}
Expand All @@ -70,7 +72,9 @@ namespace vsg
void* pNext;
};

std::map<VkStructureType, FeatureHeader*> _features;
using DeleteHandler = void (*)(FeatureHeader* ptr);

std::map<VkStructureType, std::pair<FeatureHeader*, DeleteHandler>> _features;
};
VSG_type_name(vsg::DeviceFeatures);

Expand Down
20 changes: 5 additions & 15 deletions src/vsg/vk/DeviceFeatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,15 @@ DeviceFeatures::~DeviceFeatures()

VkPhysicalDeviceFeatures& DeviceFeatures::get()
{
if (auto itr = _features.find(VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2); itr != _features.end())
{
return reinterpret_cast<VkPhysicalDeviceFeatures2*>(itr->second)->features;
}

VkPhysicalDeviceFeatures2* feature = new VkPhysicalDeviceFeatures2{};
feature->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
feature->pNext = nullptr;

_features[VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2] = reinterpret_cast<FeatureHeader*>(feature);
return get<VkPhysicalDeviceFeatures2, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2>().features;

return feature->features;
}

void DeviceFeatures::clear()
{
for (auto& feature : _features)
{
delete feature.second;
feature.second.second(feature.second.first);
}

_features.clear();
Expand All @@ -58,10 +48,10 @@ void* DeviceFeatures::data() const
FeatureHeader* previous = nullptr;
for (auto itr = _features.rbegin(); itr != _features.rend(); ++itr)
{
itr->second->pNext = previous;
previous = itr->second;
itr->second.first->pNext = previous;
previous = itr->second.first;
}

// return head of the chain
return const_cast<void*>(reinterpret_cast<const void*>(_features.begin()->second));
return const_cast<void*>(reinterpret_cast<const void*>(_features.begin()->second.first));
}

0 comments on commit 6b183fb

Please sign in to comment.