Cannot create main window?


I've been trying to figure this out for hours now, and I'm at my wit's end. I would surely appreciate it if someone could tell me when I'm doing wrong.

After finding some C++ MFC tutorials, I tried to make a simple GUI application with C++, but it seems to have problem at generating the main window(dialog?). When I'm trying to compile the code, following message shows :


Unhandled exception at 0x00E7A9DC in GUI_Employee_0501.exe: 0xC0000005: Access violation reading location 0xFEFEFF66.


and the break point stops inside <strong>winmain.cpp</strong>, at <strong>pThread->m_pMainWnd->DestroyWindow();</strong>. The value of <strong>pThread->m_pMainWnd</strong> is <strong>NULL</strong>, which I suspect as the cause of the problem.

Can you specify what is the problem here? I have a sample code and that is very similar to mine but that works, so I truly have no idea what is happening!

#include <afxwin.h> #include "resource.h" #include <iostream> using namespace std; #pragma comment(linker, "/entry:WinMainCRTStartup /subsystem:console") CEdit *pFNAME; CEdit *pLNAME; CEdit *pSALARY; CEdit *pDDAY; CEdit *pMMONTH; CEdit *pYYEAR; CComboBox *pGENDER; CButton *pADD; CButton *pDELETE; CButton *pSAVE; CButton *pLOAD; class ENTRY_FORM : public CDialog { public: ENTRY_FORM(CWnd* pParent = NULL) : CDialog(ENTRY_FORM::IDD, pParent) { } enum { IDD = dialog_main }; protected: virtual void DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } virtual BOOL OnInitDialog() { CDialog::OnInitDialog(); SetUpInterfacePointers(); return true; } void SetUpInterfacePointers(){ ... does its own thing ... } public: afx_msg void add() { PRESS_ADD(); } void PRESS_ADD() { ... does its own thing ... } DECLARE_MESSAGE_MAP() }; class Program : public CWinApp { public: Program(){ } public: virtual BOOL InitInstance() { CWinApp::InitInstance(); cout << "CWinAPP:initInstance" << endl; ENTRY_FORM dlg; m_pMainWnd = &dlg; cout << "mpMainWnd" << endl; INT_PTR nResponse = dlg.DoModal(); return FALSE; } }; // BEGIN_MESSAGE_MAP(ENTRY_FORM, CDialog) ON_COMMAND(button_add, add) END_MESSAGE_MAP() // Program theApp;


Change in InitInstance():

return FALSE;


return TRUE;

This is because <a href="https://msdn.microsoft.com/pl-pl/library/ae6yx0z0.aspx" rel="nofollow">CWinApp::InitInstance</a> should return FALSE only in case of failure, and TRUE if all initialization went OK. In case of failure, AfxWinMain will try to destroy window pointed by m_pMainWnd, which is not possible (it causes Undefined Behaviour) because you assign to m_pMainWnd a local object (which is destroyed once InitInstance ends).


S.B Bae - to investigate it further and find a root cause, you will need to debug place where m_pMainWnd should be set to NULL once your dialog ends. This should be in CWnd::OnNcDestroy() in wincore.cpp. There is a line pThread->m_pMainWnd = NULL; which apparently is not being executed in your application.


  • MSVC 2015 /Wall has lots of not useful messages
  • XMPP push notifications causing problems (delay + duplications) in messages
  • Interpret strings as packed binary data in C++
  • C++: Use input string as variable name
  • TCL throws invalid command name when writing csv data to a matrix within a namespace
  • Memory usage and time for execution for another process using C#?
  • Raise Session_OnStart event from custom ASP.NET SessionStateProvider class
  • How to load a CascadeClassifier using Emgu c#
  • Need help to stop program terminating without users consent
  • PHP Handling Namespace with SimpleXML
  • Finding All Combinations (cartesian product) of list values in vb.net
  • How do I fix “The program issued a command but the command length is incorrect.” error when calling
  • C++ String tokenisation from 3D .obj files
  • How to add System.Windows dll to Visual Studio 2010 express?
  • C++ Coin flip program error
  • string.IsNullOrEmpty() Doesn't Seem to Work on a String within a Class within a Class
  • runtime-check whether an instance (Base*) override a parent function (Base::f())
  • In Java, how can I construct a File from a resource?
  • Building Qt project for C++11 standard
  • C++ friend class std::vector
  • WPF Template Binding in ToggleButton UserControl
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • JBoss External Properties Files in Classpath
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • Launch Runnable Jar from Web Start
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • TFS: Get latest causes slow project reloading
  • Controls, properties, events and timers running in design time
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • How do I rollback to a specific git commit
  • Buffer size for converting unsigned long to string
  • Hits per day in Google Big Query
  • How to get Windows thread pool to call class member function?
  • Checking variable from a different class in C#
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal