프로그램에서 메모리 누수가 발생했습니다. 그래서 누수를 찾을 수 dotMemory을 사용하고,이 누출의 원인이되는 기능입니다 같습니다마샬링 (Marshal)시 메모리 누수가 발생했습니다. 복사
private void LoadBits()
{
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bm.Width, bm.Height);
bmpData = bm.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bm.PixelFormat);
stride = bmpData.Stride;
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
byteCount = Math.Abs(bmpData.Stride) * bm.Height;
bytes = new byte[byteCount];
// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, bytes, 0, byteCount);
}
가 그리고이 나는 비트의 잠금을 해제하는 방법.
private void SaveBits()
{
// Update Stuff
IntPtr ptr = bmpData.Scan0;
System.Runtime.InteropServices.Marshal.Copy(bytes, 0, ptr, byteCount);
bm.UnlockBits(bmpData);
}
이 클래스에 IDisposable 인터페이스를 구현했습니다. SaveBits를 호출하기 때문에 SaveBits를 호출하는 것을 잊어 버린 경우에도 GC가이를 수행해야합니다. 그리고 예, bm.Dispose()를 호출하고 Dispose 메서드에서 모든 것을 null로 설정합니다.
LoadBits 및 SaveBits에서 다른 변수를 사용하고 있습니다. 하나는'_bm'이고 다른 하나는'bm'입니다, 당신은 잘못된 객체에서 잠금 해제 비트를 할 수 있습니까? 또한'SaveBits()'가 호출되기 전에'LoadBits()'가 두 번 이상 호출 될 수 있으며 또한 메모리 누수를 일으킬 수 있습니다. 'SaveBits()'의 마지막에'bmpData = null'을 넣고'If (bmpData! = null)'를'LoadBits()'의 시작 부분에 놓고 예외가 던져 지는지 확인하십시오. –
질문을 게시 한 후 리팩터링했습니다. 그래서 "LoadBits"는 오래된 var 이름을 보여주었습니다. 업데이트하도록 허용하십시오. – Trauer
널 검사를하고 예외를 throw하면 던지지 않습니까? –