为什么创建 2 个变量会导致自定义 STL C++ VS2019 崩溃?

Why does creating 2 variables cause a crash in custom STL, C++ VS2019?

提问人:Furkan Can Turan 提问时间:3/25/2020 最后编辑:BoannFurkan Can Turan 更新时间:3/26/2020 访问量:151

问:

您好,我正在尝试重写我自己的内存管理器和 STL(没什么花哨的,只是一些基本的矢量和字符串功能),但我得到了一个奇怪的行为。我正在努力获得内存管理领域的经验,因为我是一名有空闲时间的高中生。问题是,当我创建第一个变量时,一切都很完美,但是在创建第二个变量后,程序在创建第一个变量时崩溃了。

字符串.h/.cpp

    class String {
        char* pointer_toBuffer = nullptr;
        size_t buffer_length = 0;
        IAllocator* Allocator;
    public:
        String(const char* text, IAllocator* Allocator);}

    String::String(const char* text, TuranAPI::MemoryManagement::IAllocator* MemoryAllocator) : Allocator(MemoryAllocator) {
        std::cout << "String creation has started: " << text << std::endl;
        unsigned int i = 0;
        while (text[i] != 0) {
            i++;
        }
        buffer_length = i + 1;
        pointer_toBuffer = (char*)Allocator->Allocate_MemoryBlock(buffer_length * sizeof(char));//When I write the Second String part, FirstString crashes directly. I use VSDebug and it says access violation here while creating FirstString. It is successful if I delete the SecondString part.
        for (unsigned int letterindex = 0; letterindex < i; letterindex++) {
            pointer_toBuffer[letterindex] = text[letterindex];
        }
        pointer_toBuffer[i] = 0;
    }

内存管理.h/cpp

    TAPIMemoryAllocator::TAPIMemoryAllocator(MemoryBlockInfo MemoryPool_toUse){
        std::cout << "TAPIMemoryAllocator is created!\n";
        std::cout << "MemoryPool's start pointer: " << MemoryPool_toUse.address << std::endl;
        MemoryPool.address = MemoryPool_toUse.address;
        MemoryPool.size = MemoryPool_toUse.size;
        SELF = this;
    }
    void* TAPIMemoryAllocator::Allocate_MemoryBlock(size_t size) {
        std::cout << "MemoryPool's start pointer: " << MemoryPool.address << std::endl;
        std::cout << "A buffer of " << size << " bytes allocation request found in TAPIMemoryAllocator!\n";
        if (SELF == nullptr) {
            TMemoryManager First(1024 * 1024 * 1024 * 1);
            MemoryBlockInfo FirstMemoryBlock;
            FirstMemoryBlock.address = SELF->MemoryPool.address;
            FirstMemoryBlock.size = size;
            Allocated_MemoryBlocks[0] = FirstMemoryBlock;
            return (char*)SELF->MemoryPool.address;
        }
        void* finaladdress = SELF->MemoryPool.address;
        for (unsigned int blockindex = 0; blockindex < MAX_MEMORYBLOCKNUMBER; blockindex++) {
            MemoryBlockInfo& MemoryBlock = Allocated_MemoryBlocks[blockindex];
            finaladdress = (char*)finaladdress + MemoryBlock.size;
            if (size <= MemoryBlock.size && MemoryBlock.address == nullptr) {
                std::cout << "Intended block's size is less than found memory block!\n";
                MemoryBlock.address = finaladdress;
                //You shouldn't change Memory Block's size because all of the allocations before this are based upon the previous size!
                //You should move all the previous allocated memory to set the size (which is not ideal!)
                //If I'd want to find memory leaks causing this, I could write code here to log the leaks!
                return MemoryBlock.address;
            }
            else if (MemoryBlock.size == 0 && MemoryBlock.address == nullptr) {
                std::cout << "An empty block is created for intended block! Block's Array index is: " << blockindex << "\n";
                std::cout << "MemoryPool's start pointer: " << MemoryPool.address << std::endl << "MemoryBlock's pointer: " << finaladdress << std::endl;
                //This means this index in the Allocated_MemoryBlocks has never been used, so we can add the data here!
                MemoryBlock.address = finaladdress;
                MemoryBlock.size = size;
                return MemoryBlock.address;
            }
        }
        //If you arrive here, that means there is no empty memory block in the Allocated_MemoryBlocks array!
        std::cout << "There is no empty memory block in the Allocated_MemoryBlocks array, so nullptr is returned!\n";
        return nullptr;
    }

    TMemoryManager::TMemoryManager(size_t Main_MemoryBlockSize) {
        if (SELF != nullptr) {
            std::cout << "You shouldn't create a MemoryManager!";
            return;
        }
        std::cout << "TMemoryManager is created!\n";
        MainMemoryBlock.address = malloc(Main_MemoryBlockSize);
        MainMemoryBlock.size = Main_MemoryBlockSize;
        SELF = this;
        std::cout << "Main Memory Block's start pointer: " << MainMemoryBlock.address << std::endl;

        MemoryBlockInfo TuranAPI_MemoryPool;
        TuranAPI_MemoryPool.address = MainMemoryBlock.address;
        std::cout << "TuranAPI_MemoryPool.address: " << TuranAPI_MemoryPool.address << std::endl;
        TuranAPI_MemoryPool.size = 1024 * 1024 * 10;
        TAPIMemoryAllocator Create(TuranAPI_MemoryPool);
    }
    TMemoryManager* TMemoryManager::SELF = nullptr;
    TMemoryManager First(1024 * 1024 * 1024 * 1);

