我用Zedgraph框架构建了一个简单的WinForms应用程序。我显示一个图表并添加一个事件以将随机点添加到该图,因此这是一个动画图。
该程序在Windows上可以运行,但是我在树莓派上遇到单声道问题。当我启动应用程序时,程序一开始运行良好。但是随着时间的推移,我注意到程序的内存随着时间的增加而增加。因此,我决定使用Mono执行一些测试(在Windows上使用dotMemory)和一些堆快照,但是没有什么与内存泄漏相符的。
过了一会儿,我的应用程序在树莓派上崩溃了。
Error in `mono': free(): invalid pointer: 0x028c2f6c
Stacktrace:
at <unknown> <0xffffffff>
at (wrapper managed-to-native) System.Drawing.GDIPlus.GdipDeleteStringFormat (intptr) <0x00037>
at System.Drawing.StringFormat.Dispose (bool) [0x00012] in <0de15b16c04f4218bdea63901051ecc9>:0
at System.Drawing.StringFormat.Finalize () [0x00000] in <0de15b16c04f4218bdea63901051ecc9>:0
at (wrapper runtime-invoke) object.runtime_invoke_virtual_void__this__ (object,intptr,intptr,intptr) [0x0001e] in <ee725f74938b4972b8a81df0be67ae23>:0
=================================================================
Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================
一些堆镜头:
Heap shot 0 at 28.839 secs: size: 1377800, object count: 19166, class count: 654, roots: 0
Bytes Count Average Class name
294560 5015 58 System.String
255392 2 127696 System.Windows.Forms.XEvent[]
75600 27 2800 System.Byte[]
62656 979 64 System.EventHandler
39408 1642 24 System.RuntimeType
37240 585 63 System.Object[]
33200 50 664 System.Windows.Forms.ImplicitHScrollBar
33200 50 664 System.Windows.Forms.ImplicitVScrollBar
32664 1361 24 System.ComponentModel.EventHandlerList.ListEntry
29464 120 245 System.Collections.Hashtable.bucket[]
25176 208 121 System.Int32[]
22720 355 64 System.Windows.Forms.MouseEventHandler
22528 44 512 System.Windows.Forms.SizeGrip
19680 820 24 System.Collections.ArrayList
19584 98 199 System.Windows.Forms.Hwnd
17832 4 4458 System.Collections.Generic.Dictionary.Entry<System.String,System.String>[]
15872 31 512 System.Windows.Forms.TableLayoutPanel
13072 108 121 System.Char[]
12480 520 24 System.Reflection.MonoMethod
8160 20 408 System.Windows.Forms.PictureBox
8000 1 8000 System.Collections.Generic.Dictionary.Entry<System.Int32,System.String>[]
7616 119 64 System.Collections.Hashtable
6960 145 48 System.Reflection.MonoProperty
6848 107 64 System.Windows.Forms.KeyEventHandler
6848 107 64 System.Windows.Forms.Timer
6400 40 160 System.Windows.Forms.MenuItem
6144 64 96 System.TimeZoneInfo.AdjustmentRule
6120 15 408 System.Windows.Forms.Label
5960 4 1490 System.UInt32[]
5952 124 48 System.Collections.Generic.Dictionary<System.Object,System.Int32>
...
Heap shot 38 at 564.759 secs: size: 1044896, object count: 21390, class count: 507, roots: 0
Bytes Count Average Class name
363128 6208 58 System.String (bytes: -24, count: +0)
115384 1014 113 System.Byte[] (bytes: +0, count: +0)
87072 2308 37 System.Object[] (bytes: +0, count: +0)
75480 3145 24 System.Collections.ArrayList (bytes: +0, count: +0)
57816 84 688 System.Collections.Hashtable.bucket[] (bytes: +0, count: +0)
31072 971 32 System.Windows.Forms.Matchlet (bytes: +0, count: +0)
29760 1240 24 System.RuntimeType (bytes: +0, count: +0)
27456 1716 16 System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry (bytes: +0, count: +0)
17616 2 8808 System.Windows.Forms.XEvent[] (bytes: +0, count: +0)
12168 105 115 System.Char[] (bytes: +0, count: +0)
12040 301 40 ZedGraph.PointPair (bytes: +0, count: +0)
11392 178 64 System.EventHandler (bytes: +0, count: +0)
9960 415 24 System.Windows.Forms.Match (bytes: +0, count: +0)
8928 38 234 System.Int32[] (bytes: -256, count: -1)
7248 302 24 System.ComponentModel.EventHandlerList.ListEntry (bytes: +0, count: +0)
6144 64 96 System.TimeZoneInfo.AdjustmentRule (bytes: +0, count: +0)
5960 4 1490 System.UInt32[] (bytes: +0, count: +0)
5632 44 128 ZedGraph.Fill (bytes: +0, count: +0)
5448 227 24 System.Reflection.MonoMethod (bytes: +0, count: +0)
5312 83 64 System.Collections.Hashtable (bytes: +0, count: +0)
5184 324 16 System.Drawing.Drawing2D.Matrix (bytes: +1152, count: +72)
4928 77 64 System.Windows.Forms.MouseEventHandler (bytes: +0, count: +0)
4336 19 228 System.UInt64[] (bytes: +0, count: +0)
4208 66 63 System.String[] (bytes: +0, count: +0)
4080 85 48 System.Configuration.SectionInfo (bytes: +0, count: +0)
4040 2 2020 System.Collections.Generic.KeyValuePair<System.DateTime,System.TimeType>[] (bytes: +0, count: +0)
4024 1 4024 System.Globalization.InternalEncodingDataItem[] (bytes: +0, count: +0)
3984 6 664 System.Windows.Forms.ImplicitHScrollBar (bytes: +0, count: +0)
3984 6 664 System.Windows.Forms.ImplicitVScrollBar (bytes: +0, count: +0)
3840 24 160 System.Windows.Forms.MenuItem (bytes: +0, count: +0)
我的代码:
public partial class Form1 : Form
{
RollingPointPairList listOfPoints = new RollingPointPairList(301);
System.Timers.Timer TimerForEvent = new System.Timers.Timer { Interval = 200 };
ElapsedEventHandler eventTimer;
public Form1()
{
InitializeComponent();
eventTimer = EventLiveChart;
CreateGraphicsFromSensor();
}
private void EventLiveChart(object sender, ElapsedEventArgs e)
{
try
{
Random r = new Random();
listOfPoints.Add(DateTime.Now.ToOADate(), r.Next(5));
DateTime now = DateTime.Now;
zedGraphControl.GraphPane.XAxis.Scale.Max = now.ToOADate();
zedGraphControl.GraphPane.XAxis.Scale.Min = DateTime.Now.AddSeconds(-60).ToOADate();
zedGraphControl.AxisChange();
zedGraphControl.Invalidate();
}
catch (Exception ex)
{
Console.Write(ex);
}
}
private void CreateGraphicsFromSensor()
{
try
{
// initialization chart -------------------------
var myPane = zedGraphControl.GraphPane;
if (myPane.CurveList.Count > 0)
{
myPane.CurveList.RemoveAt(0);
TimerForEvent.Stop();
}
myPane.Title.Text = "Chart";
myPane.XAxis.Title.Text = "Time";
myPane.XAxis.Scale.MaxAuto = true;
int numberOfDivision = 4;
myPane.XAxis.Scale.MinorStep = (int)60 / numberOfDivision;
myPane.XAxis.Scale.MajorStep = (int)60 / numberOfDivision;
myPane.XAxis.Scale.Format = "HH:mm:ss";
var myCurve = myPane.AddCurve("Unit", listOfPoints, Color.Red, SymbolType.Diamond);
myCurve.Symbol.Fill = new Fill(Color.Red);
myPane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45F);
zedGraphControl.Refresh();
// -----------------------------------
TimerForEvent.Elapsed += eventTimer;
TimerForEvent.Start();
}
catch (Exception ex)
{
Console.Write(ex.ToString());
}
}
}
}
谢谢!