.NET

Reply
Active Contributor
bkenyon13
Posts: 45
Registered: ‎04-20-2012
Message 1 of 14 (438 Views)
Accepted Solution

Fatal Exception when running code

438 Views, 13 Replies
05-25-2012 06:56 AM

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

 

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.LockDocument
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

 

Member
CarterN
Posts: 4
Registered: ‎05-25-2012
Message 2 of 14 (434 Views)

Re: Fatal Exception when running code

05-25-2012 06:58 AM in reply to: bkenyon13

Hello,

 

What type of exception are you getting? What line are you getting the exception on?

Active Contributor
bkenyon13
Posts: 45
Registered: ‎04-20-2012
Message 3 of 14 (431 Views)

Re: Fatal Exception when running code

05-25-2012 07:18 AM in reply to: CarterN

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?

Member
CarterN
Posts: 4
Registered: ‎05-25-2012
Message 4 of 14 (429 Views)

Re: Fatal Exception when running code

05-25-2012 07:26 AM in reply to: bkenyon13

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!

Active Contributor
bkenyon13
Posts: 45
Registered: ‎04-20-2012
Message 5 of 14 (425 Views)

Re: Fatal Exception when running code

05-25-2012 07:33 AM in reply to: CarterN

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.

*Expert Elite*
dgorsman
Posts: 5,611
Registered: ‎10-12-2006
Message 6 of 14 (421 Views)

Re: Fatal Exception when running code

05-25-2012 07:34 AM in reply to: bkenyon13

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.
A good question will be halfway to a good answer.


*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 7 of 14 (421 Views)

Re: Fatal Exception when running code

05-25-2012 07:38 AM in reply to: bkenyon13

Use LockDocument if you're working with external drawings,

search for "Unhandled Access Violation" on this NG

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Contributor
bkenyon13
Posts: 45
Registered: ‎04-20-2012
Message 8 of 14 (417 Views)

Re: Fatal Exception when running code

05-25-2012 08:04 AM in reply to: Hallex

would I lock each external document?

 

or where would I use the LockDocument?

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 9 of 14 (415 Views)

Re: Fatal Exception when running code

05-25-2012 08:25 AM in reply to: bkenyon13

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
Active Contributor
bkenyon13
Posts: 45
Registered: ‎04-20-2012
Message 10 of 14 (412 Views)

Re: Fatal Exception when running code

05-25-2012 08:46 AM in reply to: Hallex

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.LockDocument
            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

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!