Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用静态std::vector类成员时发生访问冲突_C++_Static_Containers_Access Violation - Fatal编程技术网

C++ 使用静态std::vector类成员时发生访问冲突

C++ 使用静态std::vector类成员时发生访问冲突,c++,static,containers,access-violation,C++,Static,Containers,Access Violation,每50次左右,我在我的DLL实现UI中都会遇到访问冲突,大多数情况下运行时都很好,我怀疑这可能是由于使用了静态向量: 下面是具有堆栈跟踪的类方法的代码快照: BaseWindow.hpp BaseWindow.cpp ClassAtoms.hpp 这就是声明/定义有问题的静态向量的地方 #define SUPPRESS(...) __pragma(warning(suppress : __VA_ARGS__)) class UI_API ClassAtoms { // the

每50次左右,我在我的DLL实现UI中都会遇到访问冲突,大多数情况下运行时都很好,我怀疑这可能是由于使用了静态向量:

下面是具有堆栈跟踪的类方法的代码快照:

BaseWindow.hpp

BaseWindow.cpp

ClassAtoms.hpp 这就是声明/定义有问题的静态向量的地方

#define SUPPRESS(...) __pragma(warning(suppress : __VA_ARGS__))

class UI_API ClassAtoms
{   
     // the rest of the code...

public:
    /** Add registered window class to ATOM container */
    inline static void AddClassAtom(const ATOM& atom);

    // the rest of the class

private:
    /** Container for registered window classes */
    SUPPRESS(4251);  // needs to have dll-interface (inlining will result in internal compiler error)
    static std::vector<ATOM> mAtoms;

      // the rest of the code...
};

void ClassAtoms::AddClassAtom(const ATOM& atom)
{
    mAtoms.push_back(atom);
}
ClassAtoms.cpp

下面是相关的堆栈跟踪:

在中的0x00007FFA691212DE vcruntime140d.dll处引发异常 TestUI.exe:0xC0000005:访问冲突读取位置 0x000001A35C589000

vcruntime140d.dll!memcpy_repmovs 114号线未知

UI.dll!std::_Copy_memmoveunsigned short*_First,unsigned short*_Last,unsigned short*_dest1745 C行++

UI.dll!std::_Uninitialized_move>unsigned short*const\u First,unsigned short*const\u Last,unsigned short*Dest, std::分配器和所有行1738 C++

UI.dll!std::vector>::_Emplace_reallocateunsigned short* 常数,其中PTR、常数无符号短线和线路707 C++

UI.dll!std::vector>::布设_backconst无符号短& 第659 C行++

UI.dll!wsl::ui::BaseWindow::RegisterClsconst标记WNDCLASSEXW& wnd_类线路131 C++

UI.dll!wsl::ui::MainWindow::InitializeInstance_*hInstance,int x、 整数y、整数宽度、整数高度、整数y、整数y、整数y、整数y、整数宽度、整数高度、整数y、整数y、整数y、整数y、整数y、整数y、整数y、整数y、整数y、整数y、整数y、整数y、整数y、整数y、整数y、整数y dwStyle,无符号长dwExStyle,HICON_*HICON,HUMENU_*HUMENU 第68行C++

TestUI.exe!TestMainWindowHINSTANCE\u*hInstance,hInstance\u* HPREVISTANCE,wchar\U t*lpCmdLine,int nCmdShow第45行C++

[外部代码]


你看到这段代码有什么问题吗?我的向量初始化正确吗?如果是,那么为什么push_-back失败了?

你可以使用meyer的单例:

而不是:

static std::vector<ATOM> mAtoms;
制作一个函数:

static auto& atoms() {
  static std::vector<ATOM> s;
  return s;
}
现在向量在第一次使用时初始化。这也会影响静态销毁顺序-这可能是一个问题,也可能不是问题-但您应该意识到这一点

或者,您可以尝试内联初始化-这可能会移动init。在init上。秩序

 static inline std::vector<ATOM> mAtoms;
并删除.cpp init

也就是说,很可能不是向量导致堆损坏

您需要调试堆损坏。
在windows上,一个好的开始是

ATOM是否有点像void*?根据msdn:typedef WORD ATOM typedef unsigned short WORD是的,谢谢,我在msdn文档中找到了它。ATOM的值在0x0001到0xBFFF之间。我怀疑数据损坏,可能是在故障点之前。您是否打开了警告?你有没有用调试器检查过代码?我不是建议用它来解决问题。引用值类型是愚蠢的。这只是一个直截了当的错误:似乎与此类似,但它来自2018年:谢谢您的建议!我会实现这个,若问题在接下来的几个小时内或者直到明天不会再次发生,我会将此标记为一个答案+1现在@metablaster查看我的更新-它可能根本不是导致损坏的向量…谢谢,我将使用已经阅读msdn文档的_CRTSETDBGSFLAG!我想报告,这似乎是一个初学者的错误!坏消息是,您的解决方案不起作用,但增加了崩溃的频率。好消息是,这有助于我更快地查明问题,并且与CrtDebug函数集一起,我确定这是由于两个未同步的线程同时访问向量。我现在不使用线程,这就是为什么我自己没有弄明白,这两个线程只在测试项目中设置,因为只有两个线程,所以崩溃非常罕见。多谢各位!我学会了如何使用CRT调试fn进行调试。
static std::vector<ATOM> mAtoms;
static auto& atoms() {
  static std::vector<ATOM> s;
  return s;
}
 static inline std::vector<ATOM> mAtoms;