Main.cpp

String FirstString("How are you?", TAPIMemoryAllocator::SELF);
std::cout << FirstString << std::endl; //If I delete the below, it prints "How are you?" as expected
String SecondString("I'm fine, thanks!", TAPIMemoryAllocator::SELF);
std::cout << SecondString << std::endl; 
C++ 字符串 内存管理 visual-studio-2019

评论

5赞 Some programmer dude 3/25/2020
“创造失败”是什么意思?什么?请编辑您的问题以显示一个最小的可重复示例(强调最小和完整的部分)。另外,请花一些时间阅读如何提问,以及这个问题清单FirstStringTuranAPI::MemoryManagement::TAPIMemoryAllocator::SELF
0赞 Furkan Can Turan 3/25/2020
谢谢,我修好了它们。我不想添加它们,因为帖子越来越长,而且调试的报告不是关于 Allocator 的。如果 SecondString 是在 main.cpp 中编码的,则 VSDebug 表示在创建 FirstString 时存在访问冲突。如果我删除 main.cpp 的 SecondString 部分,则不会出现错误,它会打印 你好吗?不出所料
1赞 drescherjm 3/25/2020
这将是同一个调试器。虽然我从 1982 年开始调试代码,从 1997 年开始专业调试。您需要设置断点并逐行单步执行代码。您需要知道 F9、F10 和 F11 键的作用,这些是 Visual Studio 中最重要的键。如果在某些不属于你的代码中遇到访问冲突,请使用 Visual Studio 工具栏上的“堆栈框架”向上移动到代码的调用堆栈。
1赞 Some programmer dude 3/25/2020
分配器中确实存在错误:if (SELF == nullptr) { ... FirstMemoryBlock.address = SELF->MemoryPool.address; ... }
1赞 drescherjm 3/26/2020
我可能错了,但是当我尝试遵循脑海中的代码时,这是一个问题。我现在帮不上忙了。今天晚些时候为有薪工作举行虚拟会议。SELF = this;

答:

0赞 Furkan Can Turan 3/26/2020 #1

已解决:问题出在 Allocator 中。当分配器超出范围时,将删除其Allocate_MemoryBlock函数(它是一个虚拟函数,而不是静态函数)。我不知道为什么当只创建一个字符串(可能是编译器优化)时不会发生这种情况,但是存储 Allocator 本身(所有变量都已经是静态的)并将 SELF 作为存储的指针解决了问题。

评论

3赞 drescherjm 3/26/2020
当您遇到未定义行为时,最不幸的行为之一是代码按预期工作,让您误以为代码是正确的。
2赞 drescherjm 3/26/2020
小心静态初始化顺序惨败。