mrbuk2001
asked on
Unhandled exception when adding CDateTimeCtrl
Hello,
I am new to C++ programing and ran into a problem building a dialog based MFC app. My app worked find until I tried to add a Date Time Picker and a CDateTimeCtrl variable to my dialog via the GUI.
In my biologstatDlg.h:
-------------------------- ---------
CDateTimeCtrl m_StartDateCtrl;
In my CbiologstatsDlg::OnInitDia log():
-------------------------- ---------- ---------- ------
CTime m_StartDate = CTime::GetCurrentTime();
m_StartDateCtrl.SetTime(&m _StartDate );
It compiles fine, but nothing starts. When I run it in debug, I am able to step through the dialog contructor successfully. The program bombs out with an unhadled exception when the CbiologstatApp::InitInstan ce() tries create a reference to the newly built dialog.
CbiologstatsDlg dlg;
m_pMainWnd = &dlg; <---- Unhandled exception occurs here
What am I doing wrong? Does it have something to do with trying to create a reference of the dialog when the CDateTimeCtrl in the dialog does not yet have a reference to the CTime object?
I am new to C++ programing and ran into a problem building a dialog based MFC app. My app worked find until I tried to add a Date Time Picker and a CDateTimeCtrl variable to my dialog via the GUI.
In my biologstatDlg.h:
--------------------------
CDateTimeCtrl m_StartDateCtrl;
In my CbiologstatsDlg::OnInitDia
--------------------------
CTime m_StartDate = CTime::GetCurrentTime();
m_StartDateCtrl.SetTime(&m
It compiles fine, but nothing starts. When I run it in debug, I am able to step through the dialog contructor successfully. The program bombs out with an unhadled exception when the CbiologstatApp::InitInstan
CbiologstatsDlg dlg;
m_pMainWnd = &dlg; <---- Unhandled exception occurs here
What am I doing wrong? Does it have something to do with trying to create a reference of the dialog when the CDateTimeCtrl in the dialog does not yet have a reference to the CTime object?
ASKER
Alex, I have the control object defined in the h file as shown. And the DDX_Control is pointing to the correct IDC. I didn't modify the computer generated code provided when I created the date time picker.
On further analysis, I must have done some bad programming practice someplace in my dlg app, because I am able to build a new dlg app with just a date time picker that works. Even though my problem program works without the date time picker, I bet what ever resources the date time picker relies upon is running into a conflict with the way I did something else.
Time to dot my 'i's and cross my 't's, or it's back to the drawing board.
Post exact exception information: stack and code fragment where exception is rased.
ASKER
When I step through the program, the exception occurs in CbiologstatApp::InitInstan ce() at the following line:
m_pMainWnd = &dlg;
There is no stack data. I get a pop up box with the following:
Unhandled exception at 0x00000001 in biologstats.exe: 0xC0000005: Access violation reading location 0x00000001.
The exception allows me to break or continue. Continue ends the program and Break asks if I want to see the Assembly. If I show assembly, I see all the memory, but all the values show "???".
Maybe something is wrong with the project since I've been zipping it up and moving it around to different PCs. I've decided to build a new project re-create the dialog, and copy the main processing code from the old project. I'll let you know how it goes.
Mike
m_pMainWnd = &dlg;
There is no stack data. I get a pop up box with the following:
Unhandled exception at 0x00000001 in biologstats.exe: 0xC0000005: Access violation reading location 0x00000001.
The exception allows me to break or continue. Continue ends the program and Break asks if I want to see the Assembly. If I show assembly, I see all the memory, but all the values show "???".
Maybe something is wrong with the project since I've been zipping it up and moving it around to different PCs. I've decided to build a new project re-create the dialog, and copy the main processing code from the old project. I'll let you know how it goes.
Mike
Post InitiInstance code.
Try the following: Put breakpoint on the line m_pMainWnd = &dlg line and stop in debugger there. Type in the Watch window "this" and see what value is shown. Expand "this" in the Watch window and see the values of the application class.
Try the following: Put breakpoint on the line m_pMainWnd = &dlg line and stop in debugger there. Type in the Watch window "this" and see what value is shown. Expand "this" in the Watch window and see the values of the application class.
mrbuk2001,
Have you initialized the common controls properly?
I think you have to tell InitCommonControlsEx that you are going to use that class ( Date-Time class ).
ICC_DATE_CLASSES flag in INITCOMMONCONTROLSEX struct Load date and time picker control class .. init this struct and pass to InitCommonControlsEx() function.
try this on init :
INITCOMMONCONTROLSEX iccex = { sizeof(INITCOMMONCONTROLSE X), ICC_DATE_CLASSES };
// Init the common controls.
InitCommonControlsEx ( &iccex );
-MAHESH
Have you initialized the common controls properly?
I think you have to tell InitCommonControlsEx that you are going to use that class ( Date-Time class ).
ICC_DATE_CLASSES flag in INITCOMMONCONTROLSEX struct Load date and time picker control class .. init this struct and pass to InitCommonControlsEx() function.
try this on init :
INITCOMMONCONTROLSEX iccex = { sizeof(INITCOMMONCONTROLSE
// Init the common controls.
InitCommonControlsEx ( &iccex );
-MAHESH
Put above code in InitInstance() function of your application.
-MAHESH
-MAHESH
Creating a new project (dialog based, using Visual Studio 2005) I get the following code
// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&Init Ctrls);
CWinApp::InitInstance();
AfxEnableControlContainer( );
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need
// Change the registry key under which our settings are stored
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
CzzzDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&Init
CWinApp::InitInstance();
AfxEnableControlContainer(
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need
// Change the registry key under which our settings are stored
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
CzzzDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
I think i have suggested same to put code like this on InitInstance() but with ICC_DATE_CLASSES classes
As per documentation of InitCommonControlsEx() ICC_DATE_CLASSES Load date and time picker control class and ICC_WIN95_CLASSES Load animate control, header, hot key, list view, progress bar, status bar, tab, tooltip, toolbar, trackbar, tree view, and up-down control classes.
InitInstance()
{
......
INITCOMMONCONTROLSEX iccex = { sizeof(INITCOMMONCONTROLSE X), ICC_DATE_CLASSES };
// Init the common controls.
InitCommonControlsEx ( &iccex );
....
}
-MAHESH
As per documentation of InitCommonControlsEx() ICC_DATE_CLASSES Load date and time picker control class and ICC_WIN95_CLASSES Load animate control, header, hot key, list view, progress bar, status bar, tab, tooltip, toolbar, trackbar, tree view, and up-down control classes.
InitInstance()
{
......
INITCOMMONCONTROLSEX iccex = { sizeof(INITCOMMONCONTROLSE
// Init the common controls.
InitCommonControlsEx ( &iccex );
....
}
-MAHESH
Similar - I put it in because of the comments - only applies for XP and visual styles via manifest. It also shows a different way of initialising/setting what one requires.
>> m_pMainWnd = &dlg; <---- Unhandled exception occurs here
It is very unlikely that the error is occuring right there. That is a simple assignment statement. The error must be in the line above. That line invokes the constructor of your CDialog-derived object. Place a breakpoint in that code.
That said, I have seen several strange behaviors in the CDateTimeCtrl object. One relates to using the multithreaded libraries. Sometimes the error goes away when you use make the Release build (for some reason).
Here's an example where it was hard to identify the actual source, indicating a possible bug in MFC:
https://www.experts-exchange.com/questions/21229912/COleDateTime-Crashing-on-some-computers.html
Another avenue to explore: In the dialog editor, try changing the properties (such as "Use Spin Control" and "Show None") to see if the problem persists.
Finally, it looks like you are trying to set the time using a CTime value. Try using COelDateTImeinstead. That worked here:
https://www.experts-exchange.com/questions/20153408/CDateTimeCtrl-Problem.html#6306498
-- Dan
It is very unlikely that the error is occuring right there. That is a simple assignment statement. The error must be in the line above. That line invokes the constructor of your CDialog-derived object. Place a breakpoint in that code.
That said, I have seen several strange behaviors in the CDateTimeCtrl object. One relates to using the multithreaded libraries. Sometimes the error goes away when you use make the Release build (for some reason).
Here's an example where it was hard to identify the actual source, indicating a possible bug in MFC:
https://www.experts-exchange.com/questions/21229912/COleDateTime-Crashing-on-some-computers.html
Another avenue to explore: In the dialog editor, try changing the properties (such as "Use Spin Control" and "Show None") to see if the problem persists.
Finally, it looks like you are trying to set the time using a CTime value. Try using COelDateTImeinstead. That worked here:
https://www.experts-exchange.com/questions/20153408/CDateTimeCtrl-Problem.html#6306498
-- Dan
ASKER
I solved the issue. I used a win2003 environment to create the project, but unzip the project on my home system running XP. The problem is that I forgot to delete the Debug folder from the win2003 enviroment. WinXP doesn't like win2003 DLLs. After deleting the debug folder and rebuilding all works fine.
Sorry for the fire drill. I appreciate the ideas though.
Mike
Sorry for the fire drill. I appreciate the ideas though.
Mike
Great! I'm glad it's fixed. Now you should follow the steps here to close this question so the cleanup crew won't need to process this question: https://www.experts-exchange.com/help.jsp#hi70
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
CDateTimeCtrl m_StartDateCtrl; // h-file
DDX_Control(pDX, IDC_DATETIMEPICKER1, m_StartDateCtrl); // in DoDataExchange function, replace IDC_DATETIMEPICKER1 with actual resource ID
Usually control variables are added using MFC Wizard, it adds all required code. You can do this manually or remove existing variable and add it again using Wizard.