FIX: A memory leak may occur when you use the Advanced Data Tablegram persistence mechanism to persist a hierarchical recordset to a Stream object in a Microsoft Data Access Components 2.8 application (898056)



The information in this article applies to:

  • Microsoft Data Access Components 2.8

SYMPTOMS

In a Microsoft Data Access Components (MDAC) 2.8 application, when you use the Advanced Data Tablegram (ADTG) persistence mechanism to persist a hierarchical recordset to a Stream object by using the Recordset.Save method, a memory leak may occur.

Note To use the ADTG persistence mechanism, pass the adPersistADTG value as the PersistFormat parameter.

CAUSE

The ADTG persistence mechanism retrieves the chapters that correspond to a hierarchical recordset. However, even when you close the Stream object to which you persisted the recordset, and then you release this Stream object, the ADTG persistence mechanism does not release the chapters. The ADTG persistence mechanism incorrectly releases the chapters only when you close the recordset and then release the recordset. Therefore, the problem that is mentioned in the "Symptoms" section may occur.

RESOLUTION

A supported hotfix is now available from Microsoft, but it is only intended to correct the problem that this article describes. Apply it only to systems that are experiencing this specific problem.

To resolve this problem, contact Microsoft Product Support Services to obtain the hotfix. For a complete list of Microsoft Product Support Services telephone numbers and information about support costs, visit the following Microsoft Web site:Note In special cases, charges that are ordinarily incurred for support calls may be canceled if a Microsoft Support Professional determines that a specific update will resolve your problem. The usual support costs will apply to additional support questions and issues that do not qualify for the specific update in question.

The English version of this hotfix has the file attributes (or later file attributes) that are listed in the following table. The dates and times for these files are listed in Coordinated Universal Time (UTC). When you view the file information, it is converted to local time. To find the difference between UTC and local time, use the Time Zone tab in the Date and Time tool in Control Panel.
   Date         Time   Version         Size     File name
   ---------------------------------------------------------  
   22-Apr-2005  22:01  2000.85.1061.0   73,728  Dbnetlib.dll     
   22-Apr-2005  22:02  2000.85.1061.0   28,672  Dbnmpntw.dll     
   22-Apr-2005  22:01  2.80.1061.0     315,392  Msadce.dll       
   22-Apr-2005  22:01  2.80.1061.0     135,168  Msadco.dll       
   22-Apr-2005  22:02  2.80.1061.0      49,152  Msadcs.dll       
   22-Apr-2005  22:01  2.80.1061.0     147,456  Msadds.dll       
   22-Apr-2005  22:01  2.80.1061.0     507,904  Msado15.dll      
   22-Apr-2005  22:02  2.80.1061.0     163,840  Msadomd.dll      
   22-Apr-2005  22:02  2.80.1061.0     184,320  Msadox.dll       
   22-Apr-2005  22:01  2.80.1061.0      53,248  Msadrh15.dll     
   22-Apr-2005  22:02  2.80.1061.0     225,280  Msdaora.dll      
   22-Apr-2005  22:01  2.80.1061.0     192,512  Msdaprst.dll     
   22-Apr-2005  22:01  2.80.1061.0     147,456  Msdart.dll       
   22-Apr-2005  22:01  2.80.1061.0     303,104  Msdasql.dll      
   22-Apr-2005  22:02  2.575.1061.0    139,264  Msorcl32.dll     
   22-Apr-2005  22:01  3.525.1061.0    221,184  Odbc32.dll       
   22-Apr-2005  22:02  2000.85.1061.0   24,576  Odbcbcp.dll      
   22-Apr-2005  22:01  2.80.1061.0     442,368  Oledb32.dll      
   22-Apr-2005  22:01  2000.85.1061.0  503,808  Sqloledb.dll     
   22-Apr-2005  22:01  2000.85.1061.0  401,408  Sqlsrv32.dll     
   22-Apr-2005  21:50  2000.85.1061.0  208,896  Sqlxmlx.dll

WORKAROUND

To work around this problem, close the recordset, and then release the recordset. Do this immediately after you release the Stream object to which you persisted the recordset. For example, you can use the following code example to close and to release a recordset that is named g_rs.
g_rs.Close 
Set g_rs = Nothing

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

Steps to reproduce the problem

  1. Start Microsoft Visual Basic 6.0.
  2. Create a Standard EXE project. By default, a form that is named Form1 is created.
  3. Add a project reference to the Microsoft ActiveX Data Objects 2.8 Library.
  4. Add two command buttons to the Form1 form. By default, the Command1 command button and the Command2 command button are created.
  5. In the Caption property of the Command1 command button, type Memory Leak Test.
  6. In the Caption property of the Command2 command button, type Memory No-Leak Test.
  7. Add the following lines of code to the Form1 form.
    Private Sub Command1_Click()
    Call LeakTest
    End Sub
    
    Private Sub Command2_Click()
    Call NoLeakTest
    End Sub
  8. Add a module to the project. By default, a module that is named Module1 is created.
  9. Add the following lines of code to the Module1 module.
    Option Explicit
    
    ' The following procedure demonstrates a memory leak that occurs when you use the
    ' Advanced Data Tablegram (ADTG) persistence mechanism to persist a hierarchical
    ' recordset to a Stream object:
    Sub LeakTest()
        
        Dim g_rs As ADODB.Recordset
    
        Dim i           As Long
        Dim j           As Long
        Dim lIndLeak    As Long
        Dim sShape      As String
        Dim rsChild     As ADODB.Recordset
        Dim oStream     As ADODB.Stream
        
        ' Use the SHAPE command to create a disconnected hierarchical recordset.
        sShape = "SHAPE APPEND new adVarChar(14) as Field0, " & _
                             " new adVarChar(14) as Field1, " & _
                             " new adVarChar(14) as Field2, " & _
                             " new adVarChar(14) as Field3, " & _
                             " ((SHAPE APPEND new adVarChar(14) as chldField0, " & _
                                            " new adVarChar(14) as chldField1, " & _
                                            " new adVarChar(14) as chldField2, " & _
                                            " new adVarChar(14) as chldField3 " & _
                             "   ) RELATE Field0 to chldField0) as Field4 "
    
        ' Create the Recordset object.
        If g_rs Is Nothing Then
            Set g_rs = New ADODB.Recordset
            g_rs.CursorLocation = adUseClient
            g_rs.Open sShape, "Provider=MsDataShape;Data Provider=None;", adOpenStatic, adLockBatchOptimistic, adCmdText
        
            ' Insert data to the recordset.
            For i = 0 To 10000
                g_rs.AddNew
                g_rs(0).Value = "Value0" & i
                g_rs(1).Value = "Value1" & i
                g_rs(2).Value = "Value2" & i
                g_rs(3).Value = "Value3" & i
                Set rsChild = g_rs(4).Value
                For j = 0 To 10
                    rsChild.AddNew
                    rsChild(0).Value = "Value0" & i
                    rsChild(1).Value = "Value1" & j
                    rsChild(2).Value = "Value2" & j
                    rsChild(3).Value = "Value3" & j
                    rsChild.Update
                Next j
                g_rs.Update
                
            Next i
            Set rsChild = Nothing
        End If
        
        MsgBox "Start the memory leak test."
        
        ' The following loop generates the memory leak. You create a Stream object, save
        ' the previously generated recordset to the Stream object, close the Stream object,
        ' and then release the Stream object. You notice that some memory
        ' that was allocated in the process of saving the recordset is not released.
        For lIndLeak = 0 To 20
        
           Set oStream = New Stream
           Call g_rs.Save(oStream, adPersistADTG)
        
           oStream.Close
           Set oStream = Nothing
           
        Next lIndLeak
        
        MsgBox "End the memory leak test."
        
        ' The memory is released only after you close and then release the original 
        ' recordset. To do this, uncomment the following two lines of code:
        
        'g_rs.Close
        'Set g_rs = Nothing
        
        ' If you do not release the recordset by using the previous line of code, and if
        ' g_rs is a global variable, the memory will continue to be used.
    End Sub
    
    ' The following procedure demonstrates that no memory leak occurs when you use the
    ' Advanced Data Tablegram (ADTG) persistence mechanism to persist a nonhierarchical
    ' recordset to a Stream object:
    Sub NoLeakTest()
    
        Dim g_rs As ADODB.Recordset
    
        Dim i           As Long
        Dim j           As Long
        Dim lIndLeak    As Long
        Dim sShape      As String
        Dim rsChild     As ADODB.Recordset
        Dim oStream     As ADODB.Stream
        
        ' Use the SHAPE command to create a disconnected nonhierarchical recordset.
        ' Notice that the part of the code that you used to create the hierarchical recordset 
        ' has been commented to make sure that you create a nonhierarchical recordset.
        sShape = "SHAPE APPEND new adVarChar(14) as Field0, " & _
                             " new adVarChar(14) as Field1, " & _
                             " new adVarChar(14) as Field2, " & _
                             " new adVarChar(14) as Field3 " '& _
    '                         " ((SHAPE APPEND new adVarChar(14) as chldField0, " & _
    '                                        " new adVarChar(14) as chldField1, " & _
    '                                        " new adVarChar(14) as chldField2, " & _
    '                                        " new adVarChar(14) as chldField3 " & _
    '                         "   ) RELATE Field0 to chldField0) as Field4 "
    
        ' Create the Recordset object.
        If g_rs Is Nothing Then
            Set g_rs = New ADODB.Recordset
            g_rs.CursorLocation = adUseClient
            g_rs.Open sShape, "Provider=MsDataShape;Data Provider=None;", adOpenStatic, adLockBatchOptimistic, adCmdText
        
            ' Insert data to the recordset.
            For i = 0 To 10000
                g_rs.AddNew
                g_rs(0).Value = "Value0" & i
                g_rs(1).Value = "Value1" & i
                g_rs(2).Value = "Value2" & i
                g_rs(3).Value = "Value3" & i
                ' Notice that the next eight lines of code have been commented because
                ' they correspond to code for the hierarchical recordset.
    '            Set rsChild = g_rs(4).Value
    '            For j = 0 To 10
    '                rsChild.AddNew
    '                rsChild(0).Value = "Value0" & i
    '                rsChild(1).Value = "Value1" & j
    '                rsChild(2).Value = "Value2" & j
    '                rsChild(3).Value = "Value3" & j
    '            Next j
                g_rs.Update
            Next i
            ' The following line of code has also been commented because it corresponds to
            ' the hierarchical recordset.
    '        Set rsChild = Nothing
        End If
        
        MsgBox "Start the memory no-leak test."
        
        ' The following loop does not generate a memory leak because you are using a
        ' nonhierarchical recordset.
        For lIndLeak = 0 To 20
        
           Set oStream = New Stream
           Call g_rs.Save(oStream, adPersistADTG)
           
           oStream.Close
           Set oStream = Nothing
        
        Next lIndLeak
        
        MsgBox "End the memory no-leak test."
        
        ' The following code is not required because there is no memory leak:
        'g_rs.Close
        'Set g_rs = Nothing
    End Sub 
  10. Run the application. The Form1 form appears.
  11. To test the memory leak scenario, click Memory Leak Test. To test the no-leak scenario, click Memory No-Leak Test.

REFERENCES

For more information about this issue on computers where Microsoft Data Access Components 2.7 is installed, click the following article number to view the article in the Microsoft Knowledge Base:

832921 FIX: Memory leak occurs when you use the Advanced Data Tablegram persistence mechanism to persist a hierarchical recordset to a Stream object

For more information about the terminology that is used to describe Microsoft software updates, click the following article number to view the article in the Microsoft Knowledge Base:

824684 Description of the standard terminology that is used to describe Microsoft software updates

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:


Modification Type:MinorLast Reviewed:6/1/2006
Keywords:kbcode kbPersistSt kbfix kbBug kbHotfixServer kbQFE KB898056 kbAudDeveloper