Microsoft KB Archive/832921

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

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 &quot;Symptoms&quot; 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:

http://support.microsoft.com/contactus/?ws=support

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 &quot;Symptoms&quot; 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 &quot;Applies to&quot; section.



Steps to reproduce the behavior
 Start Microsoft Visual Basic 6.0. Create a Standard EXE project. By default, a form that is named Form1 is created. Add a project reference to the Microsoft ActiveX Data Objects 2.7 Library. Add two command buttons to the Form1 form. By default, the Command1 command button and the Command2 command button are created.</li> In the Caption property of the Command1 command button, type Memory Leak Test .</li> In the Caption property of the Command2 command button, type Memory No-Leak Test .</li>  Add the following code to the Form1 form: Private Sub Command1_Click Call LeakTest End Sub

Private Sub Command2_Click Call NoLeakTest End Sub </li> Add a module to the project. By default, a module that is named Module1 is created.</li>  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 = &quot;SHAPE APPEND new adVarChar(14) as Field0, &quot; & _ &quot; new adVarChar(14) as Field1, &quot; & _ &quot; new adVarChar(14) as Field2, &quot; & _ &quot; new adVarChar(14) as Field3, &quot; & _ &quot; ((SHAPE APPEND new adVarChar(14) as chldField0, &quot; & _ &quot; new adVarChar(14) as chldField1, &quot; & _ &quot; new adVarChar(14) as chldField2, &quot; & _ &quot; new adVarChar(14) as chldField3 &quot; & _ &quot;  ) RELATE Field0 to chldField0) as Field4 &quot;

' Create the Recordset object. If g_rs Is Nothing Then Set g_rs = New ADODB.Recordset g_rs.CursorLocation = adUseClient g_rs.Open sShape, &quot;Provider=MsDataShape;Data Provider=None;&quot;, adOpenStatic, adLockBatchOptimistic, adCmdText ' Insert data to the recordset. For i = 0 To 10000 g_rs.AddNew g_rs(0).Value = &quot;Value0&quot; & i           g_rs(1).Value = &quot;Value1&quot; & i            g_rs(2).Value = &quot;Value2&quot; & i            g_rs(3).Value = &quot;Value3&quot; & i            Set rsChild = g_rs(4).Value For j = 0 To 10 rsChild.AddNew rsChild(0).Value = &quot;Value0&quot; & i               rsChild(1).Value = &quot;Value1&quot; & j                rsChild(2).Value = &quot;Value2&quot; & j                rsChild(3).Value = &quot;Value3&quot; & j                rsChild.Update Next j           g_rs.Update Next i       Set rsChild = Nothing End If   MsgBox &quot;Start the memory leak test.&quot; ' 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 &quot;End the memory leak test.&quot; ' 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 = &quot;SHAPE APPEND new adVarChar(14) as Field0, &quot; & _ &quot; new adVarChar(14) as Field1, &quot; & _ &quot; new adVarChar(14) as Field2, &quot; & _ &quot; new adVarChar(14) as Field3 &quot; '& _ '                        &quot; ((SHAPE APPEND new adVarChar(14) as chldField0, &quot; & _ '                                       &quot; new adVarChar(14) as chldField1, &quot; & _ '                                       &quot; new adVarChar(14) as chldField2, &quot; & _ '                                       &quot; new adVarChar(14) as chldField3 &quot; & _ '                        &quot;   ) RELATE Field0 to chldField0) as Field4 &quot;

' Create the Recordset object. If g_rs Is Nothing Then Set g_rs = New ADODB.Recordset g_rs.CursorLocation = adUseClient g_rs.Open sShape, &quot;Provider=MsDataShape;Data Provider=None;&quot;, adOpenStatic, adLockBatchOptimistic, adCmdText ' Insert data to the recordset. For i = 0 To 10000 g_rs.AddNew g_rs(0).Value = &quot;Value0&quot; & i           g_rs(1).Value = &quot;Value1&quot; & i            g_rs(2).Value = &quot;Value2&quot; & i            g_rs(3).Value = &quot;Value3&quot; & 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 = &quot;Value0&quot; & i '                rsChild(1).Value = &quot;Value1&quot; & j '                rsChild(2).Value = &quot;Value2&quot; & j '                rsChild(3).Value = &quot;Value3&quot; & 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 &quot;Start the memory no-leak test.&quot; ' 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 &quot;End the memory no-leak test.&quot; ' The following code is not necessary because there is no memory leak: 'g_rs.Close 'Set g_rs = Nothing End Sub </li> Run the application. The Form1 form appears.</li> Click Memory Leak Test or click Memory No-Leak Test to test the memory leak scenario or the no-leak scenario.</li></ol>

<div class="references_section">