Issue with ExecuteScalar()

Use this forum for questions on how to use .NET Memory Profiler and how to analyse memory usage.
Post Reply
tkelley@hrimaging.com
Posts: 7
Joined: Mon Dec 07, 2009 7:34 pm

Issue with ExecuteScalar()

Post by tkelley@hrimaging.com » Mon Feb 22, 2010 4:43 pm

I have an applicatin the has an mdi parent and an mdi child form. On the child form I call a function that returns an integer value. The function call lookls like this: _rowVersionLF = Common.ReturnSingleIntegerValue(strSQL);

The Common.ReturnSingleIntegerValue is this:

Code: Select all

public static int ReturnSingleIntegerValue(string _sqlStr)
        { 
            string conString = System.Configuration.ConfigurationManager.AppSettings["DP2"];
            SqlConnection con = new SqlConnection(conString);
            con.Open();
            SqlCommand cmd = new SqlCommand(_sqlStr, con);
            cmd.CommandTimeout = 150;

            tmpInt = (int)cmd.ExecuteScalar();
            con.Close();
            con.Dispose();
            cmd.Dispose();

            return (tmpInt);
        }
tmpint is declared in the Common project.

After closing the form and taking a snapshot, I am told there is an undisposed instance (remove external reference) of a sqldatareader.

Here is a copy of the callstack:

System.Data!System.Data.SqlClient.SqlCommand.RunExecuteReaderTds( CommandBehavior,RunBehavior,bool,bool )
System.Data!System.Data.SqlClient.SqlCommand.RunExecuteReader( CommandBehavior,RunBehavior,bool,string,DbAsyncResult )
System.Data!System.Data.SqlClient.SqlCommand.RunExecuteReader( CommandBehavior,RunBehavior,bool,string )
System.Data!System.Data.SqlClient.SqlCommand.ExecuteScalar()
HRIChimp.Win.Common!HRIChimp.Win.Common.ReturnSingleIntegerValue( string ) Common.cs
HRIChimp.Win.SubjectInfo!HRIChimp.Win.SubjectInfo.SubjectInfo.PopulateDetails() SubjectInfo.cs
HRIChimp.Win.SubjectInfo!HRIChimp.Win.SubjectInfo.SubjectInfo.SubjectInfo_Load( object,EventArgs ) SubjectInfo.cs
System.Windows.Forms!System.Windows.Forms.Form.OnLoad( EventArgs )
HRIChimp.Win.Common!HRIChimp.Win.BaseForm.OnLoad( EventArgs ) BaseForm.cs
System.Windows.Forms!System.Windows.Forms.Form.OnCreateControl()
System.Windows.Forms!System.Windows.Forms.Control.CreateControl( bool )
System.Windows.Forms!System.Windows.Forms.Control.CreateControl()
System.Windows.Forms!System.Windows.Forms.Control.WmShowWindow( ref Message )
System.Windows.Forms!System.Windows.Forms.Control.WndProc( ref Message )
System.Windows.Forms!System.Windows.Forms.ScrollableControl.WndProc( ref Message )
System.Windows.Forms!System.Windows.Forms.ContainerControl.WndProc( ref Message )
System.Windows.Forms!System.Windows.Forms.Form.WmShowWindow( ref Message )
System.Windows.Forms!System.Windows.Forms.Form.WndProc( ref Message )
System.Windows.Forms!System.Windows.Forms.Control.ControlNativeWindow.OnMessage( ref Message )
System.Windows.Forms!System.Windows.Forms.Control.ControlNativeWindow.WndProc( ref Message )
System.Windows.Forms!System.Windows.Forms.NativeWindow.Callback( IntPtr,int,IntPtr,IntPtr )
[Native to managed transition]
[Managed to native transition]
System.Windows.Forms!System.Windows.Forms.UnsafeNativeMethods.IntCreateWindowEx( int,string,string,int,int,int,int,int,HandleRef,HandleRef,HandleRef,object )
System.Windows.Forms!System.Windows.Forms.UnsafeNativeMethods.CreateWindowEx( int,string,string,int,int,int,int,int,HandleRef,HandleRef,HandleRef,object )
[Truncated]

I am not sure if there is an issue with the code or the .NET Framework (2.0)
T Kelley

Andreas Suurkuusk
Posts: 1005
Joined: Wed Mar 02, 2005 7:53 pm

Re: Issue with ExecuteScalar()

Post by Andreas Suurkuusk » Tue Feb 23, 2010 9:27 pm

The SqlDataReader instance indirectly created by ExecuteScalar is falsely identified as undisposed by the profiler. The Close method of the SqlDataReader is called, instead of the Dispose method. This causes the profiler to miss that the instance is actually (sort of) disposed (the Dispose method calls Close).

The profiler actually knows that the Close method of the SqlDataReader is a substitute for the Dispose, but in the current version this method is not used to detect that SqlDataReader instances are disposed. This will be improved in a future version.
Best regards,

Andreas Suurkuusk
SciTech Software AB

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests