[英]vector::reserve ran into read access violation when working with Vulkan API
C++20
代码必须是 C++20,因为我用 C++20 编写了一些模板,没有C++20
,没有编译 Comp。 使用 VS 2022 在 Debug conf 中发生。 在发布 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
弹出窗口 window 显示:
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
function 应该无关紧要。 然而,即使我调整了我的代码,我还是遇到了这个问题:只是让this->imgs[*]
的构造函数什么也不做。
this->imgs
在保留 btw 之前是空的。 不知道怎么会这样。 我记得很久以前我用 Vulkan 写过这样的代码 API 它遇到了同样的问题。
我知道我可以完全删除储备,一切都会正常进行。 但我想知道为什么这段代码与 Vulkan API 不兼容,并且通常与交换链相关。 我无法在std::vector<int>;
// LMAO 我发短信 ';' 而不是'.',不错!
我发现了问题。 这是因为我事先调用了析构函数。 如果我不调用 reserve,向量可以在销毁后直接重用没有问题。 感谢 PaulMcKenzie 的评论。 我将修改我的代码以防止在使用 vector::~vector 之前调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.