我有這種方法,我從計時器滴答事件每X秒調用方法。 在開始的幾秒鐘內沒有任何問題,但是在20秒後或者顯示異常。爲什麼我會發生OutOfMemoryException?
private void ReturnTexture(Texture texture_take, List<Point> lstPnt, float[] anglArr, float angle)
{
int i, j, bytes = 2048 * 512, stride = 2048;
GraphicsStream textureStream;
byte[] rgbValues = new byte[bytes];
//sets texture to complete transparent
GCHandle gch = GCHandle.Alloc(rgbValues, GCHandleType.Pinned);
MemSet(gch.AddrOfPinnedObject(), 0x0, rgbValues.Length);
//add 90 degrees because the cone starts at 90
angle += 90F + 23.75F;
//checks all points and draws yellow only those how are inside the cone
if (lstPnt != null)
{
for (i = 0; i < lstPnt.Count - 1; i++)
{
if (anglArr[i] <= angle && anglArr[i] >= angle - 47.5F) //if point angle is inside cone. Cone angle is 47.5 degrees
{
j = lstPnt[i].Y * stride + lstPnt[i].X * 4;
//yellow
rgbValues[j + 0] = (byte)0;
rgbValues[j + 1] = (byte)255;
rgbValues[j + 2] = (byte)255;
rgbValues[j + 3] = (byte)255;
}
else
{
}
}
}
唯一的例外是上線:
byte[] rgbValues = new byte[bytes];
當異常發生,我看到變量字節包含:1048576 而rgbValues是空
我異常之前使用斷點發生並看到以字節爲單位的值是相同的:1048576但異常僅在20秒左右後才發生,而不僅僅是在開始時。
類型 '的System.OutOfMemoryException' 的未處理的異常發生在天氣Radar.exe
System.OutOfMemoryException was unhandled
HResult=-2147024882
Message=Exception of type 'System.OutOfMemoryException' was thrown.
Source=Weather Radar
StackTrace:
at Weather_Radar.Form1.ReturnTexture(Texture texture_take, List`1 lstPnt, Single[] anglArr, Single angle) in d:\C-Sharp\Weather Radar\Weather Radar\Weather Radar\Form1.cs:line 314
at Weather_Radar.Form1.timer1_Tick(Object sender, EventArgs e) in d:\C-Sharp\Weather Radar\Weather Radar\Weather Radar\Form1.cs:line 59
at System.Windows.Forms.Timer.OnTick(EventArgs e)
at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Weather_Radar.Program.Main() in d:\C-Sharp\Weather Radar\Weather Radar\Weather Radar\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
314線在Form1是:字節[] rgbValues =新的字節[字節];
form1中的第59行是:ReturnTexture(scannedCloudsTexture,cloudPoints,angleArray,angleF);
這是計時器滴答事件代碼:
static float angle_ = 0.0F;
static float angleF = 0.0F;
private void timer1_Tick(object sender, EventArgs e)
{
if (angleF > 360F)
{
angleF -= 360F;
}
ReturnTexture(scannedCloudsTexture, cloudPoints, angleArray, angleF);
DisplayOnScreen(angle_);
angle_ += (float)(Math.PI * 1d/180d);
angleF += 1.0F;
}
的GCHandle免受垃圾收集器,所以你應該釋放內存使用調用GCHandle.Free方法,當手柄沒有更多必要 – InferOn 2014-09-30 07:20:19
我敢打賭,每個時鐘滴答您的RAM增加。嘗試重複使用您的數組,而不是在每次打勾時創建 – Reniuz 2014-09-30 07:21:27