question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Application hangs on GC.Collect()

See original GitHub issue

Describe the bug Some of our components use DirectX to fast render. On porting I found an issue that application hangs when I put such components in window`s content. After some researching I suggest that the issue relates to GC and interrop services. I prepared a synthetic sample to reproduce this issue. In my sample I create a descendant of SurfaceImageSource. After initialize component I query ISurfaceImageSourceNative interface from SurfaceImageSource.

image

image

image

On Window SizeChanged event I call GC.Collect(). When I run app and start resizing, the application hangs. If I run app under visual studio debugger, the application hangs with Visual Studio together.

If I comment GC.Collect (ln 43) or comment Marshal.GetObjectForIUnknown(pOut) line (ln 38), all works fine. There is why I suggested that issue relates to garbage collector and interrop services.

Steps to reproduce the bug

  1. Compile and run attached project
  2. Resize a window. Application will hang.
  3. Comment line 43 (GC.Collect()) and run application again. All will work without errors.
  4. Uncomment line 43 and comment lines 38-40. All will work good again.

App31.zip

Version Info

Windows 10 1903 (OS Build 18362.1082)

NuGet package version:

WinUI 3.0.0-preview2.200713.0

Windows app type:

UWP Win32
No Yes
Device form factor Saw the problem?
Desktop Yes

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
alexserovcommented, Nov 3, 2020

After looking into the @BorzillaR sample, I see that the issue reproduces when the coreclr!RCWWalker::s_pGCManager field is not null. So I can hardly imagine how to work with native (COM) Windows runtime types since this may lead to application hanging.

To reproduce this, you can create a blank app from the Visual Studio template and replace the MainWindow ctor with the following code:

public MainWindow() {
    this.InitializeComponent();
    try {
        Marshal.GetObjectForIUnknown(ThisPtr);
    } catch { }
    GC.Collect();
}                

If you attach a debugger to the application, you’ll see a deadlock with the following call stack:

[0x0]   ntdll!NtWaitForAlertByThreadId + 0xc   
[0x1]   ntdll!RtlAcquireSRWLockExclusive + 0x17b   
[0x2]   Microsoft_UI_Xaml!DirectUI::ReferenceTrackerManager::ReferenceTrackingStarted + 0x3a   
[0x3]   coreclr!TrackerObjectManager::BeginReferenceTracking + 0x39   
[0x4]   coreclr!InteropLib::Com::BeginExternalObjectReferenceTracking + 0xb   
[0x5]   coreclr!Interop::OnGCStarted + 0x84   
[0x6]   coreclr!GCToEEInterface::GcStartWork + 0x31   
[0x7]   coreclr!WKS::gc_heap::garbage_collect + 0x23b   
[0x8]   coreclr!WKS::GCHeap::GarbageCollectGeneration + 0x1b0   
[0x9]   coreclr!WKS::GCHeap::GarbageCollectTry + 0x52   
[0xa]   coreclr!WKS::GCHeap::GarbageCollect + 0xc9   
[0xb]   coreclr!GCInterface::Collect + 0x56   
[0xc]   System_Private_CoreLib!System.GC.Collect()$##60003C0 + 0x23   
1reaction
stevenbrixcommented, Nov 17, 2020

Great, thanks @angelazhangmsft!

I’m going to close out this issue now that preview3 has been released. @BorzillaR, if you see this issue again, please respond and I will re-open. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Something blocks my memory and the program hangs ...
As you can see that GetTotalMemory is waiting for finalizers, so it has to be a finalizer which hangs up the application. GC.Collect()...
Read more >
Help Needed , Garbage collector Never activates and ...
Help Needed , Garbage collector Never activates and application eventually freezes computer[Solved].
Read more >
App hangs, GC.collect() fixet it. Why? - dlang forum
I've been tracking down a hang in our pilot app. Using writeln, it appears to hang at newing a slice. After many hours...
Read more >
GC freezes system for 10+ minutes?
Collect() somewhere central, which would magically prevent the system from getting that messed up where it would take so long to clean up....
Read more >
gc — Garbage Collector interface
This module provides an interface to the optional garbage collector. It provides the ability to disable the collector, tune the collection frequency, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found