cpp 当 std::queue 初始化时,它分配内存,然后抛出访问冲突读取位置

问题描述 投票:0回答:1

问题描述

我的复杂代码可以简化如下:

#include <vulkan/vulkan_raii.hpp>

#include <functional>
#include <queue>
#include <iostream>

class TestClass
{
    std::queue<std::function<void(vk::raii::CommandBuffer const&)>> m_pending_commands;
};

int main()
{
    TestClass testClassIns;
    
    do 
    {
        std::cout << '\n' << "Press a key to continue...";
    } while (std::cin.get() != '\n');

    return 0;
}

当我创建一个新类时,队列成员被初始化,它分配内存,然后抛出访问冲突读取位置错误。

我之前的班级中有队列成员,并且它不会抛出错误。事实上,如果你编译下面的代码,是没有错误的。该错误是在最近提交后发生的。

那么bug原因一定在其他地方?

我修改了与队列无关的部分。或者它们没有什么关系,我从类中删除一个数据成员,并添加一个新类类型的新数据成员。

这意味着:

// before the commit leading bug
class TestClass
{
    // ...
    MyClassA insA;
    // ...
    std::queue<std::function<void(vk::raii::CommandBuffer const&)>> m_pending_commands;
};

// after the commit
class TestClass
{
    // ...
    MyClassB insB;
    // ...
    std::queue<std::function<void(vk::raii::CommandBuffer const&)>> m_pending_commands;
};

有什么建议吗?谢谢!

更多信息

我的错误提交

https://github.com/CheapMeow/MeowEngine/commit/56f3f9628aa2701fb3b12cb7d1b82fc1177056f0

我最后一次可以编译成功的提交

https://github.com/CheapMeow/MeowEngine/commit/f14e8e5640963949f04cadd35abff3f50bcbabe2

我的简化代码,没有错误,在

https://github.com/CheapMeow/VulkanPlayGround/commit/1a00d2d03c14b589312cd196f3b05d2c4ad58a13

异常抛出时我的调用堆栈

ntdll.dll!00007ffb050aecbb()    Unknown
ntdll.dll!00007ffb0504d1aa()    Unknown
ntdll.dll!00007ffb0504c7ba()    Unknown
ucrtbased.dll!00007ffa5c09d480()    Unknown
ucrtbased.dll!00007ffa5c09d20d()    Unknown
ucrtbased.dll!00007ffa5c0a037f()    Unknown
ucrtbased.dll!00007ffa5c0a0dee()    Unknown
>   MeowRuntimed.dll!operator new(unsigned __int64 size) Line 36    C++
MeowRuntimed.dll!std::_Default_allocate_traits::_Allocate(const unsigned __int64 _Bytes) Line 91    C++
MeowRuntimed.dll!std::_Allocate<16,std::_Default_allocate_traits,0>(const unsigned __int64 _Bytes) Line 248 C++
MeowRuntimed.dll!std::allocator<std::_Container_proxy>::allocate(const unsigned __int64 _Count) Line 983    C++
MeowRuntimed.dll!std::_Container_base12::_Alloc_proxy<std::allocator<std::_Container_proxy>>(std::allocator<std::_Container_proxy> && _Al) Line 1212    C++
MeowRuntimed.dll!std::deque<std::function<void __cdecl(vk::raii::CommandBuffer const &)>,std::allocator<std::function<void __cdecl(vk::raii::CommandBuffer const &)>>>::deque<std::function<void __cdecl(vk::raii::CommandBuffer const &)>,std::allocator<std::function<void __cdecl(vk::raii::CommandBuffer const &)>>>() Line 633 C++
MeowRuntimed.dll!std::queue<std::function<void __cdecl(vk::raii::CommandBuffer const &)>,std::deque<std::function<void __cdecl(vk::raii::CommandBuffer const &)>,std::allocator<std::function<void __cdecl(vk::raii::CommandBuffer const &)>>>>::queue<std::function<void __cdecl(vk::raii::CommandBuffer const &)>,std::deque<std::function<void __cdecl(vk::raii::CommandBuffer const &)>,std::allocator<std::function<void __cdecl(vk::raii::CommandBuffer const &)>>>>() Line 39    C++
MeowRuntimed.dll!Meow::RenderSystem::RenderSystem() Line 403    C++
[External Code] 
MeowRuntimed.dll!Meow::MeowEngine::Init() Line 20   C++
MeowEditor.exe!main() Line 8    C++
[External Code] 

异常详情:

Exception thrown at 0x00007FFB050AECBB (ntdll.dll) in MeowEditor.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

异常代码:

_NODISCARD _Ret_notnull_ _Post_writable_byte_size_(size) _VCRT_ALLOCATOR
_CRT_SECURITYCRITICAL_ATTRIBUTE
void* __CRTDECL operator new(size_t const size)
{
    for (;;)
    {
        if (void* const block = malloc(size))  // my exception happens here
        {
            return block;
        }

        if (_callnewh(size) == 0)
        {
            if (size == SIZE_MAX)
            {
                __scrt_throw_std_bad_array_new_length();
            }
            else
            {
                __scrt_throw_std_bad_alloc();
            }
        }

        // The new handler was successful; try to allocate again...
    }
}

异常抛出时的局部变量:

block   0x0000000000000010  void * const
malloc  0x00007ffa5ae6730f {MeowRuntimed.dll!malloc}    void *
size    16  const unsigned __int64

我的尝试

我给出了一个简化的代码,但是我发现代码没有错误。所以我认为一定是其他地方有错误,但我不知道。

c++ stl queue vulkan access-violation
1个回答
0
投票

放置一晚后,我重新编译它,然后没有出现任何错误。这很奇怪。

© www.soinside.com 2019 - 2024. All rights reserved.