I've made a message-only window class, and I'm trying to map HWNDs back to the objects with those handles. I'm trying to do that using a private static std::map<HWND, CMyClass*>
belonging to the class, like this:
MyClass.h:
class CMyClass
{
...
private:
HWND m_hWnd;
HINSTANCE m_hInstance;
LPCSTR m_szClassName;
static std::map<HWND, CMyClass*> s_mapHandles;
...
};
MyClass.cpp:
std::map<HWND, CMyClass*> CMyClass::s_mapHandles;
but when I try to add to the map, the program crashes. I've tried three different forms, and they all give the same error:
...
m_hWnd = ::CreateWindowEx(0, m_szClassName, "Message Window", 0, 0, 0, 0, 0, HWND_MESSAGE, 0, m_hInstance, 0);
s_mapHandles.insert(pair<HWND, CMyClass*>(m_hWnd, this));
or
...
s_mapHandles.insert(s_mapHandles.end(), pair<HWND, CMyClass*>(m_hWnd, this));
or even
...
s_mapHandles[m_hWnd] = this;
In each case, there crash occurs at a call to _Root()
which tries to return _Parent(_Myhead)
; _Parent(_Myhead)
returns (_Nodepref)(*_Myhead)._Parent
which fails because _Myhead
is null.
How do I initialise the map, such that its head is non-null and I can insert things without it crashing? Apologies if I've explained this badly - I'm new to C++.