BUG: Access Violation in RFX_Date If CTime Not Initialized

Last reviewed: June 26, 1997
Article ID: Q155721
The information in this article applies to:
  • The Microsoft Foundation Classes (MFC) included with: - Microsoft Visual C++, 32-bit Edition, versions 4.2, 5.0

SYMPTOMS

An 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.

CAUSE

The 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.

RESOLUTION

Initialize 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();

STATUS

Microsoft 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 INFORMATION

Here 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
Keywords : MfcDatabase vcbuglist420 vcbuglist500 kbbuglist
Technology : kbMfc
Version : 4.2 5.0
Platform : NT Windows
Issue type : kbbug


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 26, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.