c# - 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             {                (int y = 0; y < regionin_bitmapdata.height; y++)                 {                     byte* row = (byte*)regionin_bitmapdata.scan0 + (y * regionin_bitmapdata.stride);                      (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 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 fast leak when access unsafe method gc.collect , gc.waitforpendingfinalizers(); not working on unsafe method ??

  1. using gdi+ capture , method not reason of memoryleak
  2. using foreach find point color using unsafe method << memory leak 2gb on memory , keep debug line message outofmemoryexception

if call regionin_bitmap.lockbits, need make sure call unlockbits before disposing.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -