• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Active Contributor
    Posts: 45
    Registered: ‎04-20-2012
    Accepted Solution

    Fatal Exception when running code

    211 Views, 12 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

     

    Please use plain text.
    Member
    Posts: 4
    Registered: ‎05-25-2012

    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?

    Please use plain text.
    Active Contributor
    Posts: 45
    Registered: ‎04-20-2012

    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?

    Please use plain text.
    Member
    Posts: 4
    Registered: ‎05-25-2012

    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!

    Please use plain text.
    Active Contributor
    Posts: 45
    Registered: ‎04-20-2012

    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.

    Please use plain text.
    *Expert Elite*
    dgorsman
    Posts: 3,293
    Registered: ‎10-12-2006

    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.


    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,337
    Registered: ‎10-08-2008

    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
    Please use plain text.
    Active Contributor
    Posts: 45
    Registered: ‎04-20-2012

    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?

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,337
    Registered: ‎10-08-2008

    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
    Please use plain text.
    Active Contributor
    Posts: 45
    Registered: ‎04-20-2012

    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

     

    Please use plain text.