Page 1 of 1

Issue with ExecuteScalar()

Posted: Mon Feb 22, 2010 4:43 pm
by tkelley@hrimaging.com
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)

Re: Issue with ExecuteScalar()

Posted: Tue Feb 23, 2010 9:27 pm
by Andreas Suurkuusk
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.