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

Commit d34aff0

Browse files
authored
Merge pull request #179 from Unity-Technologies/zgh/build_scripts
fix: app crash because of animating gif
2 parents 33cad3c + bedfd8a commit d34aff0

File tree

8 files changed

+99
-14
lines changed

8 files changed

+99
-14
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: 29 additions & 0 deletions
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 DisposeCPtr()
63+
{
64+
if(_engineLayer != null) {
65+
_engineLayer.DisposeCPtr();
66+
}
67+
}
68+
6269
public bool _needsAddToScene = true;
6370

6471
protected void markNeedsAddToScene() {
@@ -98,6 +105,9 @@ bool debugSubtreeNeedsAddToScene {
98105
protected 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 DisposeCPtr()
258+
{
259+
base.DisposeCPtr();
260+
if(_picture != null){
261+
_picture.DisposeCPtr();
262+
}
263+
}
264+
247265
public readonly Rect canvasBounds;
248266

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

507+
public override void DisposeCPtr() {
508+
base.DisposeCPtr();
509+
Layer child = firstChild;
510+
while (child != null) {
511+
Layer next = child.nextSibling;
512+
child.DisposeCPtr();
513+
child = next;
514+
}
515+
}
516+
489517
internal override void updateSubtreeNeedsAddToScene() {
490518
base.updateSubtreeNeedsAddToScene();
491519
Layer child = firstChild;
@@ -612,6 +640,7 @@ public void removeAllChildren() {
612640
child._nextSibling = null;
613641
D.assert(child.attached == attached);
614642
dropChild(child);
643+
child.DisposeCPtr();
615644
child = next;
616645
}
617646

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void compositeFrame() {
134134
using (var scene = layer.buildScene(builder)) {
135135
Window.instance.render(scene);
136136
}
137-
137+
builder.DisposeCPtr();
138138
D.assert(() => {
139139
if (D.debugRepaintRainbowEnabled || D.debugRepaintTextRainbowEnabled) {
140140
D.debugCurrentRepaintColor =

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ internal PhysicalShapeEngineLayer(IntPtr ptr) : base(ptr) {
140140
}
141141
}
142142

143-
public class SceneBuilder : NativeWrapper {
143+
public class SceneBuilder : NativeWrapperCPtrDisposable {
144144
public SceneBuilder() : base(SceneBuilder_constructor()) {
145145
}
146146

147-
public override void DisposePtr(IntPtr ptr) {
147+
public override void DisposeCPtrImpl(IntPtr ptr) {
148148
SceneBuilder_dispose(ptr);
149149
}
150150

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,36 @@ public void Dispose() {
6464
_dispose();
6565
}
6666
}
67+
68+
/// <summary>
69+
/// This class is used to release c++ resources in advance without waiting for c# garbage collection.
70+
/// Please make sure that the resource will not be used again after it is released.
71+
/// Releasing resources prematurely may cause null pointer exceptions in references elsewhere.
72+
///
73+
/// Usage:
74+
/// Use DisposeCPtr() to manually release c++ resources, and then add an interface to release unmanaged memory
75+
/// in DisposeCPtrImpl(IntPtr ptr).
76+
/// </summary>
77+
public abstract class NativeWrapperCPtrDisposable : NativeWrapperDisposable {
78+
public bool isDisposed = false;
79+
80+
protected NativeWrapperCPtrDisposable() {
81+
}
82+
83+
protected NativeWrapperCPtrDisposable(IntPtr ptr) : base(ptr) {
84+
}
85+
86+
public void DisposeCPtr() {
87+
DisposePtr(_ptr);
88+
}
89+
public override void DisposePtr(IntPtr ptr) {
90+
if(isDisposed){
91+
return;
92+
}
93+
isDisposed = true;
94+
DisposeCPtrImpl(ptr);
95+
}
96+
97+
public abstract void DisposeCPtrImpl(IntPtr ptr);
98+
}
6799
}

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -763,12 +763,13 @@ internal _ImageInfo(int width, int height, int format, int? rowBytes = null) {
763763
public int rowBytes;
764764
}
765765

766-
public class Image : NativeWrapperDisposable, IEquatable<Image> {
766+
public class Image : NativeWrapperCPtrDisposable, IEquatable<Image> {
767+
767768
internal Image(IntPtr ptr) : base(ptr) {
768769
}
769770

770-
public override void DisposePtr(IntPtr ptr) {
771-
Image_dispose(ptr);
771+
public override void DisposeCPtrImpl(IntPtr ptr) {
772+
Image_dispose(ptr);
772773
}
773774

774775
public int width => Image_width(_ptr);
@@ -880,18 +881,31 @@ public override int GetHashCode() {
880881

881882
public delegate void ImageDecoderCallback(Image result);
882883

883-
public class FrameInfo : NativeWrapper {
884+
public class FrameInfo : NativeWrapperCPtrDisposable {
885+
884886
internal FrameInfo(IntPtr ptr) : base(ptr) {
885887
}
886888

887-
public override void DisposePtr(IntPtr ptr) {
889+
public override void DisposeCPtrImpl(IntPtr ptr) {
888890
FrameInfo_dispose(ptr);
889891
}
890892

893+
891894
public TimeSpan duration => TimeSpan.FromMilliseconds(_durationMillis);
892895
int _durationMillis => FrameInfo_durationMillis(_ptr);
893896

894-
public Image image => new Image(FrameInfo_image(_ptr));
897+
898+
private Image _image;
899+
900+
public Image image {
901+
get {
902+
if(_image == null){
903+
_image = new Image(FrameInfo_image(_ptr));
904+
}
905+
906+
return _image;
907+
}
908+
}
895909

896910
[DllImport(NativeBindings.dllName)]
897911
static extern void FrameInfo_dispose(IntPtr ptr);
@@ -1055,11 +1069,11 @@ public enum PathOperation {
10551069
reverseDifference,
10561070
}
10571071

1058-
public abstract class EngineLayer : NativeWrapper {
1072+
public abstract class EngineLayer : NativeWrapperCPtrDisposable {
10591073
protected EngineLayer(IntPtr ptr) : base(ptr) {
10601074
}
10611075

1062-
public override void DisposePtr(IntPtr ptr) {
1076+
public override void DisposeCPtrImpl(IntPtr ptr) {
10631077
EngineLayer_dispose(ptr);
10641078
}
10651079

@@ -2844,11 +2858,12 @@ static extern void Canvas_drawShadow(IntPtr ptr, IntPtr path, uint color, float
28442858
bool transparentOccluder);
28452859
}
28462860

2847-
public class Picture : NativeWrapperDisposable {
2861+
public class Picture : NativeWrapperCPtrDisposable {
2862+
28482863
internal Picture(IntPtr ptr) : base(ptr) {
28492864
}
28502865

2851-
public override void DisposePtr(IntPtr ptr) {
2866+
public override void DisposeCPtrImpl(IntPtr ptr) {
28522867
Picture_dispose(ptr);
28532868
}
28542869

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 DisposeCPtr() {
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)