I've written some code to copy wall plan modifiers between walls. The code work but introduces instability that causes an Unhandled Access Violation later in the session (closing AutoCAD always causes it). The altered file can be openned without issue after. How can I prevent the instability?
<CommandMethod("PaintWallPlanMods")> _
PublicSharedSub PaintWallPlanModifiers()
Dim objDB As Database = HostApplicationServices.WorkingDatabase
Dim objEd As Editor= DocumentManager.MdiActiveDocument.Editor
Dim strResults As String = ""
Using trans As Transaction= objDB.TransactionManager.StartTransaction
Try
Dim objSource As Wall = Nothing
Do
Dim peoSource As New PromptEntityOptions(vbCrLf & "Select source wall:")
peoSource.SetRejectMessage(vbCrLf & "Invalid selection.")
peoSource.AddAllowedClass(GetType(Wall), True)
Dim perSource As PromptEntityResult= objEd.GetEntity(peoSource)
If perSource.Status = PromptStatus.Cancel Then strResults = " Canceled at Source Selection." : Exit Try
objSource = perSource.ObjectId.GetObject(OpenMode.ForRead)
If objSource.WallModifierCount > 0 Then Exit Do
Loop
Dim peoTarget As New PromptEntityOptions(vbCrLf & "Select target wall:")
peoTarget.SetRejectMessage(vbCrLf & "Invalid selection.")
peoTarget.AddAllowedClass(GetType(Wall), True)
Dim perTarget As PromptEntityResult= objEd.GetEntity(peoTarget)
If perTarget.Status = PromptStatus.Cancel Then strResults = " Canceled at Target Selection." : Exit Try
Dim objTarget As Wall = perTarget.ObjectId.GetObject(OpenMode.ForRead)
If objTarget.WallModifierCount > 0 Then strResults = " Wall already has modifiers." : Exit Try
objTarget.UpgradeOpen()
For Each objMod As WallModifier In objSource.WallModifiers
objTarget.WallModifiers.Add(objMod)
Next
Catch
End Try
trans.Commit()
objEd.WriteMessage(vbCrLf & strResults & vbCrLf)
End Using
End Sub