8000 Restrict first garbage collection · pythonnet/pythonnet@6f0f671 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6f0f671

Browse files
committed
Restrict first garbage collection
Otherwise, collecting all at this earlier point results in corrupt memory for derived types.
1 parent 4e5afdf commit 6f0f671

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/runtime/Finalizer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ internal static void Shutdown()
191191
Instance.started = false;
192192
}
193193

194-
internal nint DisposeAll()
194+
internal nint DisposeAll(bool disposeObj = true, bool disposeDerived = true, bool disposeBuffer = true)
195195
{
196196
if (_objQueue.IsEmpty && _derivedQueue.IsEmpty && _bufferQueue.IsEmpty)
197197
return 0;
@@ -216,7 +216,7 @@ internal nint DisposeAll()
216216

217217
try
218218
{
219-
while (!_objQueue.IsEmpty)
219+
if (disposeObj) while (!_objQueue.IsEmpty)
220220
{
221221
if (!_objQueue.TryDequeue(out var obj))
222222
continue;
@@ -240,7 +240,7 @@ internal nint DisposeAll()
240240
}
241241
}
242242

243-
while (!_derivedQueue.IsEmpty)
243+
if (disposeDerived) while (!_derivedQueue.IsEmpty)
244244
{
245245
if (!_derivedQueue.TryDequeue(out var derived))
246246
continue;
@@ -258,7 +258,7 @@ internal nint DisposeAll()
258258
collected++;
259259
}
260260

261-
while (!_bufferQueue.IsEmpty)
261+
if (disposeBuffer) while (!_bufferQueue.IsEmpty)
262262
{
263263
if (!_bufferQueue.TryDequeue(out var buffer))
264264
continue;

src/runtime/Runtime.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ internal static void Shutdown()
278278
ClearClrModules();
279279
RemoveClrRootModule();
280280

281-
TryCollectingGarbage(MaxCollectRetriesOnShutdown, forceBreakLoops: true);
281+
TryCollectingGarbage(MaxCollectRetriesOnShutdown, forceBreakLoops: true,
282+
obj: true, derived: false, buffer: false);
282283

283284
NullGCHandles(ExtensionType.loadedExtensions);
284285
ClassManager.RemoveClasses();
@@ -329,7 +330,8 @@ internal static void Shutdown()
329330

330331
const int MaxCollectRetriesOnShutdown = 20;
331332
internal static int _collected;
332-
static bool TryCollectingGarbage(int runs, bool forceBreakLoops)
333+
static bool TryCollectingGarbage(int runs, bool forceBreakLoops,
334+
bool obj = true, bool derived = true, bool buffer = true)
333335
{
334336
if (runs <= 0) throw new ArgumentOutOfRangeException(nameof(runs));
335337

@@ -342,7 +344,9 @@ static bool TryCollectingGarbage(int runs, bool forceBreakLoops)
342344
GC.Collect();
343345
GC.WaitForPendingFinalizers();
344346
pyCollected += PyGC_Collect();
345-
pyCollected += Finalizer.Instance.DisposeAll();
347+
pyCollected += Finalizer.Instance.DisposeAll(disposeObj: obj,
348+
disposeDerived: derived,
349+
disposeBuffer: buffer);
346350
}
347351
if (Volatile.Read(ref _collected) == 0 && pyCollected == 0)
348352
{

0 commit comments

Comments
 (0)
0