Microsoft KB Archive/832921

From BetaArchive Wiki

Article ID: 832921

Article Last Modified on 11/5/2007



APPLIES TO

  • Microsoft Data Access Components 2.7
  • Microsoft Data Access Components 2.7 Service Pack 1




SYMPTOMS

In a Microsoft Visual Basic 6.0 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 adPersistADTG 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 that you persisted the recordset to, and then you release this Stream object, the ADTG persistence mechanism does not release the chapters. This mechanism incorrectly releases the chapters only when you close the recordset and then release the recordset. Therefore, the behavior 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
   --------------------------------------------------------------
   21-Nov-2003  08:48  2.71.9045.0       307,200  Msadce.dll       
   21-Nov-2003  08:51  2.71.9045.0        57,344  Msadcf.dll       
   21-Nov-2003  08:51  2.71.9045.0       131,072  Msadco.dll       
   21-Nov-2003  08:51  2.71.9045.0        53,248  Msadcs.dll       
   21-Nov-2003  08:48  2.71.9045.0       147,456  Msadds.dll       
   21-Nov-2003  08:48  2.71.9045.0       180,224  Msdaprst.dll     
   21-Nov-2003  08:51  2.71.9045.0       110,592  Msdarem.dll      
   21-Nov-2003  08:51  2.71.9045.0        32,768  Msdfmap.dll

Notice

This hotfix is available as part of a cumulative hotfix package. When you receive this hotfix from Microsoft Product Support Services, the article number that is listed in the hotfix package may be different from the cumulative article number. For more information about the hotfix package, see the following article in the Microsoft Knowledge Base:

836799 FIX: Hotfixes are available for MDAC 2.7 Service Pack 1


WORKAROUND

To work around the behavior that is mentioned in the "Symptoms" section, immediately after you release the Stream object that you persisted the recordset to, close the recordset, and then release the recordset. For example, you can use the following code example to close and then 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 behavior

  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.7 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 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 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 record
        ' set. 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 non-hierarchical
    ' 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 non-hierarchical recordset.
        ' Notice that the part of the code that you used to create the hierarchical record
        ' set has been commented to make sure that you create a non-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
                ' 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
        ' non-hierarchical 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 necessary because there is no memory leak:
        'g_rs.Close
        'Set g_rs = Nothing
    End Sub
  10. Run the application. The Form1 form appears.
  11. Click Memory Leak Test or click Memory No-Leak Test to test the memory leak scenario or the no-leak scenario.


REFERENCES

For additional information, 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:


Keywords: kbhotfixserver kbqfe kbpersistst kbqfe kbcode kbfix kbbug KB832921