![](/img/trans.png)
[英]C# app "has exited with code -1073741819 (0xc0000005) 'Access violation'" VS
[英]The program exited with code-1073741819 (0xc0000005) 'Access violation'
我正在使用graphicPath在panel1中繪制點和線。 代碼如下:
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
}
}
}
基本上我有兩個Lists
: Tockekoordinate
和tockeZelena
。 第一個包含所有點,第二個包含第一個點的約30%,我想使用在開始時初始化的pen2將其塗成綠色。
假設checkbox1被選中,我運行所有點以獲取矩形GetBounds
,因此我可以使用點坐標縮放panel1。
然后出現checkbox1.checked部分,應用程序在標記的行退出。
有誰知道是什么原因造成的? 或者至少知道一種方法來設置VS,以向我顯示有關所述錯誤的更多信息?
下面的這一行有點可疑。
using (pen2); //<--this one!!!
{
G.DrawPath(pen2, gp);
}
首先, DrawPath
將始終引發異常,因為您將使用已處置的對象。 要解決此問題,請刪除分號...
using (pen2)
{
G.DrawPath(pen2, gp);
}
第二, pen2
什么? 誰在使用它? 如果另一個線程正在使用它,則將發生訪問沖突,因為您使用的pen2
不是線程安全的。
最后,除非您一直都在重新創建它,否則pen2
從Paint事件中pen2
一個全局對象( pen2
),因為每次您的控件需要重繪其表面時都會觸發此事件。 這意味着,您的控件第二次需要重繪時,它將使用已處置的對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.