siyamed / android-shape-imageview

Custom shaped android imageview components
MIT License
2.66k stars 599 forks source link

Posible memory leak issue #17

Open kancic opened 9 years ago

kancic commented 9 years ago

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);
            ...
        }
    }
alfdev commented 8 years ago

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

kancic commented 8 years ago

No, sorry. This solution worked for me.

alfdev commented 8 years ago

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();
}
kancic commented 8 years ago

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.

Fatimamostafa commented 6 years ago

Did anyone solve this memory issue?

BilalAsif25 commented 5 years ago

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