Transaction.Dispose() always throws exception in AutoCAD 2016

Transaction.Dispose() always throws exception in AutoCAD 2016

Anonymous
Not applicable
1,086 Views
7 Replies
Message 1 of 8

Transaction.Dispose() always throws exception in AutoCAD 2016

Anonymous
Not applicable

I have some code that works well in AutoCAD versions 2012-2015 which opens a transactions, looks up some object info, then disposes the transaction.  In 2016 this code thows an AccessViolationException ("Attempted to read or write protected memory. This is often an indication that other memory is corrupt.") as soon as Dispose() is called.  I have removed transactions in a few spots in code, using direct reads through the ObjectId, which works just fine.  Everytime I replace a chunk of code with this method the next chunk that uses a Transaction starts puking.  Obviously something is very wrong or very different in AutoCAD 2016 if I cannot even use a transaction.

0 Likes
Accepted solutions (1)
1,087 Views
7 Replies
Replies (7)
Message 2 of 8

dgorsman
Consultant
Consultant

What happens if you go the Using (...) route?

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes
Message 3 of 8

Anonymous
Not applicable

The code is actually in a using pattern.  I confirmed that it is the Dispose() call causing the grief by refactoring the code chunk to call it explicitly.  So the using blocks are failing because they are calling Dispose().

0 Likes
Message 4 of 8

Jeff_M
Consultant
Consultant
Can you post some sample code that exhibits this? I have not yet run into this issue.
Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 5 of 8

Anonymous
Not applicable

I have a variety of code running into this which is all proprietary, but here is one example of what is typically being done:

 

using (var transaction = someDbObject.Database.TransactionManager.StartTransaction())
{

  //do some work

}

 

That is as simple as it is to throw the exception.  It also throw in these cases if I do no work at all, but just manually start the transaction and then manually dispose of it.  Please note that this is code that has been working for a couple of years starting with Acad 2012 on through 2015, and now is throwing up all over 2016.

0 Likes
Message 6 of 8

Anonymous
Not applicable
Accepted solution

It would appear that AutoCAD 2016 now requires that some activities be conducted in the appropriate threading context.  As soon as I started doing my work inside a call to Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.ExecuteInCommandContextAsync(...) stuff started working.  I saw this same thing happen in Revit last year.

0 Likes
Message 7 of 8

Anonymous
Not applicable

Perhaps i'm missing something, but isn't the point of "using" so you don't need to call dispose?

0 Likes
Message 8 of 8

Anonymous
Not applicable
In C#, using is just syntactic sugar. It really creates the equivalent of
a try/finally, and calls .Dispose() for you. The problem here isn't with
how .Dispose() gets called, it is that it wasn't working right. I ended
up rewriting to get around this.
0 Likes