AccessViolationException on new GetParameters() in 2015 API

AccessViolationException on new GetParameters() in 2015 API

RickyBell
Enthusiast Enthusiast
2,286 Views
15 Replies
Message 1 of 16

AccessViolationException on new GetParameters() in 2015 API

RickyBell
Enthusiast
Enthusiast

I'm upgrading my 2014 app to 2015.  get_Parameters is gone, so I'm using GetParameters().  It works for the first iteration through my selection, but on the 2nd component it throws "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

 

//Run through the collection
foreach (ElementId elId in sel.GetElementIds())
{
   Element ele = doc.GetElement(elId);
   StringBuilder varDescription = new StringBuilder();

   IList<Parameter> myParams = ele.GetParameters("Description");  //Throws error right here.
   if (myParams.Count != 0)
   {
      varDescription.Append(myParams[0].AsString());
   }

}

 

I've taken it down to the simplest of code in my project and it still persists.  Any help is appreciated!

0 Likes
2,287 Views
15 Replies
Replies (15)
Message 2 of 16

arnostlobel
Alumni
Alumni

Hello RickyBell,

 

just for clarification: You do not seem to be testing if the ele you got for each elId is actually valid (not null) before you invoke a method at the line that throws. I assume it is not null (for I think .NET would give you a different exception), but can you please confirm my assumption?

 

Thank you

Arnošt Löbel
0 Likes
Message 3 of 16

RickyBell
Enthusiast
Enthusiast

Thanks for the reply.  You're right.  I'm assuming that ElementIds produced by the uiDoc.Selection.GetElementIds() are all valid.  How would you recommend checking to see if they are valid?

 

*update - When the error occured, I wrote down the ElementId it was choking on.  I went back into the drawing and used the 'Select by ID' tool and it finds the component perfectly.

0 Likes
Message 4 of 16

arnostlobel
Alumni
Alumni

I did not think there was a problem with the element, but I wanted you to confirm that. Generally, it is a good idea to check for NULL-ness of element objects after they are obtained from the Document.GetElement(Id) method. For two reasons: Sometimes the programmer holds to the Ids  for longer than they should, thus end up asking for an element that does not exist in the model anymore. In other possible scenario the user uses the Id of a valid element, but it so happens it is an internal element that Revit does not give an access too. (Such elements generally have negative Ids). In both those cases the GetElement method returns NULL.

 

Anyhow, like I said, I do not want to claim an invalid element was the problem in your case. Assuming you confirm it is not NULL at the time you invoke the  GetParameters method, may second guess is that there is apparently something wrong with the parameter (or parameters) of the name “Description”. It is practically impossible for me to conclude what the problem might be without debugging the exact case. However, you can try a little of experimenting to find out if you can get to the parameter (or parameters) by other means. (After all the GetParameters(string) is not very trustworthy a method for finding parameters (more details in the RevitAPI.chm)). For example, try the Parameters property and search the returned collection for a parameter (or parameters) that match the name (and possibly other attributes).

Arnošt Löbel
0 Likes
Message 5 of 16

RickyBell
Enthusiast
Enthusiast

I tried a different method.  Same results.

 

            foreach (ElementId elId in sel.GetElementIds())
            {
                
                Element ele = doc.GetElement(elId);
                StringBuilder varDescription = new StringBuilder();

                ParameterSet pSet = ele.Parameters;

                foreach (Parameter p in pSet)
                {
                    if (p.Definition.Name == "Description") //Throws same error.
                    {
                        varDescription.Append(p.AsString());
                    }
                }
             }

 

I'm going to create a completely new project with just the code here to try to troubleshoot where the error is.  Thanks for your help so far.

0 Likes
Message 6 of 16

RickyBell
Enthusiast
Enthusiast

Well, I think I've gotten to the bottom of it.  It has to do with our Revit content.  Our content was created in 2014 and just pulled into a 2015 project.  Standard Pipe, Duct, Walls, etc.. work perfectly.  We'll take a closer look at our families and try this again.  Thanks for your help!

0 Likes
Message 7 of 16

arnostlobel
Alumni
Alumni

While I am glad you have found a way to solve the problem, I am still alerted by the fact that a parameter (that obviously exist) would end up with an invalid definition (as it appears). That should not happen no matter whether you opened older files that needed an upgrade or an already upgraded file, or a new file.

Since you mentioned possible relation to your family, I should probably disclose an important information regarding families. Revit2015 has done a significant changes to how families in host document are handled. Unfortunately, the first release of R2015 (and possibly the first web updates as well) suffer from a few bugs that could cause families get corrupted when loaded into host document, particularly in work shared environment. Again, as with the previous case, I would not necessarily conclude you experienced a case of corrupted families (and the corruptions are rather rare), but just in case I would definitely recommend upgrading to the latest available version of Revit 2015 before you start upgrading your 2014 (or older) files.

