Skip to content

Memory leak in GetRGBByteArrayFromBitmap #3

Description

@ardeoliv

Hi,

I just want to say great project.

By using it, I found out that you forgot to unblock the bitmap in GetRGBByteArrayFromBitmap.

In case you write a routine that needs to decode many QR codes then we get a memory leak problem that is not cleaned. If you ad at the end of your method: bitmap.UnlockBits(bmd); then the problem is solved.

Best regards

Andre

private static void GetRGBByteArrayFromBitmap(Bitmap bitmap, byte[] blue, byte[] green, byte[] red)
{
var bmd = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
var width = bitmap.Width;
var height = bitmap.Height;
var stride = bmd.Stride;
var scan0 = bmd.Scan0;

        for (int y = 0; y < height; y++)
        {
            var row = IntPtr.Add(scan0, (y * stride));
            for (int x = 0; x < width; x++)
            {
                var imgIndex = IntPtr.Add(row, x * Constants.PixelSize);
                var index = (y * width + x) * Constants.Gray8PixelSize;

                blue[index] = Marshal.ReadByte(IntPtr.Add(imgIndex, 0));
                green[index] = Marshal.ReadByte(IntPtr.Add(imgIndex, 1));
                red[index] = Marshal.ReadByte(IntPtr.Add(imgIndex, 2));
            }
        };

        bitmap.UnlockBits(bmd);
        
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions