Skip to content

Commit 3b322f7

Browse files
committed
Merge branch 'jojo59516-master'
2 parents 64c91ec + 68b4494 commit 3b322f7

File tree

12 files changed

+88
-48
lines changed

12 files changed

+88
-48
lines changed

Editor/Scripts/CompareSnapshotsView/CompareSnapshotsControl.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ void BuildMemoryTree(PackedMemorySnapshot[] snapshots, ref int uniqueId, TreeVie
132132

133133
for (int k = 0, kend = snapshots.Length; k < kend; ++k)
134134
{
135+
if (window.isClosing) // the window is closing
136+
break;
137+
135138
var snapshot = snapshots[k];
136139

137140
foreach (var section in snapshot.managedHeapSections)
@@ -156,6 +159,9 @@ void BuildGCHandleTree(PackedMemorySnapshot[] snapshots, ref int uniqueId, TreeV
156159

157160
for (int k = 0, kend = snapshots.Length; k < kend; ++k)
158161
{
162+
if (window.isClosing) // the window is closing
163+
break;
164+
159165
var snapshot = snapshots[k];
160166

161167
parent.size[k] += snapshot.gcHandles.Length * snapshot.virtualMachineInformation.pointerSize;
@@ -179,6 +185,9 @@ void BuildNativeTree(PackedMemorySnapshot[] snapshots, ref int uniqueId, TreeVie
179185

180186
for (int k = 0, kend = snapshots.Length; k < kend; ++k)
181187
{
188+
if (window.isClosing) // the window is closing
189+
break;
190+
182191
var snapshot = snapshots[k];
183192

184193
for (int n = 0, nend = snapshot.nativeTypes.Length; n < nend; ++n)
@@ -222,6 +231,9 @@ void Sum(Item parent)
222231

223232
for (int n = 0, nend = parent.children.Count; n < nend; ++n)
224233
{
234+
if (window.isClosing) // the window is closing
235+
break;
236+
225237
var item = parent.children[n] as Item;
226238
if (item == null)
227239
continue;
@@ -250,6 +262,9 @@ void BuildManagedTree(PackedMemorySnapshot[] snapshots, ref int uniqueId, TreeVi
250262

251263
for (int k = 0, kend = snapshots.Length; k < kend; ++k)
252264
{
265+
if (window.isClosing) // the window is closing
266+
break;
267+
253268
var snapshot = snapshots[k];
254269

255270
for (int n = 0, nend = snapshot.managedTypes.Length; n < nend; ++n)

Editor/Scripts/ConnectionsView/ConnectionsControl.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ public TreeViewItem BuildTree(PackedMemorySnapshot snapshot, PackedConnection[]
179179

180180
for (int n = 0, nend = m_Connections.Length; n < nend; ++n)
181181
{
182+
if (window.isClosing) // the window is closing
183+
break;
184+
182185
var connection = m_Connections[n];
183186

184187
if (m_AddTo)

Editor/Scripts/GCHandlesView/GCHandlesControl.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ public TreeViewItem BuildTree(PackedMemorySnapshot snapshot)
105105

106106
for (int n = 0, nend = m_Snapshot.gcHandles.Length; n < nend; ++n)
107107
{
108+
if (window.isClosing) // the window is closing
109+
break;
110+
108111
var gcHandle = m_Snapshot.gcHandles[n];
109112
var managedTypeIndex = -1;
110113
if (gcHandle.managedObjectsArrayIndex >= 0)

Editor/Scripts/HeapExplorerWindow.cs

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
// Heap Explorer for Unity. Copyright (c) 2019-2020 Peter Schraut (www.console-dev.de). See LICENSE.md
33
// https://github.com/pschraut/UnityHeapExplorer/
44
//
5+
#pragma warning disable 0414
56
using System.Collections;
67
using System.Collections.Generic;
78
using UnityEngine;
89
using UnityEditor;
910
using System;
11+
using System.Threading;
1012

1113
namespace HeapExplorer
1214
{
@@ -36,7 +38,6 @@ public PackedMemorySnapshot snapshot
3638
}
3739
}
3840

39-
#pragma warning disable 0414
4041
[NonSerialized] TestVariables m_TestVariables = new TestVariables(); // Allows me to easily check/test various types when capturing a snapshot in the editor.
4142
[NonSerialized] bool m_IsCapturing; // Whether the tool is currently capturing a memory snapshot
4243
[NonSerialized] GotoHistory m_GotoHistory = new GotoHistory();
@@ -57,9 +58,11 @@ public PackedMemorySnapshot snapshot
5758
[NonSerialized] int m_BusyDraws;
5859
[NonSerialized] List<Exception> m_Exceptions = new List<Exception>(); // If exception occur in threaded jobs, these are collected and logged on the main thread
5960
[NonSerialized] bool m_CloseDueToError; // If set to true, will close the editor during the next Update
61+
[NonSerialized] double m_LastRepaintTimestamp; // The EditorApplication.timeSinceStartup when a Repaint() was issued
6062

6163
static List<System.Type> s_ViewTypes = new List<Type>();
62-
#pragma warning restore 0414
64+
65+
public bool isClosing { get; private set; }
6366

6467
bool useThreads
6568
{
@@ -146,36 +149,51 @@ static void Create()
146149

147150
void OnEnable()
148151
{
152+
isClosing = false;
149153
titleContent = new GUIContent(HeGlobals.k_Title);
150154
minSize = new Vector2(800, 600);
151155
snapshotPath = "";
152156
showInternalMemorySections = showInternalMemorySections;
157+
m_LastRepaintTimestamp = 0;
158+
m_Repaint = true;
153159

154160
m_ThreadJobs = new List<AbstractThreadJob>();
155161
m_Thread = new System.Threading.Thread(ThreadLoop);
156162
m_Thread.Start();
157163

158164
CreateViews();
159165

160-
EditorApplication.update += OnApplicationUpdate;
166+
EditorApplication.update += OnEditorApplicationUpdate;
161167
}
162168

163169
void OnDisable()
164170
{
165-
TryAbortThread();
166-
m_ThreadJobs = new List<AbstractThreadJob>();
171+
lock (m_ThreadJobs)
172+
{
173+
// ask thread to exit
174+
isClosing = true;
175+
if (m_Heap != null && m_Heap.isProcessing)
176+
m_Heap.abortActiveStepRequested = true;
177+
Monitor.Pulse(m_ThreadJobs);
178+
}
179+
m_Thread.Join(); // wait for thread exit
180+
m_Thread = null;
167181

168-
EditorApplication.update -= OnApplicationUpdate;
182+
EditorApplication.update -= OnEditorApplicationUpdate;
169183

170184
DestroyViews();
171185
}
172186

173-
void OnApplicationUpdate()
187+
void OnEditorApplicationUpdate()
174188
{
175-
if (m_Repaint)
189+
if (m_Repaint || (m_Heap != null && m_Heap.isBusy))
176190
{
177-
m_Repaint = false;
178-
Repaint();
191+
if (m_LastRepaintTimestamp+0.05f < EditorApplication.timeSinceStartup)
192+
{
193+
m_Repaint = false;
194+
m_LastRepaintTimestamp = EditorApplication.timeSinceStartup;
195+
Repaint();
196+
}
179197
}
180198

181199
if (m_CloseDueToError)
@@ -319,7 +337,6 @@ void OnGUI()
319337
{
320338
m_BusyDraws--;
321339
m_BusyString = "";
322-
Repaint();
323340

324341
DrawToolbar();
325342

@@ -637,28 +654,6 @@ void LoadFromFile()
637654
LoadFromFile(path);
638655
}
639656

640-
void TryAbortThread()
641-
{
642-
if (m_Thread == null)
643-
return;
644-
645-
var guard = 0;
646-
var flags = System.Threading.ThreadState.Stopped | System.Threading.ThreadState.Aborted;
647-
648-
m_Thread.Abort();
649-
while ((m_Thread.ThreadState & flags) == 0)
650-
{
651-
System.Threading.Thread.Sleep(1);
652-
if (++guard > 1000)
653-
{
654-
Debug.LogWarning("Waiting for thread abort");
655-
break;
656-
}
657-
}
658-
659-
m_Thread = null;
660-
}
661-
662657
public void LoadFromFile(string path)
663658
{
664659
SaveView();
@@ -685,26 +680,18 @@ public void LoadFromFile(string path)
685680

686681
void ThreadLoop()
687682
{
688-
var keepRunning = true;
689-
while (keepRunning)
683+
while (!isClosing)
690684
{
691-
System.Threading.Thread.Sleep(10);
692-
switch (m_Thread.ThreadState)
693-
{
694-
case System.Threading.ThreadState.Aborted:
695-
case System.Threading.ThreadState.AbortRequested:
696-
case System.Threading.ThreadState.Stopped:
697-
case System.Threading.ThreadState.StopRequested:
698-
keepRunning = false;
699-
break;
700-
}
701-
702685
AbstractThreadJob job = null;
703686

704687
lock (m_ThreadJobs)
705688
{
706-
if (m_ThreadJobs.Count == 0)
707-
continue;
689+
while (m_ThreadJobs.Count == 0)
690+
{
691+
Monitor.Wait(m_ThreadJobs); // block myself, waiting for jobs
692+
if (isClosing)
693+
return; // exit
694+
}
708695

709696
job = m_ThreadJobs[0];
710697
m_ThreadJobs.RemoveAt(0);
@@ -936,6 +923,7 @@ public void ScheduleJob(AbstractThreadJob job)
936923
}
937924

938925
m_ThreadJobs.Add(job);
926+
Monitor.Pulse(m_ThreadJobs); // notify thread that jobs here
939927
}
940928

941929
m_Repaint = true;

Editor/Scripts/ManagedHeapSectionsView/ManagedHeapSectionsControl.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ public TreeViewItem BuildTree(PackedMemorySnapshot snapshot, PackedMemorySection
9393

9494
for (int n = 0, nend = sections.Length; n < nend; ++n)
9595
{
96+
if (window.isClosing) // the window is closing
97+
break;
98+
9699
var section = sections[n];
97100

98101
var item = new HeapSectionItem()

Editor/Scripts/ManagedObjectDuplicatesView/ManagedObjectDuplicatesControl.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ protected override void OnBuildTree(TreeViewItem root)
3333

3434
for (int n = 0, nend = m_Snapshot.managedObjects.Length; n < nend; ++n)
3535
{
36+
if (window.isClosing) // the window is closing
37+
break;
38+
3639
progress.value = (n + 1.0f) / nend;
3740

3841
var obj = m_Snapshot.managedObjects[n];

Editor/Scripts/ManagedObjectsView/ManagedObjectsControl.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ protected virtual void OnBuildTree(TreeViewItem root)
168168

169169
for (int n = 0, nend = m_Snapshot.managedObjects.Length; n < nend; ++n)
170170
{
171+
if (window.isClosing) // the window is closing
172+
break;
173+
171174
var mo = m_Snapshot.managedObjects[n];
172175
if (!OnCanAddObject(mo))
173176
continue;

Editor/Scripts/ManagedObjectsView/ManagedObjectsView.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ protected override void OnBeforeBuildTree()
6363
// Build a table that contains indices of all objects that are the "Target" of a delegate
6464
for (int n = 0, nend = m_Snapshot.managedObjects.Length; n < nend; ++n)
6565
{
66+
if (window.isClosing) // the window is closing
67+
break;
68+
6669
var obj = m_Snapshot.managedObjects[n];
6770
if (obj.address == 0)
6871
continue;

Editor/Scripts/ManagedTypesView/ManagedTypesControl.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public TreeViewItem BuildTree(PackedMemorySnapshot snapshot)
7171

7272
for (int n = 0, nend = m_Snapshot.managedTypes.Length; n < nend; ++n)
7373
{
74+
if (window.isClosing) // the window is closing
75+
break;
76+
7477
var type = m_Snapshot.managedTypes[n];
7578

7679
var item = new ManagedTypeItem

Editor/Scripts/NativeObjectsView/NativeObjectsControl.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using UnityEditor.IMGUI.Controls;
1010
using UnityEditor;
1111
using System.Reflection;
12+
using System.Threading;
1213

1314
namespace HeapExplorer
1415
{
@@ -169,6 +170,9 @@ public TreeViewItem BuildTree(PackedMemorySnapshot snapshot, BuildArgs buildArgs
169170

170171
for (int n = 0, nend = m_Snapshot.nativeObjects.Length; n < nend; ++n)
171172
{
173+
if (window.isClosing) // the window is closing
174+
break;
175+
172176
var no = m_Snapshot.nativeObjects[n];
173177
if (!buildArgs.CanAdd(no))
174178
continue;
@@ -277,6 +281,9 @@ public TreeViewItem BuildDuplicatesTree(PackedMemorySnapshot snapshot, BuildArgs
277281

278282
for (int n = 0, nend = m_Snapshot.nativeObjects.Length; n < nend; ++n)
279283
{
284+
if (window.isClosing) // the window is closing
285+
break;
286+
280287
var no = m_Snapshot.nativeObjects[n];
281288
if (!no.isPersistent)
282289
continue;
@@ -312,6 +319,9 @@ public TreeViewItem BuildDuplicatesTree(PackedMemorySnapshot snapshot, BuildArgs
312319
foreach(var dupePair in dupesLookup)
313320
for (int n = 0, nend = dupePair.Value.Count; n < nend; ++n)
314321
{
322+
if (window.isClosing) // the window is closing
323+
break;
324+
315325
var list = dupePair.Value;
316326
if (list.Count <= 1)
317327
continue;

0 commit comments

Comments
 (0)