vector::reserve在使用Vulkan API时遇到读访问冲突



C++20CODE必须是c++ 20因为我用c++ 20写过一些模板没有C++20,没有编译与VS 2022相比在调试配置中发生。不要在Release conf中发生。没有调试器,它仍然会发生。

抛出一个异常,其中:

_CONSTEXPR20 void _Orphan_range_unlocked(pointer _First, pointer _Last) const {
_Iterator_base12** _Pnext = &_Mypair._Myval2._Myproxy->_Myfirstiter;
while (*_Pnext) {
-----------^^^^^^^
const auto _Pnextptr = static_cast<const_iterator&>(**_Pnext)._Ptr;
if (_Pnextptr < _First || _Last < _Pnextptr) { // skip the iterator
const auto _Temp = *_Pnext; // TRANSITION, VSO-1269037
_Pnext           = &_Temp->_Mynextiter;
} else { // orphan the iterator
const auto _Temp = *_Pnext; // TRANSITION, VSO-1269037
_Temp->_Myproxy  = nullptr;
*_Pnext          = _Temp->_Mynextiter;
}
}
}

,

_Mypair._Myval2._Myproxy is nullptr

弹出窗口显示:

Unhandled exception thrown: read access violation.
_Pnext was 0x8.
我的代码是:
struct Swapchain 
{
struct SwapchainImage
{
VkImage img;
VkImageView view;
VkFramebuffer frame;
ImageDepthTest depth;
SwapchainImage(VkDevice device, VkImage image, const VkExtent2D& extent, VkFormat depth_format, VkSurfaceFormatKHR surface, VkRenderPass pass, const VkAllocationCallbacks* alloc = nullptr);
inline SwapchainImage(SwapchainImage&& e) noexcept : img(e.img), view(e.view), frame(e.frame), depth(std::move(e.depth)) { e.img = nullptr; e.view = nullptr; e.frame = nullptr; }
SwapchainImage(const SwapchainImage&) = delete;
~SwapchainImage() noexcept;
};
std::vector<SwapchainImage> imgs;
...
}
vkResult = BUG_CHECK(vkCreateSwapchainKHR, device, &createInfo, alloc, &this->swapchain);
this->depth_format = FindDepthFormat(phys);
uint32_t nSize = 0;
BUG_CHECK(vkGetSwapchainImagesKHR, device, this->swapchain, &nSize, nullptr);
std::vector<VkImage> vkImages(nSize);
this->imgs.reserve(nSize); // without this, everything is fine, nSize = 3 btw
BUG_CHECK(vkGetSwapchainImagesKHR, device, this->swapchain, &nSize, vkImages.data());
for (auto e : vkImages)
{
this->imgs.emplace_back(device, e, this->extent, this->depth_format, format, pass, alloc);
}

在我看来,reserve函数应该无关紧要。然而,我得到了这个问题,即使我调整了我的代码:简单地使this->imgs[*]的构造函数什么也不做

this->imgs在预留之前为空。不知道这是怎么发生的。我记得很久以前我用Vulkan API写过这样的代码,它遇到了同样的问题。

我明白我可以完全去除储备,一切都会正常工作。但是我想知道为什么这个代码与Vulkan API不兼容,并且通常与交换链相关。我不能在简单的情况下表示这一点,比如std::vector<int>;//LMAO我发的是';'而不是'。"不错!">

我找到问题了。这是因为我事先调用了析构函数。如果我不调用保留,矢量可以在销毁后直接重用,没有问题。感谢保罗·麦肯齐的评论。我将修改我的代码,以防止在使用vector::~vector之前调用它。

最新更新