473,405 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How can I make sure all unmanaged resources had been released ?

for example: SqlConnection is used in my project, how can I know if all
connections were closed after open and execution. If some guys forget to
close connections after using, how can i check it out ?
best,
eric

Nov 16 '05 #1
6 1780
Eric,

The System.Data.SqlClient library is a pure .Net client interface to the
database. Therefore if the connection is not closed, there is the timeout
and garbage collector to sort it out if someone doesn't do the job of
closing it down manually.

What type of unmanaged resources are you refering too?

If the managed code is hooked into native code via the Interop's, then the
garbage collector will again call the destructors on your unmanaged code
when it thinks you're finished with them during a clear up.

Therefore if the unmanaged code is not desroying its objects, the garbage
collector destroy these uncleared objects either as the onus lies with the
unmanaged resource and not the .Net application.
"Eric" <wa**********@msn.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
for example: SqlConnection is used in my project, how can I know if all
connections were closed after open and execution. If some guys forget to
close connections after using, how can i check it out ?
best,
eric

Nov 16 '05 #2
Thank you for your reply.

SqlClient.SqlConnection should manually call Close() or Dispose() after
using. MSDN point out this. If we didn't call Close() or Dispose() manually,
SqlConnecont won't be closed when the connection object is out of range, GC
would close the connection object in some magic time. That means, if we
don't call Close() or Dispose() as soon as possible, the connection_pool
would be exhausted.

What's the worse, the connection_pool is organized by *process* and
*ConnectionString*. This means diffrent modules in one program, would share
a same connection_pool. If one connection_pool was exhausted, we can not
even find out which module cause this problem. In a huge project, this will
be a very troublesome problem, I think.


"Dan =o)" <danielbass [at] postmaster [dot] co [dot] uk> дÈëÓʼþ
news:ub*************@TK2MSFTNGP11.phx.gbl...
Eric,

The System.Data.SqlClient library is a pure .Net client interface to the
database. Therefore if the connection is not closed, there is the timeout
and garbage collector to sort it out if someone doesn't do the job of
closing it down manually.

What type of unmanaged resources are you refering too?

If the managed code is hooked into native code via the Interop's, then the
garbage collector will again call the destructors on your unmanaged code
when it thinks you're finished with them during a clear up.

Therefore if the unmanaged code is not desroying its objects, the garbage
collector destroy these uncleared objects either as the onus lies with the
unmanaged resource and not the .Net application.
"Eric" <wa**********@msn.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
for example: SqlConnection is used in my project, how can I know if all
connections were closed after open and execution. If some guys forget to
close connections after using, how can i check it out ?
best,
eric


Nov 16 '05 #3
Firstly, I'm not sure why this is a problem... The pooling technique is
offered as a default so that the overhead is kept down when two connections
"appear" to be the same in the same process. This saves memory and resources
in general. If the connection times out because the "Connection Lifttime"
has expired, the the GC removes the pool from memory...

It's not a case of should, you "must" call Close()/Dispose()... Although
it's not as imperative as, calling "Open()" say, the fact is, failing to
call close leaves this connection wide open, and if you've numerous
different open connections, this in itself saps system resources.

Surely a simply find in the solution on "Open()" will give you an immediate
listing of all the instances where a connection is established?

Finally, you can turn pooling off by passing in "Pooling=false" in the
connection string. This would mean every new connection that is created is
set up as a seperate connection, regardless if one (or hundred) just like it
have already been used.

http://msdn.microsoft.com/library/de...taprovider.asp

If you employ proper connection maintenance from the start, it won't be a
problem...
"Eric" <wa**********@msn.com> wrote in message
news:uZ**************@TK2MSFTNGP09.phx.gbl...
Thank you for your reply.

SqlClient.SqlConnection should manually call Close() or Dispose() after
using. MSDN point out this. If we didn't call Close() or Dispose()
manually,
SqlConnecont won't be closed when the connection object is out of range,
GC
would close the connection object in some magic time. That means, if we
don't call Close() or Dispose() as soon as possible, the connection_pool
would be exhausted.

