Home All Groups Group Topic Archive Search About

Possible memory leak in SQLDataReader().

Author
21 Feb 2006 7:31 PM
Ken Varn
I have a managed C++ application that is exhibiting a memory leak.  I
isolated the leak down to a section of code that basically uses a managed
ADO.NET SQLDataReader object.  The leak seems to occur on the
ExecuteReader() call.  I am calling this method over and over again at
fairly rapid intervals.  The Close() method of the reader is always called
when I am done with the SQLDataReader.  Can someone tell me if there is a
way around this leak or if there is a fix out there?

Sample Code:

SqlCommand *SQLCmd = __gc new SqlCommand();
SqlDataReader *SQLReader;

SQLCmd->Connection = MyConnectionObject;
SQLCmd->CommandText = "sp_SelectImageEventID @nImageType = 1";   // My
stored procedure
SQLReader = SQLCmd->ExecuteReader();

if (SQLReader->HasRows)
{
// Do stuff
}

SQLReader->Close();



--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------

Author
22 Feb 2006 7:49 AM
Miha Markic [MVP C#]
Since SqlDataReader implements IDisposable, you should call Dispose after
you are done with.

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Show quote
"Ken Varn" <nospam> wrote in message
news:uO1yq1xNGHA.1032@TK2MSFTNGP11.phx.gbl...
>I have a managed C++ application that is exhibiting a memory leak.  I
> isolated the leak down to a section of code that basically uses a managed
> ADO.NET SQLDataReader object.  The leak seems to occur on the
> ExecuteReader() call.  I am calling this method over and over again at
> fairly rapid intervals.  The Close() method of the reader is always called
> when I am done with the SQLDataReader.  Can someone tell me if there is a
> way around this leak or if there is a fix out there?
>
> Sample Code:
>
> SqlCommand *SQLCmd = __gc new SqlCommand();
> SqlDataReader *SQLReader;
>
> SQLCmd->Connection = MyConnectionObject;
> SQLCmd->CommandText = "sp_SelectImageEventID @nImageType = 1";   // My
> stored procedure
> SQLReader = SQLCmd->ExecuteReader();
>
> if (SQLReader->HasRows)
> {
> // Do stuff
> }
>
> SQLReader->Close();
>
>
>
> --
> -----------------------------------
> Ken Varn
> Senior Software Engineer
> Diebold Inc.
>
> EmailID = varnk
> Domain = Diebold.com
> -----------------------------------
>
>
Author
22 Feb 2006 1:36 PM
Ken Varn
I tried calling Dispose() too.  It still appears to be leaking memory.  I
have not quite isolated the cause, but I think it may have something to do
with the fact that I switch connections fairly often as well.

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
Show quote
"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
news:uXafHS4NGHA.740@TK2MSFTNGP12.phx.gbl...
> Since SqlDataReader implements IDisposable, you should call Dispose after
> you are done with.
>
> --
> Miha Markic [MVP C#]
> RightHand .NET consulting & development www.rthand.com
> Blog: http://cs.rthand.com/blogs/blog_with_righthand/
>
> "Ken Varn" <nospam> wrote in message
> news:uO1yq1xNGHA.1032@TK2MSFTNGP11.phx.gbl...
> >I have a managed C++ application that is exhibiting a memory leak.  I
> > isolated the leak down to a section of code that basically uses a
managed
> > ADO.NET SQLDataReader object.  The leak seems to occur on the
> > ExecuteReader() call.  I am calling this method over and over again at
> > fairly rapid intervals.  The Close() method of the reader is always
called
> > when I am done with the SQLDataReader.  Can someone tell me if there is
a
> > way around this leak or if there is a fix out there?
> >
> > Sample Code:
> >
> > SqlCommand *SQLCmd = __gc new SqlCommand();
> > SqlDataReader *SQLReader;
> >
> > SQLCmd->Connection = MyConnectionObject;
> > SQLCmd->CommandText = "sp_SelectImageEventID @nImageType = 1";   // My
> > stored procedure
> > SQLReader = SQLCmd->ExecuteReader();
> >
> > if (SQLReader->HasRows)
> > {
> > // Do stuff
> > }
> >
> > SQLReader->Close();
> >
> >
> >
> > --
> > -----------------------------------
> > Ken Varn
> > Senior Software Engineer
> > Diebold Inc.
> >
> > EmailID = varnk
> > Domain = Diebold.com
> > -----------------------------------
> >
> >
>
>
Author
22 Feb 2006 1:48 PM
Miha Markic [MVP C#]
What can I say - you should use a memory profiler to check whether there is
a real leak .

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Show quote
"Ken Varn" <nospam> wrote in message
news:%23KQl%23T7NGHA.2472@TK2MSFTNGP11.phx.gbl...
>I tried calling Dispose() too.  It still appears to be leaking memory.  I
> have not quite isolated the cause, but I think it may have something to do
> with the fact that I switch connections fairly often as well.
>
> --
> -----------------------------------
> Ken Varn
> Senior Software Engineer
> Diebold Inc.
>
> EmailID = varnk
> Domain = Diebold.com
> -----------------------------------
> "Miha Markic [MVP C#]" <miha at rthand com> wrote in message
> news:uXafHS4NGHA.740@TK2MSFTNGP12.phx.gbl...
>> Since SqlDataReader implements IDisposable, you should call Dispose after
>> you are done with.
>>
>> --
>> Miha Markic [MVP C#]
>> RightHand .NET consulting & development www.rthand.com
>> Blog: http://cs.rthand.com/blogs/blog_with_righthand/
>>
>> "Ken Varn" <nospam> wrote in message
>> news:uO1yq1xNGHA.1032@TK2MSFTNGP11.phx.gbl...
>> >I have a managed C++ application that is exhibiting a memory leak.  I
>> > isolated the leak down to a section of code that basically uses a
> managed
>> > ADO.NET SQLDataReader object.  The leak seems to occur on the
>> > ExecuteReader() call.  I am calling this method over and over again at
>> > fairly rapid intervals.  The Close() method of the reader is always
> called
>> > when I am done with the SQLDataReader.  Can someone tell me if there is
> a
>> > way around this leak or if there is a fix out there?
>> >
>> > Sample Code:
>> >
>> > SqlCommand *SQLCmd = __gc new SqlCommand();
>> > SqlDataReader *SQLReader;
>> >
>> > SQLCmd->Connection = MyConnectionObject;
>> > SQLCmd->CommandText = "sp_SelectImageEventID @nImageType = 1";   // My
>> > stored procedure
>> > SQLReader = SQLCmd->ExecuteReader();
>> >
>> > if (SQLReader->HasRows)
>> > {
>> > // Do stuff
>> > }
>> >
>> > SQLReader->Close();
>> >
>> >
>> >
>> > --
>> > -----------------------------------
>> > Ken Varn
>> > Senior Software Engineer
>> > Diebold Inc.
>> >
>> > EmailID = varnk
>> > Domain = Diebold.com
>> > -----------------------------------
>> >
>> >
>>
>>
>
>
Author
22 Feb 2006 2:53 PM
Mike
FYI, Close() calls Dispose() internally.  So a call to Close is
sufficient.

You should be calling Close in a finally block.  Otherwise if there is
an exception, close may be skipped.

//instantiate reader
try
{
  //execute reader
}
finally
{
  //close reader
}

Also if you are calling a stored procedure, you should be using a
CommandType of "CommandType.StoredProcedure", then add your parameter
via the parameters collection.

I'm a C# developer, not C++ so I can't comment on if your syntax or
usage of pointers is correct.

Michael Lang

AddThis Social Bookmark Button