Stack Exchange
C++, delegates, application crashes, unknown reason
I have a weird problem with my code as the compiler generates code which crashes my testing application. 
I am using Visual C++ 2010 compiler. 
The code is:
template < typename TDstType,
           typename TSrcType >
TDstType unsafe_cast( TSrcType anySrc )
{
    return ( TDstType ) anySrc;
}

template < typename TDstType,
           typename TSrcType >
TDstType brutal_cast( TSrcType anySrc )
{
    return *( TDstType* ) &anySrc;
}

template < typename TParamType >
class EventHandler
{

public:

    template < typename TObjectType >
    EventHandler( TObjectType&  refObject, 
           void ( TObjectType::*pfnMethod )( TParamType ) );

    void operator()( TParamType anyParam );

private:

    void* m_ptrMethod;
    void* m_ptrObject;

};

template < typename TParamType  >
template < typename TObjectType >
inline EventHandler< TParamType >::EventHandler( TObjectType& refObject, void ( TObjectType::*pfnMethod )( TParamType ) )
: m_ptrMethod( brutal_cast< void* >( pfnMethod ) ),
  m_ptrObject( &refObject )
{
}

template < typename TParamType >
inline void EventHandler< TParamType >::operator()( TParamType anyParam )
{
                    class Class;
    ( unsafe_cast<        Class  *                 >( m_ptrObject )->*
      brutal_cast< void ( Class::* )( TParamType ) >( m_ptrMethod ) )( anyParam );
}

And the testing application's code:
class SomeClass
{

public:

    void Method( int intParam )
    {
        printf( "%d\n", intParam );
    }

};

int main( int intArgc, char* arrArgv[] )
{
    EventHandler< int > varEventHandler( *new SomeClass(), &SomeClass::Method );

    varEventHandler( 10 );

    return 0;
}

The compiled application crashes as it have tried to read from an invalid memory location. I checked in the Visual Studio's debugger every variable going though and none contains invalid address.
I hope anyone can help me fix this as I failed. Maybe a coffee overdose is the reason though...

I'll be honest, I'd rather expect a program containing brutal_cast and unsafe_cast not to work very well.

What's the code in your EventHandler::operator()?

This cannot work; you can't cast pointer-to-member to void*.  Doing so loses information and the program, unsurprisingly, crashes.
