[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 773
  • Last Modified:

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::OnInitDialog():
----------------------------------------------------
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::InitInstance() 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?
0
mrbuk2001
Asked:
mrbuk2001
  • 3
  • 3
  • 3
  • +3
1 Solution
 
AlexFMCommented:
I don't see how m_StartDateCtrl variable is linked to dialog control. You must have the following code:

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.
0
 
mrbuk2001Author Commented:

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.
0
 
AlexFMCommented:
Post exact exception information: stack and code fragment where exception is rased.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mrbuk2001Author Commented:
When I step through the program, the exception occurs in CbiologstatApp::InitInstance() 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
0
 
AlexFMCommented:
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.
0
 
mahesh1402Commented:
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(INITCOMMONCONTROLSEX), ICC_DATE_CLASSES };

    // Init the common controls.
    InitCommonControlsEx ( &iccex );


-MAHESH


0
 
mahesh1402Commented:
Put above code in InitInstance() function of your application.

-MAHESH
0
 
AndyAinscowCommented:
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(&InitCtrls);

      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();
0
 
mahesh1402Commented:
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(INITCOMMONCONTROLSEX), ICC_DATE_CLASSES };

    // Init the common controls.
    InitCommonControlsEx ( &iccex );

....

}

-MAHESH
0
 
AndyAinscowCommented:
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.
0
 
DanRollinsCommented:
>> 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:
   http://www.experts-exchange.com/Programming/Programming_Languages/MFC/Q_21229912.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:
     http://www.experts-exchange.com/Programming/Programming_Languages/MFC/Q_20153408.html#6306498

-- Dan

0
 
mrbuk2001Author Commented:
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
0
 
DanRollinsCommented:
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:  http://www.experts-exchange.com/help.jsp#hi70
0
 
NetminderCommented:
Closed, 125 points refunded.
Netminder
Site Admin
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now