Code Red > .NET

AutoCAD 2020 and Random System.AccessViolationExceptions

<< < (5/5)

lCine7ic:

--- Quote from: n.yuan on October 30, 2020, 10:11:07 AM ---
--- Quote from: lCine7ic on October 29, 2020, 11:36:41 AM ---
--- Code: ---Thank you for the suggestion.

I figured out where the problem lies, and a crude (not ideal) work around.

...

                    If newBTR.IsDynamicBlock Then
                        iIndex = 0
                        ...
                                Dim iTemp As Integer = 0
                                For Each cDBRP As DynamicBlockReferenceProperty In tempBR.DynamicBlockReferencePropertyCollection
                                    iIndex += 1
                                    If cDBRP.PropertyName.ToUpper = "ORIGIN" Then
                                        Debug.WriteLine("Skipping Index " + iTemp.ToString + ", <ORIGIN>")
                                        Continue For
                                    End If
                                    DynamicPropertyIndexByName.Add(cDBRP.PropertyName.ToUpper, iTemp)
                                    doc.WriteLine("Adding DynamicBlockReferenceProperty:{PropertyName.ToUpper():'" + cDBRP.PropertyName.ToUpper + "', Index:" + iIndex.ToString + "} to Dictionaries")
                                Next
                        ...
 
                    End If


--- End code ---

My crude workaround is to statically define the Indices of the Dynamic Block Reference Properties I need in the classes that uses them... I will regret this later I know  :straight:

--- End quote ---

I assume DynamicPropertyIndexByName is a Dictionary<string, int>, where string key is dynamic property name, and int value is what you are to use later (looking up as you mentioned). However, I do not see why you need the Dictionary for looking up, because the value of every item in the dictionary is the same: 0, according to your code in the foreach(){...} loop, where you set iTemp=0 before the loop, and never change its value during the loop where it is added into the Dictionary. I am not sure if it is intentional, or it is code mistake (of forgetting to change/increment its value).

Could this be the reason of the crash? Imagine that if the dynamic blocks you worked on only have one dynamic property (or 2, but one of it is named "ORIGIN", thus does not count here), the dictionary would only have 1 item, therefore there would be no problem for later lookup. But if there are more dynamic property name collected in the dictionary but all have its value equal 0, then the later lookup might cause trouble (have no idea what the later process doing here), hence the crash. More often than not, even it appears as random crash, 99.9% would come from our own code mistake.

--- End quote ---

n.yuan: You're absolutely correct about the indices all being 0 -- truth is, what I showed is not my actual code.  I tried to simplify my actual code and clearly failed to do a sanity check.  The "real" code actually does assign the correct index.  I'll update the other post to reflect the correction. 

The issue is not with the dictionary; in fact, the dictionary is only used when there is a block reference added to the drawing that is actually used, and the failure occurs even when there isn't one.

When it fails, the thread containing the failure shows the following callstack.
Acdbmgd.dll!<Module>.AcArray<AcDbDynBlockReferenceProperty,AcArrayObjectCopyReallocator<AcDbDynBlockReferenceProperty> >.setPhysicalLength(AcArray<AcDbDynBlockReferenceProperty,AcArrayObjectCopyReallocator<AcDbDynBlockReferenceProperty> >*, int) < Failure occurs here
Acdbmgd.dll!Autodesk.AutoCAD.DatabaseServices.DynamicBlockReferencePropertyCollection.DeleteUnmanagedObject()
Acdbmgd.dll!Autodesk.AutoCAD.DatabaseServices.BlockTableRecordEnumerator.DeleteUnmanagedObject()
Acdbmgd.dll!Autodesk.AutoCAD.Runtime.DisposableWrapper.~DisposableWrapper()

Again, this code has worked in previous versions of AutoCAD (AutoCAD 2013, 2016, 2018); it only started failing in AutoCAD 2020.
I'm wondering if the problem has to do changes in the native code that help implement the updated 'INSERT' and 'PURGE' commands.

lCine7ic:
Just an update.  I was able to reuse the code below still in 2020 -- the issue seemed to be with the 'Continue For'.  Not sure why it's causing problems but be warned.

Navigation

[0] Message Index

[*] Previous page

Go to full version