What's the worse, the connection_pool is organized by *process* and
*ConnectionString*. This means diffrent modules in one program, would
share
a same connection_pool. If one connection_pool was exhausted, we can not
even find out which module cause this problem. In a huge project, this
will
be a very troublesome problem, I think.


"Dan =o)" <danielbass [at] postmaster [dot] co [dot] uk> дÈëÓʼþ
news:ub*************@TK2MSFTNGP11.phx.gbl...
Eric,

The System.Data.SqlClient library is a pure .Net client interface to the
database. Therefore if the connection is not closed, there is the timeout
and garbage collector to sort it out if someone doesn't do the job of
closing it down manually.

What type of unmanaged resources are you refering too?

If the managed code is hooked into native code via the Interop's, then
the
garbage collector will again call the destructors on your unmanaged code
when it thinks you're finished with them during a clear up.

Therefore if the unmanaged code is not desroying its objects, the garbage
collector destroy these uncleared objects either as the onus lies with
the
unmanaged resource and not the .Net application.
"Eric" <wa**********@msn.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
> for example: SqlConnection is used in my project, how can I know if all
> connections were closed after open and execution. If some guys forget
> to
> close connections after using, how can i check it out ?
>
>
> best,
> eric
>
>
>



Nov 16 '05 #4
Let's have a test to find out what would happen, if we don't manually call
Dispose() or such like that :
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace PureTest
{
public class Class1
{
public Class1()
{
}

[STAThread]
static void Main()
{
while(true)
{
int i = 0;
try
{
for(i = 0; i < 1000; i++)
{
SqlConnection con = new
SqlConnection("server=ERIC;uid=eric;pwd=eric;datab ase=settlement");
con.Open();
}
}
catch(Exception ee)
{
MessageBox.Show("count=" + i.ToString() + "\r\n");

if(MessageBox.Show(ee.Message + "\r\n\r\ncontinue?", "connect fail",
MessageBoxButtons.YesNo) == DialogResult.No)
{
break;
}

}
}
}
}
}
We will see that connection_pool keep exhausted. This sample looks stupid,
but similar situation can really happen in a complex project.

Not_using connection pool, I don't think this is a good idea. Connecting is
an expensive operation, and Real_Connection to SQLServer is also limited.

best,
eric


"Dan =o)" <danielbass [at] postmaster [dot] co [dot] uk> дÈëÓʼþ
news:OB**************@TK2MSFTNGP12.phx.gbl...
Firstly, I'm not sure why this is a problem... The pooling technique is
offered as a default so that the overhead is kept down when two connections "appear" to be the same in the same process. This saves memory and resources in general. If the connection times out because the "Connection Lifttime"
has expired, the the GC removes the pool from memory...

It's not a case of should, you "must" call Close()/Dispose()... Although
it's not as imperative as, calling "Open()" say, the fact is, failing to
call close leaves this connection wide open, and if you've numerous
different open connections, this in itself saps system resources.

Surely a simply find in the solution on "Open()" will give you an immediate listing of all the instances where a connection is established?

Finally, you can turn pooling off by passing in "Pooling=false" in the
connection string. This would mean every new connection that is created is
set up as a seperate connection, regardless if one (or hundred) just like it have already been used.

http://msdn.microsoft.com/library/de...taprovider.asp
If you employ proper connection maintenance from the start, it won't be a
problem...
"Eric" <wa**********@msn.com> wrote in message
news:uZ**************@TK2MSFTNGP09.phx.gbl...
Thank you for your reply.

SqlClient.SqlConnection should manually call Close() or Dispose() after
using. MSDN point out this. If we didn't call Close() or Dispose()
manually,
SqlConnecont won't be closed when the connection object is out of range,
GC
would close the connection object in some magic time. That means, if we
don't call Close() or Dispose() as soon as possible, the connection_pool
would be exhausted.

What's the worse, the connection_pool is organized by *process* and
*ConnectionString*. This means diffrent modules in one program, would
share
a same connection_pool. If one connection_pool was exhausted, we can not
even find out which module cause this problem. In a huge project, this
will
be a very troublesome problem, I think.


