异常错误C++ //Visual Studio

Exception Error C++ //Visual Studio

本文关键字:Visual Studio C++ 错误 异常      更新时间:2023-10-16

所以我从scrath(学习目的(创建一个引擎。 当我测试我的TArray时,我在我的Memory.h文件中抛出了一个 Execption。仅当 im 尝试测试TArray时,才会发生这种情况

这是我的TArray.h代码

#pragma once
#include "Memory/DynamicLinearStackAllocator.h"
template <typename T, typename Allocator =  DynamicLinearStackAllocator>
class TArray
{
private:
Allocator m_Allocator;
uint32 m_ElementCount;
public:
FORCEINLINE TArray()
{
}
FORCEINLINE ~TArray()
{
m_Allocator.Destroy();
}
FORCEINLINE TArray(const uint32 InElementCount)
{
m_Allocator.Resize<T>(InElementCount, m_ElementCount);
m_ElementCount = InElementCount;
}
FORCEINLINE void Add(const T &InValue)
{
}
FORCEINLINE T* GetData() const { return m_Allocator.GetAllocator<T>(); }
FORCEINLINE uint32 Num() const
{
return m_ElementCount;
}
FORCEINLINE T& operator[](uint32 InElementIndex) const
{
check(m_ElementCount > InElementIndex);
return GetData()[InElementIndex];
}
};
namespace Tests
{
FORCEINLINE void TestArrays()
{
{
TArray<float> data(10);
check(data.Num() == 10);
for(uint32 i = 0; i < data.Num(); i++)
{
data[i] = 2.0f;
}
check(data[0] == 2.0f);
}
{
TArray<uint32> data(5);
data[0] = 10;
data[1] = 5;
check(data.Num() == 5);
check(data[0] == 10);
check(data[1] == 5);
}
{
TArray<float> data(1);
data[2] = 2.0f;
}
}
}

当我尝试编译(我使用 Visual Studio 2017(时,它在Memory.h中的这个位置给了我一个执行错误。

static void* Copy(void *InDestination, const void *InSource, size_t InSize)
{
check_slow(InDestination);
check_slow(InSource);
check_slow(InSize > 0);
return memcpy(InDestination, InSource, InSize);
}

在主函数中使用以下代码调用测试:

int main()
{
Tests::TestAssertion();
Tests::TestMemory();
Tests::TestAllocator();
Tests::TestArrays();
return 0;
}

我目前的目标是查看记录器是否在此处显示错误:

{
TArray<float> data(1);
data[2] = 2.0f;
}

这是完整错误 https://gyazo.com/6c1d6779623ffea97504f5b23f9fd7da 的屏幕截图

编辑:这是分配器的代码

#pragma once
#include <malloc.h>
#include "Core.h"
#define MEMORY_ALIGMENT 16
struct Memory
{
// TODO:Rework types.
static void* Allocate(const int32 InCount, const size_t InSize)
{
check_slow(InCount > 0);
check_slow(InSize > 0);
const size_t size = InSize * InCount;
return _aligned_malloc(size, MEMORY_ALIGMENT);
}
static void Free(void *InBlock)
{
check_slow(InBlock);
_aligned_free(InBlock);
}
static void* Copy(void *InDestination, const void *InSource, size_t InSize)
{
check_slow(InDestination);
check_slow(InSource);
check_slow(InSize > 0);
return memcpy(InDestination, InSource, InSize);
}
};
void* operator new (size_t InSize)
{
return Memory::Allocate(1, InSize);
}
void operator delete (void* InBlock)
{
Memory::Free(InBlock);
}

namespace Tests
{
struct MemoryTestStruct
{
uint32 p0;
uint32 p1;
uint32 p2;
uint32 p3;
};
FORCEINLINE void TestMemory()
{
MemoryTestStruct *t = new MemoryTestStruct();
check(t);
delete t;
}
}

edit2:这是 StackAlloctor 的代码

#pragma once
#include "../Core.h"
class DynamicLinearStackAllocator
{
private:
void *m_Data;
public:
template <typename T>
FORCEINLINE void Resize(const uint32 InElementCount, const uint32 InPreviousElementCount)
{
void *temp = Memory::Allocate(InElementCount, sizeof(T));
if (InPreviousElementCount > 0)
{
const SIZE_T size = sizeof(T) * InPreviousElementCount;
Memory::Copy(temp, m_Data, size);
Memory::Free(m_Data);
}
m_Data = temp;
}
template <typename T>
FORCEINLINE T* GetAllocator() const
{
return (T*)m_Data;
}
FORCEINLINE void Destroy()
{
Memory::Free(m_Data);
}
};
namespace Tests
{
FORCEINLINE void TestAllocator()
{
DynamicLinearStackAllocator alloc;
alloc.Resize<float>(2, 0);
alloc.Destroy();
}
}

您没有显示分配器的代码,但问题可能出在TArray构造函数中的这一行:

m_Allocator.Resize<T>(InElementCount, m_ElementCount);

此时,m_ElementCount尚未初始化,并且其中将有一些随机值。 然后,Resize可能会尝试释放尚未分配的内存(因为m_ElementCount中未初始化的值(。 您应该为构造函数中Resize调用的第二个参数传入 0

m_Allocator.Resize<T>(InElementCount, 0);

因为没有现有的已分配内存可供释放。

此外,TArray的默认构造函数应将m_Allocator.m_data初始化为nullptr(或将默认构造函数添加到 DynamicLinearStackAllocator 以执行此操作(并将m_ElementCount设置为 0。