.NET
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Fatal Exception when running code
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
I have been trying to figure this out for a while and I just can't pin point why I get a fatal exception when running this code (see code below).
I have done this same code (for the mose part) to import pagesetups into the active document and it works fine. however when I run it as it is here to import the page setups into other drawings it seems to always crash autocad.
please help!!!!
Code:
Private Sub copy_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles copy.Click
Dim psdb As New Database(False, True)
psdb.ReadDwgFile("M:\CAD Management\CAD Files\Template\Basic.dwt", FileOpenMode.OpenForReadAndReadShare, True, "")
For Each item In ListBox1.Items
Dim db As New Database(True, False)
db.ReadDwgFile(item.ToString, FileOpenMode.OpenForReadAndAllShare, False, "")
Using trans As Transaction = psdb.TransactionManager.StartTransaction
Using trns As Transaction = db.TransactionManager.StartTransaction
Dim psd As DBDictionary = trans.GetObject(psdb.PlotSettingsDictionaryId, OpenMode.ForRead)
Dim id As ObjectId
Dim pd As DBDictionary = trns.GetObject(db.PlotSettingsDictionaryId, OpenMode.ForRead)
For Each entry As DBDictionaryEntry In pd
pd.UpgradeOpen()
pd.Remove(entry.Value)
pd.DowngradeOpen()
Next
For Each psent As DBDictionaryEntry In psd
id = psd.GetAt(psent.Key)
Dim ps As PlotSettings = id.GetObject(OpenMode.ForRead)
Dim curps As PlotSettings = New PlotSettings(False)
curps.CopyFrom(ps)
curps.AddToPlotSettingsDictionary(db)
curps.DowngradeOpen()
Next
trns.Commit()
db.SaveAs(item.ToString, DwgVersion.Current)
db.CloseInput(True)
db.Dispose()
trns.Dispose()
End Using
trans.Dispose()
End Using
Next
psdb.CloseInput(True)
psdb.Dispose()
Me.Close()
End Sub
Solved! Go to Solution.
Re: Fatal Exception when running code
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Hello,
What type of exception are you getting? What line are you getting the exception on?
Re: Fatal Exception when running code
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Hello,
here is the error that I get:
FATAL ERROR: Unhandled Access Violation reading 0xffffffff Exception at db9acc52h
This will either happen after running the code and I start to do something else in autocad or when I close autocad.
I know it has something to so with the code that I am running as it only happens when I run the code.
also it only seems to happen with the portion of copying the pagesetups, I am not sure what the exact cause is, or which line causes it as it happens after running the code and no other errors appear except the one above.
is there a way to have it go line by line and run the code and then even after it runs have it give me any errors witht he code?
Re: Fatal Exception when running code
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
This one is beyond me, I only asked cause your code looks similar to my own and I am encountering a similar issue.
Looks like we are both stuck then!
Re: Fatal Exception when running code
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
My theory is that there is something that is hanging out there that I need to stop or close or something that is causeing the error, but I am not sure if that is the case or even how I can tell what it is.
If I figure out an answer and get this working I will be sure to post it.
Re: Fatal Exception when running code
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Doesn't Using... negate the need to Dispose() those transactions?
If you are going to fly by the seat of your pants, expect friction burns.
Adopt. Adapt. Overcome. Or be overcome.

Re: Fatal Exception when running code
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Use LockDocument if you're working with external drawings,
search for "Unhandled Access Violation" on this NG
~'J'~
C6309D9E0751D165D0934D0621DFF27919
Re: Fatal Exception when running code
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
would I lock each external document?
or where would I use the LockDocument?
Re: Fatal Exception when running code
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Use DocumentLock just for current drawing:
databse db=hostapplicationservices.workingdatabase
document doc=application.documentmanager.mdiactivedocument
using doclock as DocumentLock= doc.LockDocument' current document where you working
using newdb as Database=Readdwg()
using tx as transaction=db.transactionmanager.starttransaction
'' do you work here
end using 'tx
end using 'newdb
end using'' doclock
C6309D9E0751D165D0934D0621DFF27919
Re: Fatal Exception when running code
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
ok, I have changed my code on this and I have finally gotten it to work without crashing autocad.
not sure if it is the dispose parts that I have added
or if it was the exit for parts that I have added, put either way it works without crashing.
hope this helps others
code:
Private Sub copy_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles copy.Click
Using lock As DocumentLock = Application.DocumentManager.MdiActiveDocument.Lock Document
Try
Dim psdb As New Database(False, True)
psdb.ReadDwgFile("M:\CAD Management\CAD Files\Template\Basic.dwt", FileOpenMode.OpenForReadAndReadShare, False, "")
Using trans As Transaction = psdb.TransactionManager.StartTransaction
For Each item In ListBox1.Items
Dim db As New Database(True, False)
db.ReadDwgFile(item.ToString, FileOpenMode.OpenForReadAndAllShare, False, "")
Using trns As Transaction = db.TransactionManager.StartTransaction
Dim psd As DBDictionary = trans.GetObject(psdb.PlotSettingsDictionaryId, OpenMode.ForRead)
Dim id As ObjectId
Dim pd As DBDictionary = trns.GetObject(db.PlotSettingsDictionaryId, OpenMode.ForRead)
For Each entry As DBDictionaryEntry In pd
pd.UpgradeOpen()
pd.Remove(entry.Value)
pd.DowngradeOpen()
Exit For
Next
For Each psent As DBDictionaryEntry In psd
id = psd.GetAt(psent.Key)
Dim ps As PlotSettings = id.GetObject(OpenMode.ForRead)
Dim curps As PlotSettings = New PlotSettings(False)
curps.CopyFrom(ps)
curps.AddToPlotSettingsDictionary(db)
curps.DowngradeOpen()
trns.Commit()
curps.Dispose()
Exit For
Next
psd.Dispose()
pd.Dispose()
End Using
db.SaveAs(item.ToString, DwgVersion.Current)
db.CloseInput(True)
db.Dispose()
Exit For
Next
End Using
psdb.CloseInput(True)
psdb.Dispose()
Catch ex As System.Exception
MsgBox(vbLf & ex.Message)
End Try
End Using
Me.Close()
End Sub