"Dan =o)" <danielbass [at] postmaster [dot] co [dot] uk> дÈëÓʼþ
news:ub*************@TK2MSFTNGP11.phx.gbl...
Eric,

The System.Data.SqlClient library is a pure .Net client interface to the database. Therefore if the connection is not closed, there is the timeout and garbage collector to sort it out if someone doesn't do the job of
closing it down manually.

What type of unmanaged resources are you refering too?

If the managed code is hooked into native code via the Interop's, then
the
garbage collector will again call the destructors on your unmanaged code when it thinks you're finished with them during a clear up.

Therefore if the unmanaged code is not desroying its objects, the garbage collector destroy these uncleared objects either as the onus lies with
the
unmanaged resource and not the .Net application.
"Eric" <wa**********@msn.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
> for example: SqlConnection is used in my project, how can I know if all > connections were closed after open and execution. If some guys forget
> to
> close connections after using, how can i check it out ?
>
>
> best,
> eric
>
>
>



Nov 16 '05 #5
Eric,

Like I said, it's not right if you don't call close, I don't really see what
other option you have on a large scale project. If you have the source, then
a simply match on Open/Close calls will do.

"Eric" <wa**********@msn.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Let's have a test to find out what would happen, if we don't manually call
Dispose() or such like that :
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace PureTest
{
public class Class1
{
public Class1()
{
}

[STAThread]
static void Main()
{
while(true)
{
int i = 0;
try
{
for(i = 0; i < 1000; i++)
{
SqlConnection con = new
SqlConnection("server=ERIC;uid=eric;pwd=eric;datab ase=settlement");
con.Open();
}
}
catch(Exception ee)
{
MessageBox.Show("count=" + i.ToString() + "\r\n");

if(MessageBox.Show(ee.Message + "\r\n\r\ncontinue?", "connect fail",
MessageBoxButtons.YesNo) == DialogResult.No)
{
break;
}

}
}
}
}
}
We will see that connection_pool keep exhausted. This sample looks stupid,
but similar situation can really happen in a complex project.

Not_using connection pool, I don't think this is a good idea. Connecting
is
an expensive operation, and Real_Connection to SQLServer is also limited.

best,
eric


"Dan =o)" <danielbass [at] postmaster [dot] co [dot] uk> дÈëÓʼþ
news:OB**************@TK2MSFTNGP12.phx.gbl...
Firstly, I'm not sure why this is a problem... The pooling technique is
offered as a default so that the overhead is kept down when two

connections
"appear" to be the same in the same process. This saves memory and

resources
in general. If the connection times out because the "Connection Lifttime"
has expired, the the GC removes the pool from memory...

It's not a case of should, you "must" call Close()/Dispose()... Although
it's not as imperative as, calling "Open()" say, the fact is, failing to
call close leaves this connection wide open, and if you've numerous
different open connections, this in itself saps system resources.

Surely a simply find in the solution on "Open()" will give you an

immediate
listing of all the instances where a connection is established?

Finally, you can turn pooling off by passing in "Pooling=false" in the
connection string. This would mean every new connection that is created
is
set up as a seperate connection, regardless if one (or hundred) just like

it
have already been used.

http://msdn.microsoft.com/library/de...taprovider.asp

