Reputation: 3352
I am using C++(11) w/ Visual Studio 2012.
I create windows using a custom made wrapper class.
CUIWindow* winA = new CUIWindow ( NULL, TEXT("winAClassName"), TEXT("winACaption"), 200, 300 );
Each window has a series of "sockets" for pluggabe events.
public:
LPFNCUIWINDOWONCLOSE OnClose;
LPFNCUIWINDOWONDESTROY OnDestroy;
LPFNCUIWINDOWONNOTIFY OnNotify;
LPFNCUIWINDOWONSIZE OnSize;
LPFNCUIWINDOWONHOTKEY OnHotkey;
I use the following macro for calling the different sockets that can be assigned to my window class in the message loop:
#define MapEvent(e, fn) \
{ \
case e: \
if ( fn != nullptr ) \
return static_cast<LPARAM>( HANDLE_##e((hWnd), (wParam), (lParam), fn) ); \
}
I have a situation as below;
You can assume pWindow is a valid pointer to a CUIWindow object.
At the shown breakpoint some of the uninitialized OnXXXX events are defined as 0xCDCDCDCD and are getting called when their message arrives (regardless of me never actually explicitly setting them after class creation). This gives me an 0x0BADFOOD exception because the function pointer is bad. I would have assumed that null function pointers would have been caught by if ( fn != nullptr )
however now I am not so sure and I'm requesting help to;
Upvotes: 0
Views: 783
Reputation:
Uninitialized pointers don't generally get set to a null pointer automatically. Is changing your class slightly an option? If so, you can set all of them without naming all of them.
struct CUIWindowEvents
{
LPFNCUIWINDOWONCLOSE OnClose;
LPFNCUIWINDOWONDESTROY OnDestroy;
LPFNCUIWINDOWONNOTIFY OnNotify;
LPFNCUIWINDOWONSIZE OnSize;
LPFNCUIWINDOWONHOTKEY OnHotkey;
}
class CUIWindow
{
public:
CUIWindowEvents Events; // move all events to a simple struct
CUIWindow() // and in your constructor
: Events() // initialise Events; this sets all the pointers to null
{ ... }
};
Upvotes: 1
Reputation: 340208
In the constructor for the CUIWindow
class you need to explicitly set those members to nullptr
(or NULL
or 0
); since all those pointers are raw pointers, they won't automatically get set to 0
in the CUIWindow
constructor:
CUIWindow ( /* whatever parameters... */)
: OnClose(nullptr)
, OnDestroy(nullptr)
, OnNotify(nullptr)
, OnSize(nullptr)
, OnHotkey(nullptr)
{
// the constructor logic...
}
Upvotes: 2