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
1,816 Views
12 Replies
Message 1 of 13

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.

(view in My Videos)



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

(view in My Videos)

 

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 (2)
1,817 Views
12 Replies
Replies (12)
Message 2 of 13

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 13

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 13

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 13

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 13

vitalij.marcukov2BP2F
Advocate
Advocate

Has this been resolved?

0 Likes
Message 7 of 13

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 13

sriram_rajagopal07
Participant
Participant
0 Likes
Message 9 of 13

ricaun
Advisor
Advisor
Accepted solution

@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

Message 10 of 13

vitalij.marcukov2BP2F
Advocate
Advocate

I had to use this FEC for one of my scripts (tag rooms) and was also getting an error. The solution I have found (which works for my script) is changing the active view - uidoc.ActiveView=view. See example code below.

with revit.TransactionGroup('Tag Rooms'):
for view in views_data:
room_tags = []

uidoc.ActiveView = view # <-- fix that worked for me

for rvt_link in rvt_links_data:
rvt_link_rooms = get_links_rooms(doc, view.Id, rvt_link.Id)

if rvt_link_rooms:
with revit.Transaction('Room Tags'):
for rlr in rvt_link_rooms:
...

Note: this is an example from my setup and may not apply to every project/version. Please validate in your own environment and test on a backup model. Shared for educational purposes, as-is, without warranty or liability.

0 Likes
Message 11 of 13

sriram_rajagopal07
Participant
Participant

Dear @vitalij.marcukov2BP2F, Thank you for your suggestion & sharing the sample code. This will work in most of the scenario, but for this version it's not working as expected. 

The solution suggested by @ricaun is working fine here

0 Likes
Message 12 of 13

alexey9L7TK
Observer
Observer

I'm experiencing the same issue on some of the clients' files, on both Revit 2025.4 and Revit 2026.

 

Here's a short Revit Python Shell example to trigger the issue.

import clr

clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")

from Autodesk.Revit.DB import *  # noqa: F403

uidoc = __revit__.ActiveUIDocument  # type: ignore  # noqa: F821
doc = uidoc.Document
view = doc.ActiveView

# Pick first link instance (if any)
link_instances = list(FilteredElementCollector(doc).OfClass(RevitLinkInstance).ToElements())
link_inst = link_instances[0] if link_instances else None
link_doc = link_inst.GetLinkDocument() if link_inst else None

link_elems = (
    FilteredElementCollector(doc, view.Id, link_inst.Id).ToElements()
)

 

Unfortunately, it happens only on specific revit files (that are usually tied to Central or some other cloud storage), and I can't yet share a revit file to reproduce that.

 

I can share some information from Visual Studio catching the crash. I've attached to the Revit process. Here's what I get when Revit crashes:

alexey9L7TK_0-1768923161710.png

alexey9L7TK_1-1768923269546.png

 

I've tried all of the hacks suggested in this thread (call Dispose, avoid calling ToElements/ToElementIds, temporary 2-arg FilteredElementCollector) – it still crashes.

 

If I use FilteredElementCollector(host_doc, host_view.Id, link.Id) togeher with OfCategory or OfCategoryId, it produces an slightly different stack trace on crash:

 
 

alexey9L7TK_4-1768923524802.png

 

{The input argument "categoryId" of function Autodesk::Revit::Proxy::DB::FilteredElementCollectorProxy::OfCategoryId or one item in the collection is null at line 524 of file F:\Ship26.2\2026_px64\Source\Revit\RevitDBAPI\gensrc\APIFilteredElementCollectorProxy.cpp.}

 

However, categoryId isn't null in that case, as can be clearly seen on the MSVS screenshot.

 

 

Most likely, there's some heisenbug inside the new FilteredElementCollector(host_doc, host_view.Id, link.Id). 

 

Dear Autodesk, please look into this issue as there's not much workarounds here. Replicating the behaviour of 3-arg FilteredElementCollector is rather hard and very error-prone.

 

Thank you!

 

0 Likes
Message 13 of 13

gaurav_nawaleNF9ZX
Explorer
Explorer
public List<Room> GetLinkedRoomsVisibleInHostView(
    Document hostDoc,
    ElementId hostViewId,
    List<RevitLinkInstance> linkInstances)
{
    var result = new List<Room>();
    View hostView = hostDoc.GetElement(hostViewId) as View;

    if (hostView == null || hostView.ViewType == ViewType.DraftingView)
        return result;

    foreach (RevitLinkInstance link in linkInstances)
    {
        if (link == null)
            continue;

        Document linkDoc = link.GetLinkDocument();
        if (linkDoc == null)
            continue;

        try
        {
            using (FilteredElementCollector fecRooms = new FilteredElementCollector(linkDoc)
                .OfCategory(BuiltInCategory.OST_Rooms)
                .WhereElementIsNotElementType())
            {
                foreach (Element element in fecRooms)
                {
                    if (element is Room room && room.Area > 0)
                    {
                        result.Add(room);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            continue;
        }
    }

    return result
        .GroupBy(r => r.UniqueId)
        .Select(g => g.First())
        .ToList();
}

I'm experiencing the same crash in Revit 2025 and have already tried every workaround mentioned in this thread, but none of them have resolved the issue for me.

If anyone has found a solution, I'd really appreciate your help. Thanks in advance!



0 Likes