Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Restrict first garbage collection
Otherwise, collecting all at this earlier point results in corrupt memory for derived types.
  • Loading branch information
Frawak committed May 13, 2024
commit 6f0f6713e8f55a24ea7803584c5490eca0518739
8 changes: 4 additions & 4 deletions src/runtime/Finalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ internal static void Shutdown()
Instance.started = false;
}

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

try
{
while (!_objQueue.IsEmpty)
if (disposeObj) while (!_objQueue.IsEmpty)
{
if (!_objQueue.TryDequeue(out var obj))
continue;
Expand All @@ -240,7 +240,7 @@ internal nint DisposeAll()
}
}

while (!_derivedQueue.IsEmpty)
if (disposeDerived) while (!_derivedQueue.IsEmpty)
{
if (!_derivedQueue.TryDequeue(out var derived))
continue;
Expand All @@ -258,7 +258,7 @@ internal nint DisposeAll()
collected++;
}

while (!_bufferQueue.IsEmpty)
if (disposeBuffer) while (!_bufferQueue.IsEmpty)
{
if (!_bufferQueue.TryDequeue(out var buffer))
continue;
Expand Down
10 changes: 7 additions & 3 deletions src/runtime/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ internal static void Shutdown()
ClearClrModules();
RemoveClrRootModule();

TryCollectingGarbage(MaxCollectRetriesOnShutdown, forceBreakLoops: true);
TryCollectingGarbage(MaxCollectRetriesOnShutdown, forceBreakLoops: true,
obj: true, derived: false, buffer: false);

NullGCHandles(ExtensionType.loadedExtensions);
ClassManager.RemoveClasses();
Expand Down Expand Up @@ -329,7 +330,8 @@ internal static void Shutdown()

const int MaxCollectRetriesOnShutdown = 20;
internal static int _collected;
static bool TryCollectingGarbage(int runs, bool forceBreakLoops)
static bool TryCollectingGarbage(int runs, bool forceBreakLoops,
bool obj = true, bool derived = true, bool buffer = true)
{
if (runs <= 0) throw new ArgumentOutOfRangeException(nameof(runs));

Expand All @@ -342,7 +344,9 @@ static bool TryCollectingGarbage(int runs, bool forceBreakLoops)
GC.Collect();
GC.WaitForPendingFinalizers();
pyCollected += PyGC_Collect();
pyCollected += Finalizer.Instance.DisposeAll();
pyCollected += Finalizer.Instance.DisposeAll(disposeObj: obj,
disposeDerived: derived,
disposeBuffer: buffer);
}
if (Volatile.Read(ref _collected) == 0 && pyCollected == 0)
{
Expand Down