Author Topic: Why does this tool palette code causse Fatal Error  (Read 1595 times)

0 Members and 1 Guest are viewing this topic.

Sheldon1874

  • Mosquito
  • Posts: 19
Why does this tool palette code causse Fatal Error
« on: June 26, 2015, 06:16:38 AM »
Hi,

When I run the code below I get a Fatal Error: Unhandled Access Violation Reading 0x0000 Exception at 7f37d9c9h.  Any Ideas why and how to fix?
Code: [Select]
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Windows.ToolPalette
Imports System.Runtime

Public Class RPSPalette
    Friend Shared rpsToolps As Autodesk.AutoCAD.Windows.PaletteSet = Nothing
    <CommandMethod("RPSPal")> _
    Public Sub rpsToolPalette()
        Try

            If rpsToolps Is Nothing Then

                Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
                Dim doc As Document = Application.DocumentManager.MdiActiveDocument

                Dim toolPaletteGroupName As String = "RPSToolGroup"

                Dim rpsToolps As New Autodesk.AutoCAD.Windows.PaletteSet(toolPaletteGroupName)
                Dim aboutPalette As usrControlAbout = New usrControlAbout()
                rpsToolps.Add("Abobut Palette", aboutPalette)

            End If
            rpsToolps.Visible = True

        Catch ex As Exception
            Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Error!" & vbLf & ex.Message)
        End Try


    End Sub
End Class
AutoCAD2014/Windows7

Sheldon1874

  • Mosquito
  • Posts: 19
Re: Why does this tool palette code causse Fatal Error
« Reply #1 on: June 26, 2015, 06:28:36 AM »
Doh,

Code: [Select]
            If rpsToolps Is Nothing Then

                Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
                Dim doc As Document = Application.DocumentManager.MdiActiveDocument

                Dim toolPaletteGroupName As String = "RPSToolGroup"

                Dim rpsToolps As New Autodesk.AutoCAD.Windows.PaletteSet(toolPaletteGroupName)
                Dim aboutPalette As usrControlAbout = New usrControlAbout()
                rpsToolps.Add("Abobut Palette", aboutPalette)
                rpsToolps.Visible = True
            End If
Is the answer.

n.yuan

  • Bull Frog
  • Posts: 348
Re: Why does this tool palette code causse Fatal Error
« Reply #2 on: June 26, 2015, 09:29:09 AM »
I do not think your answer to your first post is correct, at least syntex-wise.

This issue could be because of using VB.NET without set "Option Strict" flag: you declared a "Shared" variable at class level "rpsToolps" as Nothing, then in the If...Then... you declared another local variable also named as "rptToolps" (if you use C#, this would not be allowed, and then you would have noticed your error immediately), that was why your own answer was to move "rpsToolps.Visible=True" inside the If...Then... statement and it seemed working. The net effect of your "solution" is that you never have a Shared class-level reference pointing to an instance of your PaletteSet, and each time you run your command, a new instance of the PaletteSet is created. Since when user clicking "x" to close PaletteSet does not actually dispose it but simply sets it to invisible, running your command multiple times would create many instances of the PaletteSet with all previously opened PaletteSets sitting in memory as invisible, useless garbage, you are creating a case of "memory leak" inside AutoCAD process.

The correct way is to change (inside the If...Then...End If)

Dim rpsToolps As New Autodesk.AutoCAD.Windows.PaletteSet(toolPaletteGroupName)

to

rpsToolps = New Autodesk.AutoCAD.Windows.PaletteSet(toolPaletteGroupName)

and leave

rpsToolps.Visible = True

outside the If...Then..., as you did originally.



« Last Edit: June 26, 2015, 09:32:22 AM by n.yuan »

Sheldon1874

  • Mosquito
  • Posts: 19
Re: Why does this tool palette code causse Fatal Error
« Reply #3 on: June 26, 2015, 11:55:35 AM »
Thank you for pointing out the error of my ways.  I was actually just wondering why I could still have multiple occurrences of the palette. Option Strict now enabled.

I am actually converting a fair amount from VBA to .net.  As a part-time "hacker" in AutoCAD and a full-time Land Surveyor, would it be sensible to make the effort to go to C# do you think?

Thanks again, as usual I have made some good learning.