During static code analysis for rule "CA2000: Dispose objects before losing scope"
failed on use of SqlCacheDependency.
Say for method that adds SQlDependency for caching on change, in some tables.
/*
Method start
*/
AggregateCacheDependency DependencyToCache = new AggregateCacheDependency();
try
{
SqlCacheDependency SQLDependencyObj = new SqlCacheDependency(/*DB NAme*/, /*Tables*/);
DependencyToCache.Add(SQLDependencyObj);
}
catch (Exception E)
{
throw new MyExceptionType(/*--Some args--*/);
}
/*
Insert Cache
Method End
*/
As mentioned in MSDN article SqlCacheDependency.Dispose is inherited from CacheDependency and CacheDependency.Dispose() "Releases the resources used by the CacheDependency object."
Well, is it require to dispose SqlCacheDependencyobject? Does it holds any unmanaged resource? if yes, what are those?
Lastly, how can i resolve the error generated by VS2010 static code analyser for this senario.
ERROR in above senario: CA2000 : Microsoft.Reliability : In method 'RGSCache.GetTemplateSectionZones(int)', object 'SQLDependencyObj' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'SQLDependencyObj' before all references to it are out of scope.
Thanks and Regards,
Spydaios
http://spydaios.blogspot.com ________________________________________
He is able who thinks he is able.
- Lord Buddha
To fix a violation of this rule, call Dispose on the object before all references to it are out of scope.
Note that you can use the using statement (Using in Visual Basic) to wrap objects that implement IDisposable. Objects that are wrapped
in this manner will automatically be disposed at the close of the using block.
The following are some situations where the using statement is not enough to protect IDisposable objects and can cause CA2000 to occur.
Returning a disposable object requires that the object is constructed in a try/finally block outside a using block.
Initializing members of a disposable object should not be done in the constructor of a using statement.
Nesting constructors that are protected only by one exception handler.
Member
105 Points
103 Posts
C# SqlCacheDependency Dispose for static code analysis rule CA2000
Feb 03, 2012 07:23 AM|Spydaios|LINK
Hi,
During static code analysis for rule "CA2000: Dispose objects before losing scope"
failed on use of SqlCacheDependency.
Say for method that adds SQlDependency for caching on change, in some tables.
Spydaios
http://spydaios.blogspot.com
________________________________________
He is able who thinks he is able.
- Lord Buddha
All-Star
94120 Points
18111 Posts
Re: C# SqlCacheDependency Dispose for static code analysis rule CA2000
Feb 04, 2012 09:08 PM|Decker Dong - MSFT|LINK
To fix a violation of this rule, call Dispose on the object before all references to it are out of scope.
Note that you can use the using statement (Using in Visual Basic) to wrap objects that implement IDisposable. Objects that are wrapped in this manner will automatically be disposed at the close of the using block.
The following are some situations where the using statement is not enough to protect IDisposable objects and can cause CA2000 to occur.
Returning a disposable object requires that the object is constructed in a try/finally block outside a using block.
Initializing members of a disposable object should not be done in the constructor of a using statement.
Nesting constructors that are protected only by one exception handler.
For more you can see this:http://msdn.microsoft.com/en-us/library/ms182289.aspx
Member
105 Points
103 Posts
Re: C# SqlCacheDependency Dispose for static code analysis rule CA2000
Feb 06, 2012 01:24 AM|Spydaios|LINK
Thanks Decker...
That info is really helpful.
But another question earlier mentioned still remains.
Unlike DataSets and DataTables, does SQLCacheDependency holds unmanaged resources?
Coz, even DataTables and DataSets are Disposable they inherit .Dispose() from Object class.
I don't call Dispose on DataTables or DataSets.
But have no idea whether SQLCacheDependency holds any unmanaged resources so that we need to call .Dispose()
Spydaios
http://spydaios.blogspot.com
________________________________________
He is able who thinks he is able.
- Lord Buddha
All-Star
94120 Points
18111 Posts
Re: C# SqlCacheDependency Dispose for static code analysis rule CA2000
Feb 06, 2012 01:39 AM|Decker Dong - MSFT|LINK
I suggest you——
using (SQLCacheDependency sc = new SQLCacheDependency ())
{
………………
}
This will automatically call Dispose to release things out。
Member
105 Points
103 Posts
Re: C# SqlCacheDependency Dispose for static code analysis rule CA2000
Feb 06, 2012 06:09 AM|Spydaios|LINK
You are write,
One should use "Using" for disposable object if it not to be returned from method and to be instantiated in that menthod only.
It would be most feasible way to de stuff, disregard of the way Dispose pattern for Disposable objects changes in future.
I will take your advise on this and go forward with use of "using".
Spydaios
http://spydaios.blogspot.com
________________________________________
He is able who thinks he is able.
- Lord Buddha