Message 1 of 5
Parameter.IsReadOnly throwing System.AccessViolationException
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have code that uses Parameter.IsReadOnly. In 99% of cases it works fine. On a specific customer's computer, on a specific family in a specific project, it fails with a System.AccessViolationException. I am able to get on to the customer's computer. What kinds of tests should I run or things should I look for?
The full error message reads:
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at Element.isParameterModifiable(Element*, ElementId, Boolean, Boolean )
at Autodesk.Revit.DB.Parameter.get_IsReadOnly()
...
The specific code that triggers this error:
//Function to copy parameters from one FamilyInstance to another.
public static void CopyParameters(FamilyInstance familyDestination, FamilyInstance familySource)
{
//Look at all parameters on the source FamilyInstance
foreach (Parameter paramSource in familySource.Parameters)
{
//Get the definition of the parameter.
Definition d = paramSource.Definition;
//Try to get a matching parameter from the destination FamilyInstance
Parameter paramDest = familyDestination.get_Parameter(d);
//If we don't find a match by defintion, look for a match by name
if (paramDest == null)
{
if (familyDestination.ParametersMap.Contains(d.Name))
{
paramDest = familyDestination.ParametersMap.get_Item(d.Name);
}
}
//Make sure we have a parameter and it is not read-only
if (paramDest != null && !paramDest.IsReadOnly) // Error happens here
{
//Copy the value from the source to the parameter
if (paramDest.StorageType == StorageType.Integer)
{
paramDest.Set(paramSource.AsInteger());
}
else if (paramDest.StorageType == StorageType.Double)
{
paramDest.Set(paramSource.AsDouble());
}
else if (paramDest.StorageType == StorageType.String)
{
paramDest.Set(paramSource.AsString());
}
else if (paramDest.StorageType == StorageType.ElementId)
{
paramDest.Set(paramSource.AsElementId());
}
}
}
}
David Robison
Design Master Electrical RT
Design Master Electrical RT