See the question and my original answer on StackOverflow

You pFactory reference is null. But you do initialize it, so why is it null? Because your code is broken, it does this:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    ... some code
    case WM_SIZE:
        {
           ... some code
        }
        
    // there is no break here!
    // first call to WM_SIZE falls down to WM_DESTROY which destroys your pFactory...
    
    case WM_DESTROY:
        DiscardGraphicsResources();
        SafeRelease(&pFactory);
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;

Just add a break before WM_DESTROY.