Skip to content
This repository was archived by the owner on Sep 7, 2022. It is now read-only.

Commit d904d23

Browse files
author
guanghuispark
committed
fix: app crash because of animating gif
1 parent 60822c6 commit d904d23

File tree

7 files changed

+131
-4
lines changed

7 files changed

+131
-4
lines changed

com.unity.uiwidgets/Runtime/painting/image_stream.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ void _handleAppFrame(TimeSpan timestamp) {
444444
_emitFrame(new ImageInfo(image: _nextFrame.image, scale: _scale));
445445
_shownTimestamp = timestamp;
446446
_frameDuration = _nextFrame.duration;
447+
_nextFrame.DisposeCPtr();
447448
_nextFrame = null;
448449
int completedCycles = _codec.frameCount == 0 ? 0 : _framesEmitted / _codec.frameCount;
449450

com.unity.uiwidgets/Runtime/rendering/image.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public Image image {
6464
if (value == _image) {
6565
return;
6666
}
67-
67+
if(_image != null){
68+
_image.DisposeCPtr();
69+
}
6870
_image = value;
6971
markNeedsPaint();
7072
if (_width == null || _height == null) {

com.unity.uiwidgets/Runtime/rendering/layer.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ public abstract class Layer : AbstractNodeMixinDiagnosticableTree {
5959
get { return (ContainerLayer) base.parent; }
6060
}
6161

62+
public virtual void DisposeCPter()
63+
{
64+
if(_engineLayer != null) {
65+
_engineLayer.DisposeCPtr();
66+
}
67+
}
68+
6269
public bool _needsAddToScene = true;
6370

6471
protected void markNeedsAddToScene() {
@@ -95,9 +102,12 @@ bool debugSubtreeNeedsAddToScene {
95102
}
96103
}
97104

98-
protected EngineLayer engineLayer {
105+
public EngineLayer engineLayer {
99106
get { return _engineLayer; }
100107
set {
108+
if(_engineLayer != null){
109+
_engineLayer.DisposeCPtr();
110+
}
101111
_engineLayer = value;
102112
if (!alwaysNeedsAddToScene) {
103113
if (parent != null && !parent.alwaysNeedsAddToScene) {
@@ -244,6 +254,14 @@ public PictureLayer(Rect canvasBounds) {
244254
this.canvasBounds = canvasBounds;
245255
}
246256

257+
public override void DisposeCPter()
258+
{
259+
base.DisposeCPter();
260+
if(_picture != null){
261+
_picture.DisposeCPtr();
262+
}
263+
}
264+
247265
public readonly Rect canvasBounds;
248266

249267
Picture _picture;
@@ -486,6 +504,20 @@ protected List<PictureLayer> _debugCheckElevations() {
486504
return addedLayers;
487505
}
488506

507+
public override void DisposeCPter() {
508+
base.DisposeCPter();
509+
DisposeAllChildren();
510+
}
511+
512+
private void DisposeAllChildren(){
513+
Layer child = firstChild;
514+
while (child != null) {
515+
Layer next = child.nextSibling;
516+
child.DisposeCPter();
517+
child = next;
518+
}
519+
}
520+
489521
internal override void updateSubtreeNeedsAddToScene() {
490522
base.updateSubtreeNeedsAddToScene();
491523
Layer child = firstChild;
@@ -601,6 +633,7 @@ internal void _removeChild(Layer child) {
601633
child._nextSibling = null;
602634
child._previousSibling = null;
603635
dropChild(child);
636+
child.DisposeCPter();
604637
D.assert(!child.attached);
605638
}
606639

@@ -612,6 +645,7 @@ public void removeAllChildren() {
612645
child._nextSibling = null;
613646
D.assert(child.attached == attached);
614647
dropChild(child);
648+
child.DisposeCPter();
615649
child = next;
616650
}
617651

com.unity.uiwidgets/Runtime/rendering/view.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public void compositeFrame() {
133133
var builder = new SceneBuilder();
134134
using (var scene = layer.buildScene(builder)) {
135135
Window.instance.render(scene);
136+
builder.DisposeCPtr();
136137
}
137138

138139
D.assert(() => {

com.unity.uiwidgets/Runtime/ui/compositing.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,26 @@ internal PhysicalShapeEngineLayer(IntPtr ptr) : base(ptr) {
141141
}
142142

143143
public class SceneBuilder : NativeWrapper {
144+
private bool isDisposed = false;
144145
public SceneBuilder() : base(SceneBuilder_constructor()) {
145146
}
146147

147148
public override void DisposePtr(IntPtr ptr) {
149+
if(isDisposed){
150+
return;
151+
}
152+
isDisposed = true;
148153
SceneBuilder_dispose(ptr);
149154
}
150155

156+
public void DisposeCPtr() {
157+
if(isDisposed){
158+
return;
159+
}
160+
isDisposed = true;
161+
SceneBuilder_dispose(_ptr);
162+
}
163+
151164
readonly Dictionary<EngineLayer, string> _usedLayers = new Dictionary<EngineLayer, string>();
152165

153166
bool _debugCheckUsedOnce(EngineLayer layer, string usage) {

com.unity.uiwidgets/Runtime/ui/painting.cs

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -764,11 +764,26 @@ internal _ImageInfo(int width, int height, int format, int? rowBytes = null) {
764764
}
765765

766766
public class Image : NativeWrapperDisposable, IEquatable<Image> {
767+
768+
private bool isDisposed = false;
769+
767770
internal Image(IntPtr ptr) : base(ptr) {
768771
}
769772

770773
public override void DisposePtr(IntPtr ptr) {
771-
Image_dispose(ptr);
774+
if(isDisposed){
775+
return;
776+
}
777+
isDisposed = true;
778+
Image_dispose(ptr);
779+
}
780+
781+
public void DisposeCPtr() {
782+
if(isDisposed){
783+
return;
784+
}
785+
isDisposed = true;
786+
Image_dispose(_ptr);
772787
}
773788

774789
public int width => Image_width(_ptr);
@@ -881,17 +896,44 @@ public override int GetHashCode() {
881896
public delegate void ImageDecoderCallback(Image result);
882897

883898
public class FrameInfo : NativeWrapper {
899+
bool isDisposed = false;
900+
884901
internal FrameInfo(IntPtr ptr) : base(ptr) {
885902
}
886903

887904
public override void DisposePtr(IntPtr ptr) {
905+
if (isDisposed) {
906+
return;
907+
}
908+
909+
isDisposed = true;
888910
FrameInfo_dispose(ptr);
889911
}
890912

913+
internal void DisposeCPtr() {
914+
if (isDisposed) {
915+
return;
916+
}
917+
918+
isDisposed = true;
919+
FrameInfo_dispose(_ptr);
920+
}
921+
891922
public TimeSpan duration => TimeSpan.FromMilliseconds(_durationMillis);
892923
int _durationMillis => FrameInfo_durationMillis(_ptr);
893924

894-
public Image image => new Image(FrameInfo_image(_ptr));
925+
926+
private Image _image;
927+
928+
public Image image {
929+
get {
930+
if(_image == null){
931+
_image = new Image(FrameInfo_image(_ptr));
932+
}
933+
934+
return _image;
935+
}
936+
}
895937

896938
[DllImport(NativeBindings.dllName)]
897939
static extern void FrameInfo_dispose(IntPtr ptr);
@@ -1056,13 +1098,27 @@ public enum PathOperation {
10561098
}
10571099

10581100
public abstract class EngineLayer : NativeWrapper {
1101+
private bool isDisposed = false;
10591102
protected EngineLayer(IntPtr ptr) : base(ptr) {
10601103
}
10611104

10621105
public override void DisposePtr(IntPtr ptr) {
1106+
if(isDisposed){
1107+
return;
1108+
}
1109+
isDisposed = true;
10631110
EngineLayer_dispose(ptr);
10641111
}
10651112

1113+
internal void DisposeCPtr()
1114+
{
1115+
if(isDisposed){
1116+
return;
1117+
}
1118+
isDisposed = true;
1119+
EngineLayer_dispose(_ptr);
1120+
}
1121+
10661122
[DllImport(NativeBindings.dllName)]
10671123
static extern void EngineLayer_dispose(IntPtr ptr);
10681124
}
@@ -2845,13 +2901,27 @@ static extern void Canvas_drawShadow(IntPtr ptr, IntPtr path, uint color, float
28452901
}
28462902

28472903
public class Picture : NativeWrapperDisposable {
2904+
2905+
private bool isDisposed = false;
28482906
internal Picture(IntPtr ptr) : base(ptr) {
28492907
}
28502908

28512909
public override void DisposePtr(IntPtr ptr) {
2910+
if(isDisposed){
2911+
return;
2912+
}
2913+
isDisposed = true;
28522914
Picture_dispose(ptr);
28532915
}
28542916

2917+
public void DisposeCPtr(){
2918+
if(isDisposed){
2919+
return;
2920+
}
2921+
isDisposed = true;
2922+
Picture_dispose(_ptr);
2923+
}
2924+
28552925
public Future<Image> toImage(int width, int height) {
28562926
if (width <= 0 || height <= 0) {
28572927
throw new ArgumentException("Invalid image dimensions.");

com.unity.uiwidgets/Runtime/widgets/widget_inspector.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,6 +2444,12 @@ Rect targetRect
24442444
}
24452445

24462446

2447+
public override void DisposeCPter() {
2448+
if(_picture != null){
2449+
_picture.DisposeCPtr();
2450+
}
2451+
}
2452+
24472453
public override bool findAnnotations<S>(
24482454
AnnotationResult<S> result,
24492455
Offset localPosition,

0 commit comments

Comments
 (0)