Reputation: 2184
Every 50th or so time I got access violation in my DLL implementing UI, mostly run time is just fine, I suspect this could be due to usage of static vector:
Here is code snapshot from class methods with stack trace:
BaseWindow.hpp
#define UI_API __declspec(dllexport)
class UI_API BaseWindow
: public Object // base class for ref counting
{
// the rest of the code...
protected:
/** Register window class */
[[nodiscard]] virtual bool RegisterCls(const WNDCLASSEX& wnd_class) const;
/** fill in window class info struct */
[[nodiscard]] virtual bool GetClsInfo(const PCTSTR& class_name, WNDCLASSEX& wnd_class) const;
// the rest of the code...
};
BaseWindow.cpp
bool BaseWindow::GetClsInfo(const PCTSTR& class_name, WNDCLASSEX& wnd_class) const
{
if (mhInstance) // handle to HINSTANCE
{
if (GetClassInfoEx(mhInstance, class_name, &wnd_class))
return true;
else return false; // class does not exist, not an error
}
else // error handling
{
ShowError(Exception(GenericErrorCode::InvalidHandle, TEXT("Hinstance should not be nullptr")), ERR_BOILER);
return false;
}
}
bool BaseWindow::RegisterCls(const WNDCLASSEX& wnd_class) const
{
WNDCLASSEX wcex{};
// If the function does not find a matching class and successfully copy the data,
// the return value is zero.
if (!GetClsInfo(wnd_class.lpszClassName, wcex)) // calls above function!
{
// If the function fails, the return value is zero.
const ATOM atom = RegisterClassEx(&wnd_class);
if (!atom) // error handling
{
ShowError(ERR_BOILER);
return false;
}
else
{
ClassAtoms::AddClassAtom(atom); // call below function!
}
}
return true;
}
ClassAtoms.hpp This is where problematic static vector is declared/defined
#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
SUPPRESS(26426); // Global initializer calls a non-constexpr function
std::vector<ATOM> ClassAtoms::mAtoms { };
And here is relevant stack trace:
Exception thrown at 0x00007FFA691212DE (vcruntime140d.dll) in TestUI.exe: 0xC0000005: Access violation reading location 0x000001A35C589000.
vcruntime140d.dll!memcpy_repmovs() Line 114 Unknown
UI.dll!std::_Copy_memmove(unsigned short * _First, unsigned short * _Last, unsigned short * _Dest) Line 1745 C++
UI.dll!std::_Uninitialized_move>(unsigned short * const _First, unsigned short * const _Last, unsigned short * _Dest, std::allocator & _Al) Line 1738 C++
UI.dll!std::vector>::_Emplace_reallocate(unsigned short * const _Whereptr, const unsigned short & <_Val_0>) Line 707 C++
UI.dll!std::vector>::emplace_back(const unsigned short & <_Val_0>) Line 659 C++
UI.dll!wsl::ui::BaseWindow::RegisterCls(const tagWNDCLASSEXW & wnd_class) Line 131 C++
UI.dll!wsl::ui::MainWindow::Initialize(HINSTANCE__ * hInstance, int x, int y, int width, int height, HWND__ * hParent, unsigned long dwStyle, unsigned long dwExStyle, HICON__ * hIcon, HMENU__ * hMenu) Line 68 C++
TestUI.exe!TestMainWindow(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, wchar_t * lpCmdLine, int nCmdShow) Line 45 C++
[External Code]
Do you see any problem with this code, is my vector initialized properly if so then why push_back fails?
Upvotes: 0
Views: 1254
Reputation: 10962
You could just use a meyer's singleton:
instead of:
static std::vector<ATOM> mAtoms;
make a function:
static auto& atoms() {
static std::vector<ATOM> s;
return s;
}
Now the vector is initialized on first use. This also affects static destruction order - which may or may not be an issue - but you should be aware of it.
Alternatively you could try with an inline initialization - this can likely move the init. up in init. order.
static inline std::vector<ATOM> mAtoms;
and remove the .cpp init.
That being said, it is very likely that it is not that vector causing the heap corruption.
You need to debug heap corruptions.
On windows a good start is _CrtSetDbgFlag
Upvotes: 1