locked
C# SqlCacheDependency Dispose for static code analysis rule CA2000 RRS feed

  • Question

  • User33725395 posted

    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.

    /*
    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.


    Friday, February 3, 2012 7:23 AM

Answers

  • User3866881 posted

    But have no idea whether SQLCacheDependency holds any unmanaged resources so that we need to call .Dispose()

    I suggest you——

    using (SQLCacheDependency sc = new SQLCacheDependency ())
    {
       ………………

    This will automatically call Dispose to release things out。

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 6, 2012 1:39 AM

All replies

  • User3866881 posted

    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

    Saturday, February 4, 2012 9:08 PM
  • User33725395 posted

    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()

    Monday, February 6, 2012 1:24 AM
  • User3866881 posted

    But have no idea whether SQLCacheDependency holds any unmanaged resources so that we need to call .Dispose()

    I suggest you——

    using (SQLCacheDependency sc = new SQLCacheDependency ())
    {
       ………………

    This will automatically call Dispose to release things out。

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 6, 2012 1:39 AM
  • User33725395 posted

    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".

    Monday, February 6, 2012 6:09 AM