Search code examples
autodesk-forgeautodesk-viewerautodesk-navisworks

How to fix System.AccessViolationException encountered while using nwCreate


We are using the NwCreate APIs to create geometry. Our application successfully executes and generates the desired output on its first run, however on the next execution it throws an AccessViolationException error. This occurs whether we are using the same data or different values for the subsequent run.

Our code is based on the example in this blog post: https://adndevblog.typepad.com/aec/2012/07/nwcreate-color-of-geometry-does-not-take-effect-on-cylinder.html

I have observed that if we remove this code to add the material color attribute, then we do not get this error.

LiNwcNodeAddAttribute(geom, _material);

I have traced all of the handles we're creating and verified we are destroying all handles that are created.

This is the full text of the error we're getting: System.AccessViolationException: 'Attempted to read or write protected memory.'

Has anyone else encountered a similar issue? Can you provide any troubleshooting advice?


Solution

  • Found the cause of this, the spoolID was previously destroyed

                LtNwcData spoolID = LiNwcDataCreate();
                LiNwcDataSetInt32(spoolID, arr_spool_ids[i]);
                LiNwcDataDestroy(spoolID);
    
                LtNwcData constructionComponentID = LiNwcDataCreate();
                //this was the error, using spoolID here (it was already destroyed):
                //  LiNwcDataSetInt32(spoolID, arr_cc_ids[i]);
                //fix was to change to this:
                LiNwcDataSetInt32(constructionComponentID, arr_cc_ids[i]);