.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Fatal Exception when running code

13 REPLIES 13
SOLVED
Reply
Message 1 of 14
bkenyon13
942 Views, 13 Replies

Fatal Exception when running code

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

 

13 REPLIES 13
Message 2 of 14
CarterN
in reply to: bkenyon13

Hello,

 

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

Message 3 of 14
bkenyon13
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?

Message 4 of 14
CarterN
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!

Message 5 of 14
bkenyon13
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.

Message 6 of 14
dgorsman
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.
"I don't know" is the beginning of knowledge, not the end.


Message 7 of 14
Hallex
in reply to: bkenyon13

Use LockDocument if you're working with external drawings,

search for "Unhandled Access Violation" on this NG

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 8 of 14
bkenyon13
in reply to: Hallex

would I lock each external document?

 

or where would I use the LockDocument?

Message 9 of 14
Hallex
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
Message 10 of 14
bkenyon13
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

 

Message 11 of 14
Hallex
in reply to: bkenyon13

Good point

Thanks for the sharing with community

Cheers 🙂

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 12 of 14
ChristianBlei
in reply to: bkenyon13

Hi,

 

does not the "Exit for" without any condition cause the code to leave the loop inside the first round? Maybe you are not going to reach your problem anymore......

 

Christian Blei

Christian Blei
CBT Christian Blei Tools
christianblei.de
youtube.com/channel/UCxjA_NbeScQy9C0Z1xjwXpw
Message 13 of 14
bkenyon13
in reply to: ChristianBlei

what I found was while this solved the fatal error it stopped copying the pagesetups.

So I did some troubleshooting and realized the the curps.dispose was the issue with it not copying the pagesetup

 

however I also discovered that if I removed the exit for and/or the curps.dispose I would get the fatal error again.

so in order for my code to work and copy the pagesetups without giving me the fatal error I had to stop/unload the curps without using curps.dispose and exit for.

 

so a long story somewhat short I did this:

using curps as plotsettings = new plotsettings(false)

(did work)

end using

 

This allowed me to copy the pagesetups as I wanted and not get a fatal error in the resulting code I removed all exit for's.  What I learned from this is that you have to make sure that everything gets unloaded even the littlest thing still hanging out there can cause an error.

 

Hope this helps.

Message 14 of 14
Bauron
in reply to: bkenyon13

Hello

 

I understand that this thread is over a year old, and am not sure if posting here will still elicit a response. 

If it does not, I will post again. 

 

it seems that kenyon is saying that you havet to dispose of the variables as soon as you are done using it. 

That means assigning the plot setting to a variable is out of question?

I am trying to write a function that simply returns the plot settings to the mother routine, based on the paper size the user picks in a form. 

 

I am having no luck in preventing the autocad crash once it cycles through two or three of the page setups. 

Intially, it cycled through all layout page setups. 

And trying to get it to read any page setup that is not in a layout is out of the question. 

 

Please help. 

 

Bauron

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost