0

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;

Code with breakpoint

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;

  1. Explain why this is occurring
  2. Find the best way to stop this from occurring, preferably without explicitly setting all the function pointers to zero in the constructor since sometimes there are many, many, sockets.
kvanbere
  • 3,289
  • 3
  • 27
  • 52

2 Answers2

2

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...
}
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • Thanks, this solved my problem. The other solution is alot neater though, but initializing all my events in my constructor like this stopped the errors. – kvanbere Dec 12 '12 at 07:26
1

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
    { ... }
};
  • was this not happening before because of a user defined ctor? – Karthik T Dec 12 '12 at 07:38
  • @KarthikT Yes, the question has `new CUIWindow ( NULL, TEXT("winAClassName"), TEXT("winACaption"), 200, 300 )` in it, that requires a user-defined constructor. :) –  Dec 12 '12 at 08:15