1

この投稿では: 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
}

}

4

1 に答える 1

2

ネイティブ バッファを解放する必要があります。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);
}

コメントの Jonathan.Peppers の try-finally の提案に従って更新されました

于 2012-04-11T12:12:09.450 に答える