我在Visual Studio 2013,Win7 32bit上使用C ++ / MFC应用程序。我有一个带有对话框的主应用程序,我在其中放置了一个DateTimePicker MFC对象。对于初始化,我使用了DDX_DateTimeCtrl
和m_odtFilterData
成员变量。
void CClassDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_DateTimeCtrl(pDX, IDC_DATE, m_odtFilterData);
}
BOOL CClassDlg::OnInitDialog()
{
COleDateTime odtCurrDate = COleDateTime::GetCurrentTime();
m_odtFilterData.SetDate(odtCurrDate.GetYear(), 1, 1);
CDialog::OnInitDialog();
//etc etc
UpdateData(FALSE);
return TRUE;
}
我添加了日期时间更改事件
BEGIN_MESSAGE_MAP(CClassDlg, CDialog)
ON_NOTIFY(DTN_DATETIMECHANGE, IDC_DATE, &CClassDlg::OnDtnDatetimechange)
END_MESSAGE_MAP()
和
void CClassDlg::OnDtnDatetimechange(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
if(m_odtExampleODTVarThatIHave > m_odtFilterData)
{
//do stuff
}
*pResult = 0;
}
问题是在对话框中我选择datetimepicker的日期,有时,应用程序崩溃时出现此错误:
Windows has triggered a breakpoint in AppName.exe.
This may be due to a corruption of the heap, which indicates a bug in AppName.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while AppName.exe has focus.
The output window may have more diagnostic information.
输出只说:
HEAP[AppName.exe]: HEAP: Free Heap block 279850 modified at 279e1c after it was freed
断点停留在mfc \ appmodul.cpp:
extern "C" int WINAPI
_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine, int nCmdShow)
#pragma warning(suppress: 4985)
{
// call shared/exported WinMain
/*->*/return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}
这很奇怪,只有少数点击显示月份小部件的datetimepicker按钮。如果我使用旋转控件代替小部件,那么一切都很完美......你有什么想法吗? The button that open the month widget
另一件事,如果我删除
if(m_odtExampleODTVarThatIHave > m_odtFilterData)
一切正常。似乎由于某种原因, m_odtFilterData 变量文件被月份小部件损坏。我不明白我的错误在哪里。
答案 0 :(得分:0)
我看到的一个明显问题是您忘记在CDialog::DoDataExchange(pDX);
中拨打CClassDlg::DoDataExchange(CDataExchange* pDX)
所以它看起来应该是这样的:
void CClassDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_DateTimeCtrl(pDX, IDC_DATE, m_odtFilterData);
}
请同时检查公共控件是否已在OnInitInstance
派生类的CWinApp
中正确初始化:
INITCOMMONCONTROLSEX iccex = { sizeof(INITCOMMONCONTROLSEX), ICC_DATE_CLASSES | ICC_STANDARD_CLASSES };
// Init the common controls.
InitCommonControlsEx ( &iccex );
请确保您有正确的清单:
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
另请查看您的_WIN32_WINNT
定义。