Possible memory leak in Zxing's System.Drawing

2019-04-09 11:53发布

I am currently working with Monotouch. I have an application which opens the camera and then needs to process the image that is currently being captured. I am using code which is very similar to https://github.com/reinforce-lab/com.ReinforceLab.MonoTouch.Controls/tree/master/VideoCaptureSample/

The difference of interest between my code and theirs is that in OverlayView.cs which is placed over my UIImagePickerController however, I have the following code:

using (var screenImage = CGImage.ScreenImage.WithImageInRect(Frame))
{
    _imageView = UIImage.FromImage(screenImage);
    Bitmap srcbitmap = new System.Drawing.Bitmap(_imageView);
}

However running this code will cause memory to continuously fill up until the application closes. It seems as though the System.Drawing.Bitmap is never disposed of. I am using ZXing.monotouch so I need the bitmap so that I can create a bytes[] of the bitmap to pass into an RGBLuminanceSource

Any ideas on how I can get this to play nicely?

1条回答
在下西门庆
2楼-- · 2019-04-09 12:25

If you're using the code from [1] then it looks rather incomplete. Nothing frees the memory allocated from AllocHGlobal, which would lead to out-of-memory condition.

This is not a MonoTouch bug - the zxing code should implement IDisposable on the Bitmap class and ensure everything gets freed when disposed (or when the GC runs its finalizer).

You can either complete the class (i.e. implement IDisposable) or copy/paste the part that provides you a byte[] buffer containing the image (and free the unmanaged memory once it's copied into the managed buffer).

[1] https://github.com/JohnACarruthers/zxing.MonoTouch/blob/master/Bitmap.cs

查看更多
登录 后发表回答