简体   繁体   中英

The program exited with code-1073741819 (0xc0000005) 'Access violation'

I'm using graphicPath to draw points and lines in the panel1. The code is as follows:

private void panel1_Paint_1(object sender, PaintEventArgs e)
    {                 
        Graphics G = e.Graphics;
        GraphicsPath gp = new GraphicsPath();

        foreach (var line in tockeKoordinate)
        {
            gp.AddLine((float) (line.startX), (float) (line.startY), (float) (line.endX), (float) (line.endY));
            gp.CloseFigure();
        }
        var rect = gp.GetBounds();
        var scale = Math.Min(1f * (int)(panel1.ClientSize.Width) / rect.Width,
                               1f * (int)(panel1.ClientSize.Height) / rect.Height);          

        using (Pen pen = new Pen(Color.Black, 0.0001f))
                {
                    G.SmoothingMode = SmoothingMode.AntiAlias;
                    G.Clear(Color.White);
                    G.TranslateTransform(0, +panel1.ClientSize.Height);
                    G.ScaleTransform(scale, -scale);
                    G.TranslateTransform(-rect.X, -rect.Y);
                    G.DrawPath(pen, gp);
               }
        if(checkBox1.Checked)
        {
            gp.ClearMarkers();
            foreach (var line2 in tockeZelene)
            {
                gp.AddLine((float)(line2.startX), (float)(line2.startY), (float)(line2.endX), (float)(line2.endY));
                gp.CloseFigure();
            }

            using (pen2);
            {
                G.DrawPath(pen2, gp);   <--- access violation here
            }  
        }      
    }

Basically i have two Lists : Tockekoordinate and tockeZelena . The first one contains all of the points, and the second one contains about 30% of the first one's points, which I want to paint green using my pen2 which is initialised at the beginning.

Assuming checkbox1 is checked, I run all the points in order to get the rectangle GetBounds , so I can scale the panel1 with the point coordinates.

Then the checkbox1.checked part comes and the application exits on the marked line.

Does anyone know what could cause this? Or at least know a way how to set up VS to show me more information about said error?

This line below is a bit suspicious..

 using (pen2); //<--this one!!!
 {
      G.DrawPath(pen2, gp);
 }

First, DrawPath will always throw an exception because you will be using a disposed object. To address that, remove the semi-colon...

using (pen2)
 {
      G.DrawPath(pen2, gp);
 }

Second, what's pen2 ? who's using it? If it's being used by another thread then an access violation will occur because your use of pen2 is not thread-safe.

And lastly, DON'T dispose a global object ( pen2 ) from within the Paint event unless you are recreating it all the time because this event will be triggered everytime your control needs to redraw its surface. That means, the second time your control needs to redraw, it will be using a disposed object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM