android-shape-imageview icon indicating copy to clipboard operation
android-shape-imageview copied to clipboard

Posible memory leak issue

Open kancic opened this issue 10 years ago • 6 comments

Hello,

First of all great library, thank you. I think you have a memory leak in your code that is causing OutOfMemory errors. I have added these lines of code in the PorterImageView class and they are gone now. I'm not really sure if they are all needed.

    @Override
    protected void onDetachedFromWindow()
    {
        super.onDetachedFromWindow();
        if (maskBitmap != null && !maskBitmap.isRecycled()) maskBitmap.recycle();
        if (drawableBitmap != null && !drawableBitmap.isRecycled()) drawableBitmap.recycle();
    }
 private void createMaskCanvas(int width, int height, int oldw, int oldh)
    {
            ...
            Bitmap oldBitmap = maskBitmap;
            maskBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            if (oldBitmap != null && oldBitmap != maskBitmap && !oldBitmap.isRecycled()) oldBitmap.recycle();
            maskCanvas.setBitmap(maskBitmap);

            ...

            oldBitmap = drawableBitmap;
            drawableBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            if (oldBitmap != null && oldBitmap != drawableBitmap && !oldBitmap.isRecycled()) oldBitmap.recycle();
            drawableCanvas.setBitmap(drawableBitmap);
            ...
        }
    }

kancic avatar Feb 13 '15 11:02 kancic

Hi r1m,

i'm trying your solution but i get this exception: java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@7a2da54

Have you found other solution?

Thanks

alfdev avatar Nov 10 '15 13:11 alfdev

No, sorry. This solution worked for me.

kancic avatar Nov 10 '15 14:11 kancic

I applies this changes:

private void createMaskCanvas(int width, int height, int oldw, int oldh) {
    boolean sizeChanged = width != oldw || height != oldh;
    boolean isValid = width > 0 && height > 0;
    if(isValid && (maskCanvas == null || sizeChanged)) {
        maskCanvas = new Canvas();
        Bitmap oldBitmap = maskBitmap;
        maskBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        if (oldBitmap != null && oldBitmap != maskBitmap && !oldBitmap.isRecycled()) oldBitmap.recycle();
        maskCanvas.setBitmap(maskBitmap);
        maskPaint.reset();
        paintMaskCanvas(maskCanvas, maskPaint, width, height);
        drawableCanvas = new Canvas();
        oldBitmap = drawableBitmap;
        drawableBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        if (oldBitmap != null && oldBitmap != drawableBitmap && !oldBitmap.isRecycled()) oldBitmap.recycle();
        drawableCanvas.setBitmap(drawableBitmap);
        drawablePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        invalidated = true;
    }
}

And this:

@Override
protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();

    if (maskBitmap != null && !maskBitmap.isRecycled()) maskBitmap.recycle();
    if (drawableBitmap != null && !drawableBitmap.isRecycled()) drawableBitmap.recycle();
}

alfdev avatar Nov 10 '15 14:11 alfdev

It's possible I missed something because I have a very simple use case. I only show one shape image view at the time so maybe if you use something more complex my code doesn't work.

kancic avatar Nov 10 '15 14:11 kancic

Did anyone solve this memory issue?

Fatimamostafa avatar Feb 28 '18 05:02 Fatimamostafa

I am getting the same issue, i just used on a single imageview in my whole project!

BilalAsif25 avatar Nov 30 '18 12:11 BilalAsif25