microsoft / Windows.UI.Composition-Win32-Samples

Windows.UI.Composition Win32 Samples
MIT License
459 stars 186 forks source link

Sometimes CreateCaptureItemForWindow throws fatal exception #43

Closed gileli121 closed 4 years ago

gileli121 commented 4 years ago

After few attempts to call this code:

auto activation_factory = winrt::get_activation_factory<winrt::Windows::Graphics::Capture::GraphicsCaptureItem>();
auto interop_factory = activation_factory.as<IGraphicsCaptureItemInterop>();
winrt::Windows::Graphics::Capture::GraphicsCaptureItem item = { nullptr };
interop_factory->CreateForWindow(hwnd, winrt::guid_of<ABI::Windows::Graphics::Capture::IGraphicsCaptureItem>(), reinterpret_cast<void**>(winrt::put_abi(item)));
return item;

I get exception: image

And my code does not catch it.

It is very random. most of the time it happen after a lot of attempts to create the capture item.

Questions:

  1. What can cause such exception and how to prevent it? Is there a common user mistake that cause this error?
  2. How to handle the error? How to catch the error in case it happned? My code does not catch it. I don't want that the program will crash at the worst case scenario

Thanks.

robmikh commented 4 years ago

Do you have a minimal version of the code that reproduces this error? Otherwise, please collect a process dump and we can take a look.

gileli121 commented 4 years ago

How to collect process dump with visual studio 2019?

robmikh commented 4 years ago

Once you see the unhandled exception message from your screenshot, go to Debug->Save Dump As. Please make sure you select "Minidump with Heap" when saving.

gileli121 commented 4 years ago

OK, I will do it when it will happen again and upload here the dump. Thanks

robmikh commented 4 years ago

It's been a few months since any activity, so I'm going to close this issue. Please reopen it with the crash dump if you see it again.

yjlintw commented 4 years ago

Dmp file (Googld Drive)

I'm having the same problem. The dmp file is too big so I put it on google drive. Hope this will help.

robmikh commented 4 years ago

Hey @yjlintw, do you have the same symptoms as the above? Looking at the dump, this won't be a failfast and should throw an exception that you should be able to catch. Are you able to do so?

For some reason I'm not able to recognize the error code that triggers the exception, not sure if there's an issue with the dump or there's some other issue going on. Are you able to reliably reproduce this error? Can you share more about the scenario? Is it always with a certain window? All windows? Does it ever succeed?

We may need to get this reproducing locally to make progress.

yjlintw commented 4 years ago

I don't have the try/catch statement initially. After adding that, I'm able to catch it.

It happens when I stop the capture and then start the capture again. It could happen to all windows, but it does not always happens. It happens every now and then.

My originally implementation was resizing the captured window -> stop capture -> start capture. I now resize the texture instead, which I think is a better implementation anyway, and it helps me get around this error.

ghost commented 4 years ago

I'm sorry to revive this kinda old thread, but I see similar fatal exceptions when I try to capture child windows or dialogs. I haven't read anything in the offical reference about whether the API supports top-level windows only so I wonder if this is even possible.

@robmikh, could you please confirm/deny my assumption?

robmikh commented 4 years ago

That is to be expected. Thanks for a calling that out as far as the documentation goes, we should update it. We only support the capture of top level windows.

EricBallard commented 3 years ago

Just an FYI - I was encountering this issue when trying to capture a window using DirectX 12, in the application settings - after changing to DirectX 11 (Legacy) I was able to capture the window without issue.

robmikh commented 3 years ago

Correct, the Direct3D11CaptureFramePool does not support 11-on-12. We need to do some work to support Direct3D 12.

tianyouhui commented 1 year ago

interop_factory->CreateForWindow() always crash while begin to capture window zoom->settings, is there something wrong? image

robmikh commented 1 year ago

I'm guessing it's because the window is a tool window. The general rule of thumb is that if it's in alt+tab, you should be able to capture it.