ZXing 仍然有内存泄漏.即使在发布全球分配之后

本文关键字:布全球 分配 之后 内存 泄漏 ZXing | 更新日期: 2023-09-27 18:30:39

在这篇文章中:Zxing的System.Drawing.Bitmap中可能存在内存泄漏的问题被问到关于ZXing库中的内存泄漏的问题。我已经下载并修改了具有免费分配内存的库。我什至在适用的地方引入了 using(){} 语句,但我仍然遇到内存泄漏。

我有一个怀疑。Marshal.Copy 是否可能做更多的事情,而不仅仅是将数据从源复制到目标。我是否还必须在复制后释放目的地?

正如您在下面的代码中看到的,我什至尝试只分配一次缓冲区,并且仅在请求大于前一个图像时才重新分配它,但这并没有解决问题。

问候保罗

我修改的代码:

using System;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace System.Drawing
{
public class Bitmap : IDisposable
{
    byte[] pixelData = new byte[0];
    int width = 0;
    int height = 0;
    static IntPtr m_BufferPointer = default(IntPtr);
    static int m_Size;
    /// <summary>
    /// Reallocs the buffer when it becomes too small
    /// </summary>
    private IntPtr ReallocBuffer(int size)
    {
        if(m_BufferPointer != default(IntPtr))
        {
            if(m_Size < size)
            {
                Marshal.FreeHGlobal(m_BufferPointer);
                m_BufferPointer = Marshal.AllocHGlobal(size);
            }
        }
        else
        {
            m_BufferPointer = Marshal.AllocHGlobal(size);
        }
        m_Size = size;
        return m_BufferPointer;
    }
    public Bitmap (UIImage image)
    {
        UIImage backingImage = image;
        IntPtr rawData;
        using (CGImage imageRef = backingImage.CGImage)
        {
            width = imageRef.Width;
            height = imageRef.Height;
            using (CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB ())
            {
                int size = height * width * 4;
                rawData = ReallocBuffer(size); //Marshal.AllocHGlobal (height * width * 4);
                using (CGContext context = new CGBitmapContext (rawData, width, height, 8, 4 * width, colorSpace, CGImageAlphaInfo.PremultipliedLast))
                {
                    context.DrawImage (new RectangleF (0.0f, 0.0f, (float)width, (float)height), imageRef);
                    pixelData = new byte[height * width * 4];
                    Marshal.Copy (rawData, pixelData, 0, pixelData.Length);
                }
            }
        }
    }
    private static int CountCalled;
    private static int LastCountCalled = 20000000; //30411000;
    public Color GetPixel (int x, int y)
    {
        try
        {               
            CountCalled++;
            if (CountCalled - LastCountCalled > 100000)
            {
                Debug.WriteLine (CountCalled);
                LastCountCalled += 1000000;
            }
            byte bytesPerPixel = 4;
            int bytesPerRow = width * bytesPerPixel;
            int rowOffset = y * bytesPerRow;
            int colOffset = x * bytesPerPixel;
            int pixelDataLoc = rowOffset + colOffset;
            Color ret = Color.FromArgb (pixelData [pixelDataLoc + 3], pixelData [pixelDataLoc + 0], pixelData [pixelDataLoc + 1], pixelData [pixelDataLoc + 2]);
            return ret;
        }
        catch (Exception ex)
        {
            Console.WriteLine ("Req:  {0}x{1}", x, y);
            throw ex;
        }
    }
    #region IDisposable implementation
    public void Dispose ()
    {
        pixelData = null;
        GC.Collect(0);
    }
    #endregion
}

}

ZXing 仍然有内存泄漏.即使在发布全球分配之后

你需要释放原生缓冲区,CGBitmapContext 不会为你做:

IntPtr rawData = Marshal.AllocHGlobal (height * width * 4);
try {
    using (CGContext context = new CGBitmapContext (rawData, width, height, 8, 4 * width, colorSpace, CGImageAlphaInfo.PremultipliedLast))
    {
        context.DrawImage (new RectangleF (0.0f, 0.0f, (float)width, (float)height), imageRef);
        pixelData = new byte[height * width * 4];
        Marshal.Copy (rawData, pixelData, 0, pixelData.Length);
    }
} finally {
    Marshal.FreeHGlobal (rawData);
}

根据乔纳森更新.Peppers 尝试-最终建议在评论中