I have an Observer class and a Subscriber class.  

For testing purposes, the observer creates a thread that generates fake messages and calls `CServerCommandObserver::NotifySubscribers()`, which looks like this:

```
void CServerCommandObserver::NotifySubscribers(const Command cmd, void const * const pData)
{
    // Executed in worker thread //

    for (Subscribers::const_iterator it = m_subscribers.begin(); it != m_subscribers.end(); ++it)
    {
        const CServerCommandSubscriber * pSubscriber = *it;

        const HWND hWnd = pSubscriber->GetWindowHandle();
        if (!IsWindow(hWnd)) { ASSERT(FALSE); continue; }

        SendMessage(hWnd, WM_SERVERCOMMAND, cmd, reinterpret_cast<LPARAM>(pData));
    }
}

```

The subscriber is a `CDialog` derived class, that also inherits from `CServerCommandSubscriber`.

In the derived class, I added a message map entry, that routes server commands to the subscriber class handler.

```
// Derived dialog class .cpp
ON_REGISTERED_MESSAGE(CServerCommandObserver::WM_SERVERCOMMAND, HandleServerCommand)

// Subscriber base class .cpp
void CServerCommandSubscriber::HandleServerCommand(const WPARAM wParam, const LPARAM lParam)
{
    const Command cmd = static_cast<Command>(wParam);

    switch (cmd)
    {
    case something:
        OnSomething(SomethingData(lParam)); // Virtual method call
        break;
    case // ...
    };
}

```

The problem is, that I see strange crashes in the HandleServerCommand() method:

It looks something like this:

> 
> Debug Error!
> 
> 
> Program: c:\myprogram.exe  
> 
>  Module:  
> 
>  File: i386\chkesp.c  
> 
>  Line: 42
> 
> 
> The value of ESP was not properly
>  saved across a function call. This is
>  usually the result of calling a
>  function declared with one calling
>  convention with a function pointer
>  declared with a different calling
>  convention.
> 
> 
> 

I checked the function pointer that AfxBeginThread() wants to have:

```
typedef UINT (AFX_CDECL *AFX_THREADPROC)(LPVOID); // AFXWIN.H

static UINT AFX_CDECL MessageGeneratorThread(LPVOID pParam); // My thread function

```

To me, this looks compatible, isn't it?

I don't know, what else I have to look for. Any ideas?

I made another strange observation, that might be related:
In the `NotifySubscribers`method, I call `IsWindow()` to check if the window to which the handle points, exists. Apparently it does. But calling `CWnd::FromHandlePermanent()` returns a NULL pointer.