Skip to content

Commit

Permalink
Return an empty surface in case the allocation has failed (#1784)
Browse files Browse the repository at this point in the history
Co-authored-by: Richirisu <[email protected]>
Co-authored-by: Matthew Leibowitz <[email protected]>
  • Loading branch information
3 people authored Sep 6, 2021
1 parent 8cac9b8 commit 8b060e6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,19 @@ public override void Draw(CGRect dirtyRect)
// draw the surface to the context
drawable.DrawSurface(ctx, Bounds, info, surface);
}

public override void WillMoveToWindow(UIWindow window)
{
if (drawable != null)
{
// release the memory if we are leaving the window
if (window == null)
drawable?.Dispose();
else
SetNeedsDisplay();
}

base.WillMoveToWindow(window);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public SKSurface CreateSurface(CGRect contentsBounds, nfloat scale, out SKImageI
if (bitmapData == null)
{
bitmapData = NSMutableData.FromLength(info.BytesSize);

// in case allocation has failed
if (bitmapData == null)
{
Dispose();
info = Info;
return null;
}

dataProvider = new CGDataProvider(bitmapData.MutableBytes, info.BytesSize, Dummy);

void Dummy(IntPtr data)
Expand Down
14 changes: 14 additions & 0 deletions source/SkiaSharp.Views/SkiaSharp.Views.AppleiOS/SKCanvasView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ public override void Draw(CGRect rect)
}
}

public override void WillMoveToWindow(UIWindow window)
{
if (drawable != null)
{
// release the memory if we are leaving the window
if (window == null)
drawable?.Dispose();
else
SetNeedsDisplay();
}

base.WillMoveToWindow(window);
}

public event EventHandler<SKPaintSurfaceEventArgs> PaintSurface;

protected virtual void OnPaintSurface(SKPaintSurfaceEventArgs e)
Expand Down

0 comments on commit 8b060e6

Please sign in to comment.