Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional breakpoint not working in Visual Studio 2015

I have an issue debugging my C# code in Visual Studio 2015.

I want to add a simple expression into a breakpoint,

So I added hierarchyRelation != null as condition. That is a local variable of the method I am debugging, and it exists.

However, in runtime I get the following error

"The condition for a breakpoint failed to execute. The condition was "hierarchyRelation != null". The error returned was "The breakpoint condition must evaluate to a boolean operation". Click OK to stop at this breakpoint.

Actually, the condition was more complex, but this is the simplest case that reproduces the problem. I tried variants, and even comparing properties of this variable and it always fails the same.

If I try a constant condition, like 1 != 2 or 1 = 1 it works fine. Is there any issue ? The closest related question I found was this, but it was in vb code. Its solution was to add a debug method directly in the code. Although I can do that, I want to know why is this not working.

The method code

private HierarchyNodeDto GetNodeTreeThatContainsText<TRollup, TLeaf, THierarchyRelation>(HierarchyNodeDto root, string text, PreFilter preFilter, Func<TLeaf, bool> leafContainsTextFunc, bool parentContainsText) where TRollup: HierarchyNodeDto where TLeaf: HierarchyNodeDto {
            dynamic rootNode = root as TRollup;
            if (rootNode != null) {
                if (rootNode.Nodes == null) {
                    return null;
                }
                var childNodesWithText = new List<THierarchyRelation>();
                foreach (var hierarchyRelation in rootNode.Nodes) {
                    var isLeaf = hierarchyRelation.Node.GetType() == typeof(TransactionTypeHierarchyLeafDto) || hierarchyRelation.Node.GetType() == typeof(AccountHierarchyLeafDto);
                    if (!isLeaf && hierarchyRelation.Node.Name != null && hierarchyRelation.Node.Name.ToLower().Contains(text) && preFilter != PreFilter.Leafs) {
                        childNodesWithText.Add(hierarchyRelation);
                        continue;
                    }
                    var subtreeThatContainsText = this.GetNodeTreeThatContainsText<TRollup, TLeaf, THierarchyRelation>(hierarchyRelation.Node, text, preFilter, leafContainsTextFunc, rootNode.Name.ToLower().Contains(text));
                    if (subtreeThatContainsText == null) {
                        continue;
                    }
                    hierarchyRelation.Node = subtreeThatContainsText;
                    childNodesWithText.Add(hierarchyRelation);
                }
                rootNode.Nodes = childNodesWithText;
                if (rootNode.Nodes.Count > 0 || (rootNode.Name.ToLower().Contains(text) && preFilter != PreFilter.Leafs)) {
                    return rootNode;
                }
                return null;
            }
            var rootLeaf = root as TLeaf;

            return rootLeaf != null && ((leafContainsTextFunc.Invoke(rootLeaf) && preFilter != PreFilter.Nodes) || (parentContainsText && preFilter != PreFilter.Leafs)) ? rootLeaf : null;
        }

I am adding the breakpoint in the first line inside the foreach

enter image description here

like image 662
Gonzalo.- Avatar asked Oct 05 '17 13:10

Gonzalo.-


People also ask

Why is my breakpoint not working?

If a source file has changed and the source no longer matches the code you're debugging, the debugger won't set breakpoints in the code by default. Normally, this problem happens when a source file is changed, but the source code wasn't rebuilt. To fix this issue, rebuild the project.

How do I apply conditional breakpoints in Visual Studio?

Right-click the breakpoint symbol and select Conditions (or press Alt + F9, C). Or hover over the breakpoint symbol, select the Settings icon, and then select Conditions in the Breakpoint Settings window.

Why does Visual Studio not stop at breakpoint?

This problem occurs because ASP.NET debugging isn't enabled on the application.


2 Answers

A bit late to the party but anyway, I hit the same problem like this as well, with a dynamic object too.

What I did to solve this was to cast it as an object in the breakpoint's conditional expression itself; so in your case, doing the following would work:

(object)hierarchyRelation!=null

Give it a go and see if that works out for you.

like image 142
kileak Avatar answered Sep 20 '22 00:09

kileak


The issue is that the hierarchyRelation is a dynamic variable although I'm not totally sure why. According to Expressions in the Debugger it should work (I couldn't find a reason why it shouldn't) .

    static void Main(string[] args)
    {
        dynamic foo = new Foo();

        // conditional breakpoint 'foo.Nodes == null' here
    }

    internal class Foo
    {
        public IEnumerable<Foo> Nodes = null;
    }

This code triggers the same exception whenever the debugger passes and evaluates the conditional breakpoint. Statically typing the foo variable will make the debugger able to evaluate the expression and break whenever needed.

like image 31
Jevgeni Geurtsen Avatar answered Sep 19 '22 00:09

Jevgeni Geurtsen