Microsoft KB Archive/898056

= 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 =

Article ID: 898056

Article Last Modified on 11/15/2007

-

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 &quot;Symptoms&quot; section may occur.



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

To resolve this problem, submit a request to Microsoft Online Customer Services to obtain the hotfix. To submit an online request to obtain the hotfix, visit the following Microsoft Web site:

http://go.microsoft.com/?linkid=6294451

Note If additional issues occur or any troubleshooting is required, you might have to create a separate service request. The usual support costs will apply to additional support questions and issues that do not qualify for this specific hotfix. To create a separate service request, visit the following Microsoft Web site:

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

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



Steps to reproduce the problem
 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.8 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 lines of 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 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 = &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 ' 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 = &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   ' 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 &quot;End the memory no-leak test.&quot; ' The following code is not required because there is no memory leak: 'g_rs.Close 'Set g_rs = Nothing End Sub </li> Run the application. The Form1 form appears.</li> To test the memory leak scenario, click Memory Leak Test. To test the no-leak scenario, click Memory No-Leak Test.</li></ol>

<div class="references_section">