Skip to content

Commit

Permalink
Sometimes the Bitmap is released before the canvas... Fixes #292
Browse files Browse the repository at this point in the history
 - this is fairly scary since my view has a strong reference to it
 - so we first check to make sure that the bitmap's handle is not nullptr
  • Loading branch information
mattleibow committed Jun 30, 2017
1 parent fb0eee9 commit 1ee8d2b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected override void OnDraw(Canvas canvas)
return;

// create the bitmap data if we need it
if (bitmap == null || bitmap.Width != info.Width || bitmap.Height != info.Height)
if (bitmap == null || bitmap.Handle == IntPtr.Zero || bitmap.Width != info.Width || bitmap.Height != info.Height)
{
FreeBitmap();
bitmap = Bitmap.CreateBitmap(info.Width, info.Height, Bitmap.Config.Argb8888);
Expand Down Expand Up @@ -141,7 +141,10 @@ private void FreeBitmap()
if (bitmap != null)
{
// free and recycle the bitmap data
bitmap.Recycle();
if (bitmap.Handle != IntPtr.Zero && !bitmap.IsRecycled)
{
bitmap.Recycle();
}
bitmap.Dispose();
bitmap = null;
}
Expand Down
10 changes: 7 additions & 3 deletions source/SkiaSharp.Views/SkiaSharp.Views.Android/SKSurfaceView.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Android.Content;
using System;
using Android.Content;
using Android.Graphics;
using Android.Runtime;
using Android.Util;
Expand Down Expand Up @@ -77,7 +78,7 @@ protected override void Dispose(bool disposing)
private void CreateBitmap(int width, int height)
{
// create the bitmap data
if (bitmap == null || bitmap.Width != width || bitmap.Height != height)
if (bitmap == null || bitmap.Handle == IntPtr.Zero || bitmap.Width != width || bitmap.Height != height)
{
FreeBitmap();
bitmap = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888);
Expand All @@ -89,7 +90,10 @@ private void FreeBitmap()
if (bitmap != null)
{
// free and recycle the bitmap data
bitmap.Recycle();
if (bitmap.Handle != IntPtr.Zero && !bitmap.IsRecycled)
{
bitmap.Recycle();
}
bitmap.Dispose();
bitmap = null;
}
Expand Down

0 comments on commit 1ee8d2b

Please sign in to comment.