I have WCF service hosted in a window service.I was observing some handle leaks by this service.On investigating i was able to find that the reason for the leak are the DB operation performed on *.accdb(MS acsess) file.To rule out the other possiblities(with WCF etc.) i have created a test Window service in c# and in its Onstart method I have started a thread in which I call Open and close on the database and i still see the token leak. The service is runing is Local System account and the db fiel is present in the local computer.The open and close are sucessful. Three or four tokens of the given type gets generated every time i call Open on the database and count doesnot reduce on close.So they keep on increasing.When i run the service as the logged on user i dont see the meory leak
protected override void OnStart(string[] args)
{
ThreadStart t = new ThreadStart(DBConenctionThread);
Thread t2 = new Thread(t);
t2.Start();
}
void DBConenctionThread()
{
bool b = true;
String s = @"Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=D:\testdatabase\tempelate2.accdb;Uid=;Pwd=;";
while (b)
{
try
{
using (OdbcConnection dbConnection = new OdbcConnection(s))
{
dbConnection.Open();
dbConnection.Close();
}
}
catch (NotSupportedException ex)
{
}
catch (Exception ex)
{
}
}
}
These are the details for the token being leaked from the process explorer
Name : NT AUTHORITY\SYSTEM:3e7 Type : token Description : identifies a security context.
I have very similar problem. All those tokens are in finalizer queue in GC ready to be finalized when finalization thread start. But because our service entry point was marked as [STAThread], finalization thread had never started. When we change attribute to MTAThread those token ware successfully reclaimed by GC. You my dot'n have same problem but you can check if finalization thread ever start by putting GC.WaitForPendingFinalizers() on the end of your code.