Revit 2025.4 is crashing if I use results of FilteredElementCollector

Revit 2025.4 is crashing if I use results of FilteredElementCollector

sriram_rajagopal07
Participant Participant
308 Views
8 Replies
Message 1 of 9

Revit 2025.4 is crashing if I use results of FilteredElementCollector

sriram_rajagopal07
Participant
Participant

When using a Filtered Element Collector to get elements from a linked model, everything works perfectly when the same code is executed through the Add-In Manager.




However, when I run it in debug mode (from Visual Studio) or after installing the add-in normally, Revit throws this error and crashes:

System.AccessViolationException: Attempted to read or write protected memory.

sriram_rajagopal07_0-1762931896034.gif

 

Here’s the core part of the code:

FilteredElementCollector fec = new FilteredElementCollector(
    doc,
    choosenView.Id,
    revitLinkInstance.Id
);

List<Element> elmns = new List<Element>(
    fec.WhereElementIsNotElementType().ToElement()
);

 



 

 

0 Likes
Accepted solutions (1)
309 Views
8 Replies
Replies (8)
Message 2 of 9

ricaun
Advisor
Advisor

Never used this FilteredElementCollector(Document hostDocument, ElementId viewId, ElementId linkId) and looks like was added in Revit 2024.

 

That's fun, I was not able to make work in any version. And is a little strange that works in your Add-In Manager works for some reason...

 

I tried in Revit 2024 and I have a different exception incited of the AccessViolationException with Revit crash.

Autodesk.Revit.Exceptions.InternalException: A managed exception was thrown by Revit or by one of its external applications.

 

I create this unit test and always fails, and in Revit 2025+ crash happens...

 

I wonder what Add-In Manager is doing to make work FilteredElementCollector(Document hostDocument, ElementId viewId, ElementId linkId) work...

 

Did you test in Revit 2024 with Add-In Manager?

 

Feels like FilteredElementCollector(Document hostDocument, ElementId viewId, ElementId linkId) was shipped buggy, 

 

Looks like before Revit 2024 user use this:

 

@Mohamed_Arshad did you have some issue with FilteredElementCollector(Document hostDocument, ElementId viewId, ElementId linkId)?

 

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

0 Likes
Message 3 of 9

ctm_mka
Collaborator
Collaborator

@sriram_rajagopal07 thank you for the videos, they provided much needed clarity from the original post. Looks like then the problem is not necessarily with the fec, but what you are doing with it. So, i would ask, if you debug, and expand the fec, and the results view, do you see all the elements from the linked file? aka is it actually collecting what it should?

@ricaun super weird, i got the simplified version to run while debugging in both 2023 & 2024 (visual studio 2022 if it matters?), here's the code i tried:

_uidoc = commandData.Application.ActiveUIDocument;
_doc = _uidoc.Document;
_active = _uidoc.ActiveView;

ICollection<ElementId> linkedid = new FilteredElementCollector(_doc, _active.Id).OfClass(typeof(RevitLinkInstance)).ToElementIds();
FilteredElementCollector fec = new FilteredElementCollector(_doc, _active.Id, linkedid.First());
List<Element> elems = new List<Element>(fec.WhereElementIsNotElementType().ToList());

 (s 

0 Likes
Message 4 of 9

sriram_rajagopal07
Participant
Participant
Accepted solution

Finally found the solution for this by using Dispose() method after utilizing the results of FEC

sriram_rajagopal07_0-1763013128572.png

 

0 Likes
Message 5 of 9

ricaun
Advisor
Advisor

@ctm_mka wrote:

@ricaun super weird, i got the simplified version to run while debugging in both 2023 & 2024 (visual studio 2022 if it matters?), here's the code i tried:

I'm my code I was not selection the RevitLinkInstance using the ViewId argument, I add that and the issue is gone...

 

I guess if you remove the "_active.Id" in your sample the exception gonna happen, like:

_uidoc = commandData.Application.ActiveUIDocument;
_doc = _uidoc.Document;
_active = _uidoc.ActiveView;

ICollection<ElementId> linkedid = new FilteredElementCollector(_doc).OfClass(typeof(RevitLinkInstance)).ToElementIds();
FilteredElementCollector fec = new FilteredElementCollector(_doc, _active.Id, linkedid.First());
List<Element> elems = new List<Element>(fec.WhereElementIsNotElementType().ToList());

 

 


@sriram_rajagopal07wrote:

 Finally found the solution for this by using Dispose() method after utilizing the results of FEC


@sriram_rajagopal07 what you mean by using Dispose? A sample please.

 

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

0 Likes
Message 6 of 9

vitalij.marcukov2BP2F
Advocate
Advocate

Has this been resolved?

0 Likes
Message 7 of 9

sriram_rajagopal07
Participant
Participant

@ricaun  The simple way is to use the FEC in an using statement as below

List<FamilyInstance> createdCaps = new List<FamilyInstance>();

using (FilteredElementCollector fecDucts = new FilteredElementCollector(doc)
    .OfCategory(BuiltInCategory.OST_DuctCurves)
    .WhereElementIsNotElementType())
{
    foreach (Duct duct in fecDucts)
    {
        if (duct != null)
        {
            FamilySymbol familySymbol = FetchSymbol(doc, duct);

            if (familySymbol != null)
            {
                ConnectorSet openConnectors = duct.ConnectorManager.UnusedConnectors;

                if (openConnectors.Size >= 1)
                {
                    FamilyInstance createdCap = DuctEndCapDef(doc, duct, familySymbol);
                    createdCaps.Add(createdCap);
                }
            }
        }
    }
}



And the manual method is as below

List<FamilyInstance> createdCaps = new List<FamilyInstance>();

FilteredElementCollector fecDucts = new FilteredElementCollector(doc)
    .OfCategory(BuiltInCategory.OST_DuctCurves)
    .WhereElementIsNotElementType();

try
{
    foreach (Duct duct in fecDucts)
    {
        if (duct != null)
        {
            FamilySymbol familySymbol = FetchSymbol(doc, duct);

            if (familySymbol != null)
            {
                ConnectorSet openConnectors = duct.ConnectorManager.UnusedConnectors;

                if (openConnectors.Size >= 1)
                {
                    FamilyInstance createdCap = DuctEndCapDef(doc, duct, familySymbol);
                    createdCaps.Add(createdCap);
                }
            }
        }
    }
}
finally
{
    fecDucts.Dispose();



0 Likes
Message 8 of 9

sriram_rajagopal07
Participant
Participant
0 Likes
Message 9 of 9

ricaun
Advisor
Advisor

@sriram_rajagopal07 wrote:

@ricaun  The simple way is to use the FEC in an using statement as below


What your sample have to do with the FilteredElementCollector(Document hostDocument, ElementId viewId, ElementId linkId) problem, you just added Dispose in some random FEC code.

 

The only workaround I found is call FilteredElementCollector(Document hostDocument, ElementId viewId) before FilteredElementCollector(Document hostDocument, ElementId viewId, ElementId linkId) and the issue is gone.

public IList<Element> GetElementInViewLink(Document document, View view, RevitLinkInstance revitLinkInstance)
{
    // This `FilteredElementCollector` is created to make the exception not happening when using viewId and linkId. A filter is required to be added.
    new FilteredElementCollector(document, view.Id)
        .WhereElementIsNotElementType();

    return new FilteredElementCollector(document, view.Id, revitLinkInstance.Id)
        .WhereElementIsNotElementType()
        .ToElements();
}

 

That was the only way I was able to fix the issue in my test project:

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

0 Likes