Arnošt Löbel
0 Likes
Message 8 of 16

RickyBell
Enthusiast
Enthusiast

Thanks for the info.  We found that simply doing an audit on the drawing fixed our issues.  It's always the simplest things we miss.  Thanks again for your help!

0 Likes
Message 9 of 16

RickyBell
Enthusiast
Enthusiast

My users are having to audit each project before opening.  Every single time.  If they don't, the receive the AccessViolationException when they try to read parameters from content.  Is anyone else experiencing this?  Here's the line of code it chokes on:

 

IList<Parameter> myParams = el.GetParameters("Description");

 

Thanks for any help!

0 Likes
Message 10 of 16

RickyBell
Enthusiast
Enthusiast

This issue still persists.  Has anyone experienced this?

 

The simplest expanation of what's going on is:  When reading parameter data from an element, Revit 2015 randomly throws an Access Violation Exception.  This same code works well in Revit Copernicus and Revit 2016.

 

Auditing the project while opening fixes the problem for the session, but the problem comes back again after closing and reopening the project. 

 

Any help would be appreciated.  Thanks.

0 Likes
Message 11 of 16

Anonymous
Not applicable

Not sure if this is related but it relates to parameters and Revit is crashing with "System.AccessViolationException" message.

I am new to this forum and VERY new to Revit API and C#.  Any help is appreciated.

I am working on a script that needs to select tags based on their "TagText" value (I eventually want to change the tag type based on the text value). When the script comes across a tag that is trying to access a shared parameter that doesn't exist in the tagged family (tag label uses ABC shared parameter, but the family that it is tagging doesn't have ABC shared parameter), I get a "System.AccessViolationException" error and Revit shuts down.  The issue is repeatable and it even crashes the Revit Lookup Addin.  This is a bit of the code I'm using:

 

FilteredElementCollector collector = new FilteredElementCollector(doc, doc.ActiveView.Id)

.OfClass(typeof(IndependentTag));

IList<IndependentTag> tag = collector

.Cast<IndependentTag>()

.ToList<IndependentTag>();

foreach (IndependentTag ids in tag)

{

string tags = ids.TagText; (CRASHES HERE)

TaskDialog.Show("Tag Value", tags);

}

 

Is there any way past this?

 

 

0 Likes
Message 12 of 16

Anonymous
Not applicable
I have the same exception when I try to get after the rollback transaction. Without a transaction, everything works well. But after changing the walls in transaction and rollback an exception appears. ICollection generatingElementIds = wall.GetGeneratingElementIds(edges[1]);
0 Likes
Message 13 of 16

aignatovich
Advisor
Advisor

Hi!

 

I'm almost sure, that you try to do something with Revit API from invalid context, you can use Revit API only in some specific places, e.g. IExternalCommand / IExternalApplication main thread, from external events, DMU and some others. If you try to access it, for example from modalless form, you'll crash Revit. There is specific framework, that allows you to deal with modalless dialogs, please refer to Samples\ModelessDialog\ModelessForm_ExternalEvent from Revit SDK

0 Likes
Message 14 of 16

Anonymous
Not applicable
It seems to me that my problem is very similar to the problem in this blog, in the section Moving the Beam Downwards in a Temporary Transaction: http://thebuildingcoder.typepad.com/blog/2013/03/supporting-columns.html#5 There is the same exception and also because of the transaction. Only in another method, but also because of id
0 Likes
Message 15 of 16

Anonymous
Not applicable
I does't try to do something with Revit API from invalid context. I just tried to use a temporary trick with the transaction. I need to get the intersections with the walls. Due to the fact that some walls contain doors and windows sometimes some intersections do not occur because of the voids. I was advised to remove doors and windows using a temporary transaction. I have achieved that it works correctly. But further after it in the code began to appear different exceptions. They appear in different methods of code and they are different. This and the NollReferenceExseption, Autodesk.Revit.Exceptions.InternalsExexe. I do not use modal windows and nothing like that. My code worked before using the temporary transaction. In one of the posts I was told that this is due to the fact that the transaction clears the cache SpatialElementGeometryCalculator. But I was trying to use a single SpatialElement Geometric Calculator for the rest of the program code. This did not work. Exceptions still appear.
0 Likes
Message 16 of 16

Anonymous
Not applicable
Hi, This is my post with a description of the problem and the code. https://forums.autodesk.com/t5/revit-api-forum/temporary-transaction-trick-exception/td-p/7992798
0 Likes