StackOverflowException

StackOverflowException

richard.schaffranek
Enthusiast Enthusiast
1,270 Views
9 Replies
Message 1 of 10

StackOverflowException

richard.schaffranek
Enthusiast
Enthusiast

Dear all,

I have this funny error I would like to track the visiblity changes in REVIT View. For this resons I subscribed to two events:
1. I subscribe to the ViewActivated event each time this event is called I create a new FilterdElementCollector.

2. In the Idling event I use this FilteredelementCollector to get all ElementIds;

 

This randomly produces System.StackOverflowExcpetion ... now I am a little lost in Debugging this issue, please help?

   // getting the active view when changing the view will make things faster when checking visiblity
        private void ViewActivated(object sender, ViewActivatedEventArgs e)
        {
            if (App.CurrentApp is null || App.CurrentApp.ActiveUIDocument is null) return;
            Document doc = App.CurrentApp.ActiveUIDocument.Document;
            this.ActiveViewElementCollector = new FilteredElementCollector(doc, doc.ActiveView.Id);
        }
        private bool VisiblityChanged()
        {
            if (ActiveViewElementCollector is null) return false;
            //   ICollection<ElementId> filter = new FilteredElementCollector(doc, doc.ActiveView.Id).ToElementIds();
            ICollection<ElementId> filter = this.ActiveViewElementCollector.ToElementIds();
            // einzige option hashset funktionen nutzen um event schneller zu machen...
            HashSet<ElementId> visiblity = new HashSet<ElementId>(filter, new SortedElementId());

            if (visiblity.Count != oldVisibility.Count || !oldVisibility.SetEquals(visiblity)) {
                oldVisibility = visiblity;
                return true;
            }

            return false;
        }

 

Accepted solutions (1)
1,271 Views
9 Replies
Replies (9)
Message 2 of 10

caroline.gitonga
Autodesk
Autodesk

Hi @richard.schaffranek,

I see this as more of a .NET effect rather than Revit's.  There seems to be infinite recursion/ call/loop that does not terminate, hence, throwing the StackOverflowException. I cant fully tell from the code segment you have provided maybe you could provide a bit more of a full minimum reproducible case just to understand your implementation.

Carol Gitonga, Developer Advocacy and Support, ADN Open
Message 3 of 10

richard.schaffranek
Enthusiast
Enthusiast

If the case would be easily reproduceable I would provide it as I said it randomly happens and it is the function call:

ICollection<ElementId> filter = this.ActiveViewElementCollector.ToElementIds();

debugger tells me that the stack overflow exception results from  RevitDBAPI.dll

 "I see this as more of a .NET effect rather than Revit's. " sorry I can't . We had a looked into the dll and it seams that the Methode toElementIds is just a wrapper calling a c++ methode, so we assume that the error is there....


Message 4 of 10

caroline.gitonga
Autodesk
Autodesk
A complete testcase will be valuable to be able to reproduce this on our end. Also a journal file leading to this exception.
For confidential information in the testcase and the journal file you send via private message.
Carol Gitonga, Developer Advocacy and Support, ADN Open
Message 5 of 10

RPTHOMAS108
Mentor
Mentor

It is likely occurring due to the combination of events you are subscribing to, specifically how one triggers the other or perhaps idling event alone (that is often the culprit).  System.StackOverflowExcpetion is generally something created on the managed side i.e. it is a managed exception referring to the managed stack. 

 

To get better help you would need to indicate how you are handling and subscribing to the idling event (e.g. set raise without delay yes/no)? Probably when you provide the full code regarding subscription to events etc. and what 'this' refers to (in terms of an overall class structure) there will be better suggestions available. That exception is perhaps not easy to debug but generally easy to fix since it would have occurred due to some kind of infinite loop or recursion.

 

It is probably not a good idea to do much in the handler if you've set the idling event to continuously fire. In such a scenario you should build in a quick exit clause for an unchanged state occurrence. Continuously collecting elements is not an example of that.

 

 

Message 6 of 10

richard.schaffranek
Enthusiast
Enthusiast

@RPTHOMAS108 
this / events is the main app class for the plugin 

 

internal class App : IExternalApplication{
  ....
  private FilteredElementCollector ActiveViewElementCollector = null;
  ....

  public Result OnStartup(UIControlledApplication uicApp){
    ...
    uicApp.Idling += IdlingHandler;
    uicApp.ViewActivated += ViewActivated;
    ...
  }

  private void IdlingHandler(object sender, IdlingEventArgs e){
    ...
    if (SelectionChanged()) {
      ...
    }

    if (VisiblityChanged()) {
       ...   
    }
  }

  public Result OnShutdown(UIControlledApplication a){
    a.Idling -= IdlingHandler;
    a.ViewActivated -= ViewActivated;
    return Result.Succeeded;
  }
}

 


@"In such a scenario you should build in a quick exit clause for an unchanged state occurrence. "

Well I'd love to have a quick exit but I couldn't come up with a better solution than the one posted before. If you have any ideas than I would be more than happy. Funny thing is if I don't reuse the same Filtered Element Collector it works, however I wanted to optimize the code and reuse the Filtered Element Collector since it says in the documentation:

"The first time your code constructs a collector for a given view, or the first time your code constructs a collector for a view whose display settings have just been changed, you may experience a significant performance degradation."

Message 7 of 10

jeremy_tammik
Alumni
Alumni

I agree with @RPTHOMAS108 ... tricky combination of events... can we take a step back and try to understand exactly what you are trying to achieve? The best solution is often the simplest; maybe your approach can be simplified?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 8 of 10

richard.schaffranek
Enthusiast
Enthusiast

@jeremy_tammik 

The plugin is displaying reports about the elements in the REVIT drawing. Each report is linked to one element or multiple element. Aim is to show which reports are linked to elements currently visible / selected.

I have seen that there is a new event in Revit 2023 which is called selectionChanged, which i intend to use in the future, however the plugin should also work in 2019 (currently) so the described solution was the best I could come up with. Any suggestions how to do this differently are welcomed...

Message 9 of 10

jeremy_tammik
Alumni
Alumni
Accepted solution

Thank you for the explanation. I understand your original description and question better now.

  

Some observations:

  

In the ViewActivated event, I would null out the active filtered element collector if the app or the doc is invalid. Maybe I would not even create the filtered element collector, just save the current view itself.

  

In VisibilityChanged, if you have the current view, you could check that it still matches the active doc as well. You could create e new filtered element collector right there and then. I see no reason for storing the element collector and passing it from one event handler to the other. Maybe that is causing a problem, some kind of mismatch. On the other hand, your commented-out code seems to indicate that you already tried and discarded that approach.

  

On the other hand, you seem to hint that you discarded that because of the performance warning you quote. However, that warning only refers to the first time... so, why avoid recreating the filtered element collector later on, after the first creation? I think the performance degradation occurs mostly for views that have not yet been displayed at all and therefore need to be regenerated from scratch. Afaict, that is never the case in your situation. So, why no just recreate the FEC right then and there when you need it? You say that fixes the problem, don't you?

  

Do you think the new SelectionChanged event will help? So, do you just need selected elements, or all elements visible in the current view?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 10 of 10

richard.schaffranek
Enthusiast
Enthusiast

@jeremy_tammik 

thanks for clarification on the preformance of FEC, so I will stick to my original version.

The selection Changed Event will definitely help since then I don't have to track the selection but can collect elements once a selection changes. However I will still need the visibility change detection in the ideling event, and that by fare is the more costly so it would be create If we could also get a visiblity changed event in the future ...