IndepententTag.TagText crashing

IndepententTag.TagText crashing

Anonymous
Not applicable
884 Views
6 Replies
Message 1 of 7

IndepententTag.TagText crashing

Anonymous
Not applicable

In my plugin, I am extracting floorplan tag text. This is working fine, however I'm running into a crash on a particular file that we have. This happens on Revit 2015 and 2016 and possibly more (that's all I've tested).

 

When I try to access the TagText property of IndependentTag, I get a System.AccessViolationException with the following callstack:

at getParamValueFormatted(Element* , ParamDef* , AString* , FormatOptions* , FormatIntent* )

at Autodesk.Revit.DB.IndependentTag.get_TagText()

 

This is happening for a few Door Tags in the file. Notably, all these tags show up as a question mark (?) on the floorplan. I suspect these tags were somehow corrupted when the Revit file upgraded.

 

I don't need to recover the data in these tags. All I'm asking is a way to avoid the System.AccessViolationException, as this Exception can't be caught, so it ends up crashing Revit. I'd just delete the corrupted tags, but the idea is for this plugin to be generic, and I suspect that if I have a file with this issue, others will likely run into this too. They won't know it's due to a corrupted tag, so they'll just blame my plugin, and I can't even bring up a message after it happens, because the exception is unrecoverable.

 

I suspect there's some way around this, because Revit does not crash when interacting with these tags outside of the API. Is there any way to determine that an IndependentTag is unsafe, and thus avoid accessing the TagText property for that case?

 

Thanks for any help.

Accepted solutions (1)
885 Views
6 Replies
Replies (6)
Message 2 of 7

Charles.Piro
Advisor
Advisor

Hello,

 

can you join a file with the problem ?

 

If you use a loop "foreach",you can use this for avoid the "bad" Tag :

 

 

foreach (IndependentTag tag in lstIndTag)
{
    try
    {
        //your code....
    }
    catch (Exception)
    {
        continue;
    } 
}

Smiley Wink

 

 



PIRO Charles
Developer

PIRO CIE
Linkedin


0 Likes
Message 3 of 7

Anonymous
Not applicable

Hello, thank you for your response.

 

Unfortunately, System.AccessViolationException cannot be caught with a try/catch, because it's an unrecoverable error. It's a memory access violation which is occuring in the native code in RevitAPI.dll, and so the program can't continue. I wish it were that simple, though!

 

I'm working on stripping down the file to the bare minimum so that I can upload it here. I'll reply again when I have a file to include.

0 Likes
Message 4 of 7

Anonymous
Not applicable

 

I have prepared a stripped-down file along with an example plugin to cause the crash. Please let me know your results!

 

I made the plugin for Revit 2015, but it should work on pretty much any version with the proper modificaitons (like referencing the proper dlls). Just build and install the plugin. Then load in davidkalloc-crash.rvt and go to Add-Ins->External Tools->Crash.

 

The offending tag is the '?' symbol on the floor plan named "FloorPlan"

0 Likes
Message 5 of 7

Charles.Piro
Advisor
Advisor
Accepted solution

Hello,

 

thank you for the file. Here is a way for get around the problem :

 

1) Create a config file with the lines :

 

<configuration>
  <runtime>
    <legacyCorruptedStateExceptionsPolicy enabled="true" />
  </runtime>
</configuration>

2) The code C# :

 

 

[TransactionAttribute(TransactionMode.Manual)]
[RegenerationAttribute(RegenerationOption.Manual)]
public class Crash : IExternalCommand
{
    FilteredElementCollector collector;

    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        //Get application and document objects
        UIApplication uiApp = commandData.Application;
        Document doc = uiApp.ActiveUIDocument.Document;

        collector = new FilteredElementCollector(doc).OfClass(typeof(IndependentTag));
        ReadTagText();

        return Result.Succeeded;
    }

    [System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptionsAttribute]
    void ReadTagText()
    {
        IEnumerator<Element> it = collector.GetEnumerator();
        while (it.MoveNext())
        {
            IndependentTag tag = it.Current as IndependentTag;
            if (tag != null)
            {
                try
                {
                    String tagText = tag.TagText;
                    MessageBox.Show("Tag has text: " + tagText);
                }
                catch (Exception)
                {
                    continue;
                }
            }
        }
    } 
}

 

 

I hope this will help you.

 

Smiley Wink



PIRO Charles
Developer

PIRO CIE
Linkedin


Message 6 of 7

Anonymous
Not applicable

Thank you for the response again. Can you clarify how to create the config file? What should I name it? Is there some way I need to load it with my plugin?

0 Likes
Message 7 of 7

Charles.Piro
Advisor
Advisor

Hello,

 

 

After test, the config file is not mandatory. you can juste use the C# code.

 

if this doesn't work, here's a link for understand the config file : https://msdn.microsoft.com/en-gb/library/ms184658.aspx

 

Smiley Wink



PIRO Charles
Developer

PIRO CIE
Linkedin


0 Likes