If you employ proper connection maintenance from the start, it won't be a
problem...
"Eric" <wa**********@msn.com> wrote in message
news:uZ**************@TK2MSFTNGP09.phx.gbl...
> Thank you for your reply.
>
> SqlClient.SqlConnection should manually call Close() or Dispose() after
> using. MSDN point out this. If we didn't call Close() or Dispose()
> manually,
> SqlConnecont won't be closed when the connection object is out of
> range,
> GC
> would close the connection object in some magic time. That means, if we
> don't call Close() or Dispose() as soon as possible, the
> connection_pool
> would be exhausted.
>
> What's the worse, the connection_pool is organized by *process* and
> *ConnectionString*. This means diffrent modules in one program, would
> share
> a same connection_pool. If one connection_pool was exhausted, we can
> not
> even find out which module cause this problem. In a huge project, this
> will
> be a very troublesome problem, I think.
>
>
>
>
> "Dan =o)" <danielbass [at] postmaster [dot] co [dot] uk> дÈëÓʼþ
> news:ub*************@TK2MSFTNGP11.phx.gbl...
>> Eric,
>>
>> The System.Data.SqlClient library is a pure .Net client interface to the >> database. Therefore if the connection is not closed, there is the timeout >> and garbage collector to sort it out if someone doesn't do the job of
>> closing it down manually.
>>
>> What type of unmanaged resources are you refering too?
>>
>> If the managed code is hooked into native code via the Interop's, then
>> the
>> garbage collector will again call the destructors on your unmanaged code >> when it thinks you're finished with them during a clear up.
>>
>> Therefore if the unmanaged code is not desroying its objects, the garbage >> collector destroy these uncleared objects either as the onus lies with
>> the
>> unmanaged resource and not the .Net application.
>>
>>
>> "Eric" <wa**********@msn.com> wrote in message
>> news:%2****************@TK2MSFTNGP12.phx.gbl...
>> > for example: SqlConnection is used in my project, how can I know if all >> > connections were closed after open and execution. If some guys
>> > forget
>> > to
>> > close connections after using, how can i check it out ?
>> >
>> >
>> > best,
>> > eric
>> >
>> >
>> >
>>
>>
>
>



Nov 16 '05 #6
Dan,

Your comments is helpful, but I have some diffrent viewpoints.

A simply match on Open/Close won't be enough for Resource Safe in dotNET, as
that a simply match on new/delete is not enough for Memory Safe in C++.

To my opinion, Resource Safe is also very important for a business system.
Without Resource Safe, you are unable to say when the system will break
down. Yes, GC maybe help in some situation, but the system may have broken
down before GC.Collect().

So, we have to exactly manually open and free every system resource,
obviating the disturbance of Exceptions. I feel it's even more boring than
keep Memory Safe in C++.

Sorry for the rant.

best,
eric

"Dan =o)" <danielbass [at] postmaster [dot] co [dot] uk> дÈëÓʼþ
news:eA**************@tk2msftngp13.phx.gbl...
Eric,

Like I said, it's not right if you don't call close, I don't really see what other option you have on a large scale project. If you have the source, then a simply match on Open/Close calls will do.

"Eric" <wa**********@msn.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Let's have a test to find out what would happen, if we don't manually call Dispose() or such like that :
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace PureTest
{
public class Class1
{
public Class1()
{
}

[STAThread]
static void Main()
{
while(true)
{
int i = 0;
try
{
for(i = 0; i < 1000; i++)
{
SqlConnection con = new
SqlConnection("server=ERIC;uid=eric;pwd=eric;datab ase=settlement");
con.Open();
}
}
catch(Exception ee)
{
MessageBox.Show("count=" + i.ToString() + "\r\n");

if(MessageBox.Show(ee.Message + "\r\n\r\ncontinue?", "connect fail",
MessageBoxButtons.YesNo) == DialogResult.No)
{
break;
}

}
}
}
}
}
We will see that connection_pool keep exhausted. This sample looks stupid, but similar situation can really happen in a complex project.

Not_using connection pool, I don't think this is a good idea. Connecting
is
an expensive operation, and Real_Connection to SQLServer is also limited.
best,
eric


"Dan =o)" <danielbass [at] postmaster [dot] co [dot] uk> дÈëÓʼþ
news:OB**************@TK2MSFTNGP12.phx.gbl...
Firstly, I'm not sure why this is a problem... The pooling technique is
offered as a default so that the overhead is kept down when two

connections
"appear" to be the same in the same process. This saves memory and

resources
in general. If the connection times out because the "Connection Lifttime" has expired, the the GC removes the pool from memory...

It's not a case of should, you "must" call Close()/Dispose()... Although it's not as imperative as, calling "Open()" say, the fact is, failing to call close leaves this connection wide open, and if you've numerous
different open connections, this in itself saps system resources.

Surely a simply find in the solution on "Open()" will give you an

immediate
listing of all the instances where a connection is established?

