From 09ea15e0ff8f8d7ad9ddd210340407d11951bd9c Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 20 Dec 2019 01:13:23 +0900 Subject: [PATCH] shareable: Bug fix: Unexpected allocation when extending an image Fixes #1028 --- internal/shareable/image.go | 6 +++--- internal/shareable/image_test.go | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/internal/shareable/image.go b/internal/shareable/image.go index c9cffd5dd..07690d3ed 100644 --- a/internal/shareable/image.go +++ b/internal/shareable/image.go @@ -97,14 +97,15 @@ func (b *backend) TryAlloc(width, height int) (*packing.Node, bool) { } nExtended := 1 + var n *packing.Node for { if !b.page.Extend(nExtended) { // The page can't be extended any more. Return as failure. return nil, false } nExtended++ - if n := b.page.Alloc(width, height); n != nil { - // The page is just for emulation, so we don't have to free it. + n = b.page.Alloc(width, height) + if n != nil { b.page.CommitExtension() break } @@ -114,7 +115,6 @@ func (b *backend) TryAlloc(width, height int) (*packing.Node, bool) { s := b.page.Size() b.restorable = b.restorable.Extend(s, s) - n := b.page.Alloc(width, height) if n == nil { panic("shareable: Alloc result must not be nil at TryAlloc") } diff --git a/internal/shareable/image_test.go b/internal/shareable/image_test.go index 0231aa333..dccb8d8ab 100644 --- a/internal/shareable/image_test.go +++ b/internal/shareable/image_test.go @@ -393,4 +393,17 @@ func TestDisposeImmediately(t *testing.T) { img1.MarkDisposed() } +// Issue #1028 +func TestExtendWithBigImage(t *testing.T) { + img0 := NewImage(1, 1, false) + defer img0.MarkDisposed() + + img0.ReplacePixels(make([]byte, 4*1*1)) + + img1 := NewImage(1025, 1025, false) + defer img1.MarkDisposed() + + img1.ReplacePixels(make([]byte, 4*1025*1025)) +} + // TODO: Add tests to extend shareable image out of the main loop