# 'System.AccessViolationException' occurred in Rhino3dmIO.dll, InstanceDefinitionGeometry

**URL:** <https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705>\
**Category:** openNURBS\
**Created:** [November 13, 2018, 6:08am UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705 "2018-11-13T06:08:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![archinate1](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/archinate1/32/498181_2.png) [@archinate1](https://discourse.mcneel.com/u/archinate1)\
**Post date:** [November 13, 2018, 6:08am UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705/1 "2018-11-13T06:08:34Z")

</div>

Hello,  
I am building a tool where I am accessing the geometry of block definitions and instances. These are InstanceDefinitionGeometry and InstanceReferenceGeometry respectively in Rhino3dmIO.dll.

When I start working with these objects, it seems I regularly produce a ‘System.AccessViolationException’ in Rhino3dmIO.dll which results in my application to crash. The full message is:

```
An unhandled exception of type 'System.AccessViolationException' occurred in Rhino3dmIO.dll Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

```

It appears to be happening in even simple cases of accessing one or two InstanceDefinitionGeometry. Sometimes my code will seemingly complete and then the program will crash without an error message.

For example, here I am iterating over InstanceDefinitionGeometry and identifying breps and meshes in an Instance as lists of File3dmObjects. I am not sure why this would be problematic… is there anything special I need to be doing when dealing with InstanceDefinitionGeometry that would make it different than dealing with, say, a normal File3dmObject that is not tied to a block?

```
            foreach (InstanceDefinitionGeometry inst in instDefs)
            {
                string name = inst.Name;
                Guid[] objects = inst.GetObjectIds();

                List<File3dmObject> brepObjs = new List<File3dmObject>();
                List<File3dmObject> meshObjs = new List<File3dmObject>();

                // get File3dmObjects of block objects
                foreach (Guid g in objects)
                {
                    File3dmObject obj = _rhinoDoc.Doc.Objects.FindId(g);
                    GeometryBase geo = obj.Geometry;
                    if (geo is Rhino.Geometry.Brep)
                    {
                        brepObjs.Add(obj);
                    }
                    else if (geo is Rhino.Geometry.Mesh)
                    {
                        meshObjs.Add(obj);
                    }
                }

             // start working with collected File3dmObjects

            }
```

---

<div class="post-metadata">

**Author:** ![stevebaer](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/stevebaer/32/473567_2.png) [@stevebaer](https://discourse.mcneel.com/u/stevebaer)\
**Post date:** [November 13, 2018, 6:20am UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705/2 "2018-11-13T06:20:39Z")

</div>

Sounds like a bug in Rhino3dmIo that I need to fix. Do you have a small sample that shows this behavior so I can be lazy?

---

<div class="post-metadata">

**Author:** ![archinate1](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/archinate1/32/498181_2.png) [@archinate1](https://discourse.mcneel.com/u/archinate1)\
**Post date:** [November 13, 2018, 3:45pm UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705/3 "2018-11-13T15:45:07Z")

</div>

Thanks @stevebaer  
I can’t really isolate culprit code. I don’t know where it is since it crashes without any stack trace or message. ‘System.AccessViolationException’ appeared only a few times after the code seemingly completed to give me something to go on and clue me in on that it is something to do with Rhino3dmIO.dll.

I zero’d in on my use of InstanceDefinitionGeometry to access block geometry because it is a new addition to my project. After getting the File3dmObjects from the blocks, I am exported them to separate files.My other workflows for using File3dmObjects (not from blocks) are all working as expected.

I’m going to also try changing up my order of operations to try and rule out some other things. The tool I’m making has Revit as the host app… and I think we all know how muddy the waters can get there.

---

<div class="post-metadata">

**Author:** ![stevebaer](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/stevebaer/32/473567_2.png) [@stevebaer](https://discourse.mcneel.com/u/stevebaer)\
**Post date:** [November 16, 2018, 6:38am UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705/4 "2018-11-16T06:38:47Z")

</div>

I’m guessing that this crash is happening when the Visual Studio debugger is not attached?

---

<div class="post-metadata">

**Author:** ![archinate1](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/archinate1/32/498181_2.png) [@archinate1](https://discourse.mcneel.com/u/archinate1)\
**Post date:** [November 17, 2018, 8:27pm UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705/5 "2018-11-17T20:27:11Z")

</div>

@stevebaer This happens during a debug session in Visual Studio. I have try/catch on all methods used in my code. However, no specific exceptions are caught. The only message I’ve been able to produce is the “An unhandled exception of type ‘System.AccessViolationException’ occurred in Rhino3dmIO.dll Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.”

No line info is given which makes me think it has to do with something deep in Rhino3dmIO. I have similar code that operates on File3dmObjects like breps and surfaces. It is when I started working with InstanceDefinitions and InstanceReferences that things go haywire.

---

<div class="post-metadata">

**Author:** ![archinate1](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/archinate1/32/498181_2.png) [@archinate1](https://discourse.mcneel.com/u/archinate1)\
**Post date:** [November 17, 2018, 11:54pm UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705/6 "2018-11-17T23:54:35Z")

</div>

The “An unhandled exception of type ‘System.AccessViolationException’…”  
 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/e/7/e70e5e1a573fb04a923d3e1fac3a01177bcf1f73.png)

---

<div class="post-metadata">

**Author:** ![stevebaer](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/stevebaer/32/473567_2.png) [@stevebaer](https://discourse.mcneel.com/u/stevebaer)\
**Post date:** [November 18, 2018, 12:11am UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705/7 "2018-11-18T00:11:23Z")

</div>

Hmm… I wonder if an exception is occurring when a class is being freed up by the GC

---

<div class="post-metadata">

**Author:** ![archinate1](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/archinate1/32/498181_2.png) [@archinate1](https://discourse.mcneel.com/u/archinate1)\
**Post date:** [November 18, 2018, 5:53pm UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705/8 "2018-11-18T17:53:05Z")

</div>

Hi @stevebaer,  
I worked up a simplified example of where I am encountering these types of errors. I am using Rhino 6 and the latest version of Rhino3dmIO.dll on NuGet.

I am testing with a file with 3 simple InstanceDefinitionGeometry. There are several InstanceReferenceGeometry in the Rhino model (block instances).

[meshblocktest.3dm](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/0/3/0363d42101d0c5bfb82c0c06a765c0f89fcf5b43.3dm) (127.2 KB)

This code below simply iterates over the document’s File3dmObjects. If the File3dmObject has geometry of the type InstanceReferenceObject, I am then getting information the transform data and the name of the parent InstanceDefinitionGeometry.

I can step through the code and watch the program successfully get the data I want here within the debugger. However, when the program completes I receive either a hard crash with no warning OR sometimes the System.AccessViolationException message.

Here is the code:

```
// Getting a list of Rhino File3dmObject by layer
List<File3dmObject> rhObjs = GetLayerObjects(setLayer);

// Loop over objects
foreach (File3dmObject rhObj in rhObjs)
{
  // Check if File3dmObject geometry is InstanceReferenceGeometry
  if (rhObj.Geometry is Rhino.Geometry.InstanceReferenceGeometry)
  {
    InstanceReferenceGeometry geo = (InstanceReferenceGeometry)rhObj.Geometry;

    // Lookup the parent block definition
    Guid blockDefId = geo.ParentIdefId;
    InstanceDefinitionGeometry def = _doc.AllInstanceDefinitions.FindId(blockDefId);

    // block definition name
    string defname = def.Name;

    // transform data for block instance
    Rhino.Geometry.Transform xform = geo.Xform;
  }
}
```

---

<div class="post-metadata">

**Author:** ![stevebaer](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/stevebaer/32/473567_2.png) [@stevebaer](https://discourse.mcneel.com/u/stevebaer)\
**Post date:** [November 20, 2018, 4:56pm UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705/9 "2018-11-20T16:56:37Z")

</div>

Thanks! I’ll take a look.

---

<div class="post-metadata">

**Author:** ![stevebaer](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/stevebaer/32/473567_2.png) [@stevebaer](https://discourse.mcneel.com/u/stevebaer)\
**Post date:** [November 20, 2018, 7:41pm UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705/10 "2018-11-20T19:41:29Z")

</div>

I can repeat this and am working on a fix.  
[https://mcneel.myjetbrains.com/youtrack/issue/RH-49605](https://mcneel.myjetbrains.com/youtrack/issue/RH-49605)

The InstanceDefinitionGeometry is definitely where the problem lies.

Thanks for the sample, immensely helpful

---

<div class="post-metadata">

**Author:** ![stevebaer](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/stevebaer/32/473567_2.png) [@stevebaer](https://discourse.mcneel.com/u/stevebaer)\
**Post date:** [November 20, 2018, 9:14pm UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705/11 "2018-11-20T21:14:39Z")

</div>

I just committed a fix for this. Hopefully you’ll see this in the next Rhino3dmIO build

---

<div class="post-metadata">

**Author:** ![archinate1](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/archinate1/32/498181_2.png) [@archinate1](https://discourse.mcneel.com/u/archinate1)\
**Post date:** [November 20, 2018, 9:33pm UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705/12 "2018-11-20T21:33:49Z")

</div>

AWESOME! Looking forward to it! Thanks for the assist @stevebaer

---

<div class="post-metadata">

**Author:** ![archinate1](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/archinate1/32/498181_2.png) [@archinate1](https://discourse.mcneel.com/u/archinate1)\
**Post date:** [November 20, 2018, 10:35pm UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705/13 "2018-11-20T22:35:35Z")

</div>

If I may ask @stevebaer, when is the next Rhino3dmIO going to be available on NuGet?

---

<div class="post-metadata">

**Author:** ![stevebaer](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/stevebaer/32/473567_2.png) [@stevebaer](https://discourse.mcneel.com/u/stevebaer)\
**Post date:** [November 20, 2018, 11:08pm UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705/14 "2018-11-20T23:08:13Z")

</div>

I’m not exactly sure; everything is so automated now😀 @will may have a good idea on when the next nuget builds will become available

---

<div class="post-metadata">

**Author:** ![will](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/will/32/479454_2.png) [@will](https://discourse.mcneel.com/u/will)\
**Post date:** [November 21, 2018, 8:15am UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705/15 "2018-11-21T08:15:09Z")

</div>

Packages for the latest 6.11 RC and 7.0 WIP are now available 🧙

---

<div class="post-metadata">

**Author:** ![archinate1](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/archinate1/32/498181_2.png) [@archinate1](https://discourse.mcneel.com/u/archinate1)\
**Post date:** [November 21, 2018, 2:10pm UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705/16 "2018-11-21T14:10:06Z")

</div>

Hi @will  
I am looking now. It looks like Rhino3dmIO Nuget is still 6.10.18308.14011. Will the version with @stevebaer’s fix also be published soon?

Thanks,  
-Nate

---

<div class="post-metadata">

**Author:** ![will](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/will/32/479454_2.png) [@will](https://discourse.mcneel.com/u/will)\
**Post date:** [November 21, 2018, 3:04pm UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705/17 "2018-11-21T15:04:48Z")

</div>

Yesterday’s 6.11 RC release has the `-rc` prerelease suffix, so you’ll need to check the “include prereleases” box!

---

<div class="post-metadata">

**Author:** ![archinate1](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/archinate1/32/498181_2.png) [@archinate1](https://discourse.mcneel.com/u/archinate1)\
**Post date:** [November 21, 2018, 9:25pm UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705/18 "2018-11-21T21:25:58Z")

</div>

@stevebaer is 6.11.18323.19181-rc the correct one with your fix? It appears to be the latest prerelease for 6.11 on NuGet. I just installed it and I am getting the same crash behavior as before with the InstanceDefinitionGeometry code snippet.

-Nate

---

<div class="post-metadata">

**Author:** ![archinate1](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/archinate1/32/498181_2.png) [@archinate1](https://discourse.mcneel.com/u/archinate1)\
**Post date:** [November 21, 2018, 9:34pm UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705/19 "2018-11-21T21:34:07Z")

</div>

@stevebaer disregard that last note… I had another plugin also loading that was using an older version and it was loading that one, I think. Seems to be running okay now. I’ll let you know if I run into any issues as I continue testing.

Thanks again!

---

<div class="post-metadata">

**Author:** ![archinate1](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/archinate1/32/498181_2.png) [@archinate1](https://discourse.mcneel.com/u/archinate1)\
**Post date:** [November 21, 2018, 9:49pm UTC](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705/20 "2018-11-21T21:49:14Z")

</div>

@stevebaer Okay… so it seems as though my last note about 6.11.18323.19181-rc was correctly stated. It appears that my code completes but the crash is now more delayed than before (if that makes sense at all.).

My code appears to complete as expected but the program eventually crashes in the same manner as I was describing before with that InstanceDefinitionGeometry code snippet. If I have the wrong 6.11 RC installed, please let me know.

[Next page](https://discourse.mcneel.com/t/system-accessviolationexception-occurred-in-rhino3dmio-dll-instancedefinitiongeometry/74705.md?page=2)
