From b2a4ddf853090425e463552c5fd8796e5db622c2 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 22 Jul 2018 06:27:57 +0900 Subject: [PATCH] shareable: Refactoring: Add moveTo --- internal/shareable/shareable.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/internal/shareable/shareable.go b/internal/shareable/shareable.go index 0a8740904..2a2506949 100644 --- a/internal/shareable/shareable.go +++ b/internal/shareable/shareable.go @@ -95,6 +95,15 @@ type Image struct { countForShare int } +func (i *Image) moveTo(dst *Image) { + dst.dispose(false) + *dst = *i + + // i is no longer available but Dispose must not be called + // since i and dst have the same values like node. + runtime.SetFinalizer(i, nil) +} + func (i *Image) isShared() bool { return i.node != nil } @@ -154,11 +163,8 @@ func (i *Image) forceShared() { } } newI.replacePixels(pixels) - i.dispose(false) - *i = *newI - // TODO: This is a very tricky hack not to call Dispose twice for the same backend. - // Avoid such hack. - runtime.SetFinalizer(newI, nil) + newI.moveTo(i) + i.countForShare = 0 } func (i *Image) region() (x, y, width, height int) {