## FIX: Memory Leak when Requerying with a CTime Parameter

Q115035

The information in this article applies to:

- The Microsoft Foundation Classes (MFC), included with:
  - Microsoft Visual C++, version 1.5

## SYMPTOMS

When running an MFC application that uses the database classes, the following message may appear in the Output Window of the debugger:

>        Detected memory leaks!
>        Dumping objects -&gt;
>        {45}dbrfx.cpp(1331) : non-object block at $3FF74442, 16 bytes long
>        Object dump complete.

The message only occurs when you select a date/time field of a table, use RFX_Date() to map a CTime variable to the field, and call Requery().

## CAUSE

Line 1331 in DBRFX.CPP includes the following code:

        // Allocate TIMESTAMP_STRUCT for SQLBindCol
        pFX->m_prs->m_pvFieldProxy[nField-1] = new TIMESTAMP_STRUCT;

This line runs each time date/time fields are bound to CTime recordset variables using the RFX_Date() record field exchange function. When doing a Requery(), the fields of the result set must be rebound to the CRecordset's variables, and thus a Requery() causes the memory leak to occur. The line of code shown above assigns a new value to an element of a pointer array, leaving the previous allocation stranded in memory.

## RESOLUTION

To resolve the memory leak, override the virtual Requery() function of CRecordset and delete the elements of the m_pvFieldProxy array. The following code demonstrates what to do:

    BOOL CMyAppRecordSet::Requery()
    {
        // delete proxies for recordset fields
        if (m_pvFieldProxy != NULL)
        {
            for (UINT nField = 0; nField != m_nFields; nField++)
                delete m_pvFieldProxy[nField];
        }

        return CRecordset::Requery();
    }

## STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug was corrected in Visual C++ version 1.51.

Additional query words: ODBC 1.50 2.50

Keywords : kb16bitonly kbDatabase kbMFC kbODBC kbVC
Issue type :
Technology : kbAudDeveloper kbMFC

Last Reviewed: May 8, 2001 © 2001 Microsoft Corporation. All rights reserved. Terms of Use.