简体   繁体   中英

csharp unsafe BitmapData Access Memory Leak

        public Point PixelSearchPoint(Bitmap b, Color PixelColor, int Shade_Variation)
    {


        Color Pixel_Color = PixelColor;

        Point Pixel_Coords = new Point(-1, -1);
        using (Bitmap RegionIn_Bitmap = (Bitmap)b.Clone())
        {
            BitmapData RegionIn_BitmapData = RegionIn_Bitmap.LockBits(new Rectangle(0, 0, RegionIn_Bitmap.Width, RegionIn_Bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            int[] Formatted_Color = new int[3] { Pixel_Color.B, Pixel_Color.G, Pixel_Color.R }; //bgr

            unsafe
            {
               for (int y = 0; y < RegionIn_BitmapData.Height; y++)
                {
                    byte* row = (byte*)RegionIn_BitmapData.Scan0 + (y * RegionIn_BitmapData.Stride);

                    for (int x = 0; x < RegionIn_BitmapData.Width; x++)
                    {
                        if (row[x * 3] >= (Formatted_Color[0] - Shade_Variation) & row[x * 3] <= (Formatted_Color[0] + Shade_Variation)) //blue
                        {
                            if (row[(x * 3) + 1] >= (Formatted_Color[1] - Shade_Variation) & row[(x * 3) + 1] <= (Formatted_Color[1] + Shade_Variation)) //green
                            {
                                if (row[(x * 3) + 2] >= (Formatted_Color[2] - Shade_Variation) & row[(x * 3) + 2] <= (Formatted_Color[2] + Shade_Variation)) //red
                                {
                                    Pixel_Coords = new Point(x, y);
                                    RegionIn_Bitmap.Dispose();
                                    goto End;

                                }
                            }
                        }
                    }
                }
            }
        }
        End:
        b.Dispose();
        GC.Collect();
        GC.WaitForPendingFinalizers();
        return Pixel_Coords;
    }

this code search from bitmap find color code

 using (Bitmap b = new Bitmap(NativeHelper.GetOriginalWinScreen(handle)))
                            {


                                Point p = PixelSearchPoint(b, c, 0);
                                if (p.X != -1 && p.Y != -1)
                                {
                                    Logwrite(p.X + "/" + p.Y + " Click With" + c.ToString());
                                    chk = true;
                                    ClickPos(p.X, p.Y);
                                    notecomplete = true;
                                    counttest++;
                                }

                                b.Dispose();

                            }

this code memory so fast leak when access unsafe method GC.Collect and GC.WaitForPendingFinalizers(); Not working on UnSafe method ??

  1. Using GDI+ Capture and this method Not reason of MemoryLeak
  2. using Foreach Find Point from Color using unsafe method << Memory Leak 2GB Over memory and keep debug line message OutOfMemoryException

如果调用RegionIn_Bitmap.LockBits,则需要确保在处理之前调用UnlockBits。

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