BUG: Access Violation in RFX_Date If CTime Not InitializedLast reviewed: June 26, 1997Article ID: Q155721 |
The information in this article applies to:
SYMPTOMSAn application may fail with an access violation while executing the RFX_Date() function. A message similar to the following appears:
Unhandled exception in My.exe (MFC42D.DLL): 0xC0000005: Access Violation. CAUSEThe RFX_Date() function in MFC 4.2 now requires initialization of CTime objects. Versions of MFC earlier than 4.2 do not have this requirement. AppWizard and ClassWizard do not initialize the CTime member variables for you. Because CTime member variables are not initialized in the CRecordset constructor, an access violation can occur when RFX_Date() tries to use the uninitialized data.
RESOLUTIONInitialize the CTime member variables in the constructor of your CRecordset- derived class. The following is one way to initialize the CTime member variable:
m_myTime = CTime::GetCurrentTime(); STATUSMicrosoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.
MORE INFORMATIONHere is one common scenario where you may see an access violation: Call CRecordset::AddNew() on an empty recordset, set the value of the CTime member variable, and then call CRecordset::Update(). The insert to the database works; however, an access violation may occur before the call to Update() returns. The following steps show how the access violation occurs: After the insert to the database is complete, Update() attempts to reload the data for the previous record using the CRecordset::LoadFields() function. LoadFields() calls CRecordset::DoFieldExchange(), which calls the RFX_Date() function for the CTime field. The CFieldExchange::LoadField case within RFX_Date() calls CTime::GetYear():
void AFXAPI RFX_Date(CFieldExchange* pFX, LPCTSTR szName, CTime& value){ ... switch (pFX->m_nOperation) { .... case CFieldExchange::LoadField: { ... pts->year = (SWORD)value.GetYear(); ... } ...} The definition for GetYear() in AFX.INL dereferences the pointer returned from GetLocalTm(NULL): _AFX_INLINE int CTime::GetYear() const { return (GetLocalTm(NULL)->tm_year) + 1900; }GetLocalTm() returns the value from localtime(), which is a NULL pointer if the CTime value is a negative number. Because CTime has not been initialized, it may have a negative value. When GetYear() attempts to dereference this NULL pointer, the access violation occurs in AFX.INL at line 265.
|
Additional query words: crash gpf exception
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |