Microsoft KB Archive/250315

= FIX: Memory Leak When You Clone a Server-Side Recordset =

Article ID: 250315

Article Last Modified on 5/17/2007

-

APPLIES TO


 * Microsoft ActiveX Data Objects 2.5

-



This article was previously published under Q250315



SYMPTOMS
If you clone a server-side ADO Recordset object, you may encounter a memory leak. This behavior can occur in varying degrees in any of the following products:


 * Microsoft OLE DB Provider for ODBC
 * Microsoft OLE DB Provider for Jet, version 4.0
 * Microsoft OLE DB Provider for SQL Server



RESOLUTION
If it is necessary to clone a Recordset, use a client-side cursor.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article. This bug was corrected in ActiveX Data Objects (ADO), version 2.6. You can download the latest version of ADO from the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/data/aa937695.aspx



Steps to Reproduce Behavior
 Create a new Standard EXE project in Visual Basic. Form1 is created by default. From the Project Menu, click References, and select the Microsoft Active X Data Object 2.5 check box.  Paste the following code in the form module of Form1:

Note You must change User ID= to the correct value before you run this code. Make sure that User ID has the appropriate permissions to perform this operation on the database. Option Explicit

Private Sub Form_Load Dim cnn As ADODB.Connection Dim rs As ADODB.Recordset Dim crs As Object Set cnn = CreateObject("ADODB.Connection") cnn.Open "Provider=SQLOLEDB;Data Source=servername;" & _ "Initial Catalog=pubs;User ID=;" Set rs = New ADODB.Recordset rs.CursorLocation = adUseServer 'rs.CursorLocation = adUseClient rs.Open "select * from authors where au_lname like 'S%'", _ cnn, adOpenStatic, adLockReadOnly Dim i As Long For i = 0 To 120000 Set crs = rs.Clone crs.Filter = "Au_Lname = 'Smith'" crs.Close Set crs = Nothing Next i   rs.Close cnn.Close Set rs = Nothing Set cnn = Nothing End Sub  Modify the connection string to connect to your Microsoft SQL Server database. Start Performance Monitor, and monitor the private bytes for the Visual Basic 6.0 process.</li> Run the code, and notice that the private bytes increase steadily. When the private bytes reach 100 percent, most likely the application locks up.</li>  Uncomment the following line of code: 'rs.CursorLocation = adUseClient and comment the following line: rs.CursorLocation = adUseServer </li> Run the application, and monitor the private bytes. Notice that the private bytes increase but then level off, and the application does not lock up.</li></ol>

Additional query words: mem leak serverside cursor

Keywords: kbado260fix kbbug kbfix KB250315

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.