У меня есть статический встроенный std::unordered_map. По какой-то причине в конце программы некоторые классы внутри карты выдавали ошибку при автоматическом уничтожении:
Invalid address specified to RtlValidateHeap( 0000027D28B70000, 0000027D28BA5BA8 )
После некоторого тестирования я обнаружил, что это происходит только в том случае, если класс принадлежит родительскому классу, содержащему виртуальную функцию. Есть ли способ исправить это или обойти это?
Это часть моего кода:
main.cpp
#include
class Parent
{
public:
virtual int function(){
return -1;
}
};
class Child : public Parent, public Harmony::Core::Object
{
public:
Child(uint64_t uuid) :
Harmony::Core::Object(uuid, this) {};
int function(){
return 0;
}
};
int main()
{
Child::create(NULL); // create a child class and store it in an static inline map
}
Object.h
#pragma once
#include
#include
#include
#include
#include
#include
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Header
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace Harmony
{
namespace Core
{
template
class Object
{
public:
typedef std::unique_ptr Pointer;
public:
Object(const uint64_t& uuid, Type* pointer);
~Object();
const uint64_t getUUID();
static Type& find(const uint64_t& uuid);
static Type& create(const uint64_t& uuid);
static void add(std::unique_ptr pointer);
static void rmv(const uint64_t& uuid);
static const uint64_t randomUUID();
protected:
static const uint64_t assignUUID(const uint64_t uuid);
protected:
const uint64_t m_uuid;
static inline std::unordered_map m_pointers;
static inline std::unordered_map m_objects;
};
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template
inline Harmony::Core::Object::Object(const uint64_t& uuid, Type* pointer) :
Harmony::Core::Object::m_uuid(Harmony::Core::Object::assignUUID(uuid))
{
Harmony::Core::Object::m_pointers[Harmony::Core::Object::m_uuid] = pointer;
}
template
inline Harmony::Core::Object::~Object()
{
assert(Harmony::Core::Object::m_pointers.contains(Harmony::Core::Object::m_uuid));
Harmony::Core::Object::m_pointers.erase(Harmony::Core::Object::m_uuid);
}
template
inline const uint64_t Harmony::Core::Object::getUUID()
{
return Harmony::Core::Object::m_uuid;
}
template
inline Type& Harmony::Core::Object::find(const uint64_t& uuid)
{
assert(Harmony::Core::Object::m_pointers.contains(uuid));
return *Harmony::Core::Object::m_pointers[uuid];
}
template
inline Type& Harmony::Core::Object::create(const uint64_t& uuid)
{
std::unique_ptr pointer = std::make_unique(uuid);
Type& reference = *pointer;
Harmony::Core::Object::add(std::move(pointer));
return reference;
}
template
inline void Harmony::Core::Object::add(std::unique_ptr pointer)
{
const uint64_t& uuid(pointer->getUUID());
assert(!Harmony::Core::Object::m_objects.contains(uuid));
Harmony::Core::Object::m_objects[uuid] = std::move(pointer);
}
template
inline void Harmony::Core::Object::rmv(const uint64_t& uuid)
{
assert(Harmony::Core::Object::m_pointers.contains(uuid));
if (Harmony::Core::Object::m_objects.contains(uuid))
Harmony::Core::Object::m_objects.erase(uuid);
if (Harmony::Core::Object::m_pointers.contains(uuid))
Harmony::Core::Object::m_pointers.erase(uuid);
}
template
inline const uint64_t Harmony::Core::Object::randomUUID()
{
static std::random_device randomDevice;
static std::mt19937_64 engine(randomDevice());
static std::uniform_int_distribution uniformDistribution;
return uniformDistribution(engine);
}
template
inline const uint64_t Harmony::Core::Object::assignUUID(const uint64_t uuid)
{
uint64_t index(uuid);
while (index == NULL or Harmony::Core::Object::m_pointers.contains(index))
index = Harmony::Core::Object::randomUUID();
return index;
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... -at-the-en