Example #1
0
            public AnalysisContext(CSharpExplodedGraph explodedGraph)
            {
                this.objectDisposedPointerCheck = new ObjectDisposedPointerCheck(explodedGraph);
                this.objectDisposedPointerCheck.ObjectDisposed += ObjectDisposedHandler;

                explodedGraph.AddExplodedGraphCheck(this.objectDisposedPointerCheck);
            }
        private static void CheckForMultipleDispose(CSharpExplodedGraph explodedGraph, SyntaxNodeAnalysisContext context)
        {
            var objectDisposedCheck = new ObjectDisposedPointerCheck(explodedGraph);

            explodedGraph.AddExplodedGraphCheck(objectDisposedCheck);

            // Store the nodes that should be reported and ignore duplicate reports for the same node.
            // This is needed because we generate two CFG blocks for the finally statements and even
            // though the syntax nodes are the same, when there is a return inside a try/catch block
            // the walked CFG paths could be different and FPs will appear.
            var nodesToReport = new Dictionary <SyntaxNode, string>();

            void memberAccessedHandler(object sender, ObjectDisposedEventArgs args)
            {
                nodesToReport[args.SyntaxNode] = args.SymbolName;
            }

            objectDisposedCheck.ObjectDisposed += memberAccessedHandler;

            try
            {
                explodedGraph.Walk();
            }
            finally
            {
                objectDisposedCheck.ObjectDisposed -= memberAccessedHandler;
            }

            foreach (var item in nodesToReport)
            {
                context.ReportDiagnosticWhenActive(Diagnostic.Create(rule, item.Key.GetLocation(), item.Value));
            }
        }
Example #3
0
        private static void CheckForMultipleDispose(CSharpExplodedGraph explodedGraph, SyntaxNodeAnalysisContext context)
        {
            var objectDisposedCheck = new ObjectDisposedPointerCheck(explodedGraph);

            explodedGraph.AddExplodedGraphCheck(objectDisposedCheck);

            void memberAccessedHandler(object sender, ObjectDisposedEventArgs args)
            {
                context.ReportDiagnosticWhenActive(
                    Diagnostic.Create(rule, args.Location, args.Name));
            }

            objectDisposedCheck.ObjectDisposed += memberAccessedHandler;

            try
            {
                explodedGraph.Walk();
            }
            finally
            {
                objectDisposedCheck.ObjectDisposed -= memberAccessedHandler;
            }
        }