Finally, you can turn pooling off by passing in "Pooling=false" in the
connection string. This would mean every new connection that is created
is
set up as a seperate connection, regardless if one (or hundred) just like
it
have already been used.

http://msdn.microsoft.com/library/de...taprovider.asp
If you employ proper connection maintenance from the start, it won't be a problem...
"Eric" <wa**********@msn.com> wrote in message
news:uZ**************@TK2MSFTNGP09.phx.gbl...
> Thank you for your reply.
>
> SqlClient.SqlConnection should manually call Close() or Dispose() after > using. MSDN point out this. If we didn't call Close() or Dispose()
> manually,
> SqlConnecont won't be closed when the connection object is out of
> range,
> GC
> would close the connection object in some magic time. That means, if we > don't call Close() or Dispose() as soon as possible, the
> connection_pool
> would be exhausted.
>
> What's the worse, the connection_pool is organized by *process* and
> *ConnectionString*. This means diffrent modules in one program, would
> share
> a same connection_pool. If one connection_pool was exhausted, we can
> not
> even find out which module cause this problem. In a huge project, this > will
> be a very troublesome problem, I think.
>
>
>
>
> "Dan =o)" <danielbass [at] postmaster [dot] co [dot] uk> дÈëÓʼþ
> news:ub*************@TK2MSFTNGP11.phx.gbl...
>> Eric,
>>
>> The System.Data.SqlClient library is a pure .Net client interface to

the
>> database. Therefore if the connection is not closed, there is the

timeout
>> and garbage collector to sort it out if someone doesn't do the job of >> closing it down manually.
>>
>> What type of unmanaged resources are you refering too?
>>
>> If the managed code is hooked into native code via the Interop's, then >> the
>> garbage collector will again call the destructors on your unmanaged

code
>> when it thinks you're finished with them during a clear up.
>>
>> Therefore if the unmanaged code is not desroying its objects, the

garbage
>> collector destroy these uncleared objects either as the onus lies with >> the
>> unmanaged resource and not the .Net application.
>>
>>
>> "Eric" <wa**********@msn.com> wrote in message
>> news:%2****************@TK2MSFTNGP12.phx.gbl...
>> > for example: SqlConnection is used in my project, how can I know

if all
>> > connections were closed after open and execution. If some guys
>> > forget
>> > to
>> > close connections after using, how can i check it out ?
>> >
>> >
>> > best,
>> > eric
>> >
>> >
>> >
>>
>>
>
>



Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mart Rogers | last post by:
We have been trying to avoid switches to unmanaged code in our development, but have recently folded in a call to CDO by using System.Web.Mail as a quick way to send email. Yes this is a call to...
2
by: Marek Malowidzki | last post by:
Hi all, I am writing a component that exposes a C++ library as a .NET component. The approach is somewhat automatic: every library C++ class has its managed C++ counterpart that keeps a pointer...
5
by: Barry Anderberg | last post by:
I'm using a tool by Sci-Tech called the .NET Memory Profiler. We have a massive .NET/C# application here and it has been exhibiting memory leak behavior for some time. Attempting to remedy the...
4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
6
by: William F. Kinsley | last post by:
I am thinking of porting an existing MFC application to MC++ and I have created a simple MFC application to test the environment. My sample MFC application is compilied with the /clr switch. I...
3
by: Steve | last post by:
I have former VC++ 6.0 application which was ported to .NET. Now we continue to develop this app under VS.NET. The app now contains both managed and unmanaged classes. Now I have a problem with...
4
by: Maxwell | last post by:
Hello, Newbie question here for disposing of unmanaged resources in MC++.NET. I have a managed VS.NET 2003 MC++ wrapper class that wraps a unmanaged C++ dll. What I am trying to figure out is...
4
by: PromisedOyster | last post by:
There are various contradictory newsgroup postings on this issue, but I would really like a definitive answer from the .NET gurus out there? We have various WinForms that contain multiple Icons...
1
by: George2 | last post by:
Hello everyone, I think unmanaged resource means the resources (e.g. memory and file handler) which is used directly (new, FILE*) other than using a wrapper class (Resource Acquisition Is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.