I am working on a time card program in Visual C++ (FYI: I am VERY new to this, so, please, be gentle with me) :) It compiles with 0 errors and 0 warnings, but when it executes I get an error message that the program needs to close. When I tried to go into Debug, it gave me "Unhandled exception in MFCTimeCard.exe:0XC0000005: Access Violation" I click on OK and it goes to this piece of code in the AfxWinMain()

if (!pThread->InitInstance())
{
if (pThread->m_pMainWnd != NULL)
{
TRACE0("Warning: Destroying non-NULL m_pMainWnd\n");
pThread->m_pMainWnd->DestroyWindow();
}
nReturnCode = pThread->ExitInstance();
goto InitFailure;
}
nReturnCode = pThread->Run();

Please let me know if anyone needs more info. BTW: I did not touch this particular bit when working. I did however add the InitInstance() in a source file.
THANKS!!!

Dani AI

Generated

This is a classic MFC startup problem. was right to point at InitInstance and ’s question about the return value is valid — returning FALSE isn’t necessarily a bug for a dialog-based app. confirmed the root cause: the CWinApp-derived application object wasn’t instantiated. A quick checklist and examples follow so others who hit an Access Violation in AfxWinMain can fix it fast.

Make sure the application class has exactly one global instance (usually in the app .cpp). If the AppWizard didn’t add it, create it yourself, for example:

CTimeCardApp theApp;

Without that global object the MFC framework won’t initialize the app state correctly and InitInstance/ExitInstance plumbing can fail.

For a dialog-based app follow the usual InitInstance pattern: initialize containers, create the dialog, set m_pMainWnd before calling DoModal, then return FALSE to exit the app after the modal dialog closes:

BOOL CTimeCardApp::InitInstance()
{
    AfxEnableControlContainer();
    CTimeCardDlg dlg;
    m_pMainWnd = &dlg;
    INT_PTR nRes = dlg.DoModal();
    // handle nRes (IDOK/IDCANCEL) if needed
    return FALSE; // normal for modal-dialog apps
}

Debugging tips: build a Debug configuration, set a breakpoint at the top of InitInstance, step in and inspect pointers (m_pMainWnd, any dialog member pointers). Enable first‑chance exceptions so you catch the failure as it happens and check the call stack. Also verify any resource IDs, COM/OLE initializations, or control containers are initialized before use.

Following the AppWizard template (global app object + m_pMainWnd assignment + proper initialization calls) avoids this AV in AfxWinMain. Thanks to and for the diagnostic direction.

Recommended Answers

All 5 Replies

The real error will be in the InitInstance() method.

That is kinda what I figured, but I've looked it over a dozen times Here is my code:

BOOL CTimeCardApp::InitInstance() {
    CTimeCard time;
    time.DoModal();
    return FALSE;
}

Thanks!

Doesn't returning false signify failure? I dunno, I'm not an MFC guy. If that's not the problem, then time is crashing on init.

I think you're onto something.
Where is the CTimeCard initialised?
What is it anyway? If it's a reference to something else you're calling a method on an uninitialised object which will indeed cause an access violation.

Got it! I forgot to put in the a line to instantiate the application object. Thanks for your help!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.