marv51
marv51

Reputation: 376

WPF app not exiting because of UWP PdfDocument

I'm trying to use the UWP Windows.Data.Pdf API from a WPF-Desktop app. It can render a PDF page to a PNG image just fine.

The issue I have is that after executing the PDF rendering, closing the WPF-Window does no longer exit the application. The process keeps running. I tried pausing the application in the debugger in this state, but no application code appears to be running. Some thread or resource must still be open, I guess.

My question here is does anybody have any idea why this happens? And furthermore, how can I debug this kind of issue?

The issue is reproducible with the empty WPF template in VS17, I added to the .csproj:

<TargetPlatformVersion>10.0.16299.0</TargetPlatformVersion>
...
<Reference Include="System.Runtime.WindowsRuntime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>$(MSBuildProgramFiles32)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll</HintPath>
</Reference>
<Reference Include="Windows">
  <HintPath>$(MSBuildProgramFiles32)\Windows Kits\10\UnionMetadata\10.0.16299.0\Windows.winmd</HintPath>
</Reference>

From a simple button I'm executing this code:

using System;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Data.Pdf;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;
...
var filename = @"C:\Users\...\a.pdf"
var f = await StorageFile.GetFileFromPathAsync(filename);
byte[] content;
using (var stream = await f.OpenReadAsync())
{
    var d = await PdfDocument.LoadFromStreamAsync(stream);
    using (var page0 = d.GetPage(0))
    {
        await page0.PreparePageAsync();
        using (var randomAccessStream = new InMemoryRandomAccessStream())
        {
            await page0.RenderToStreamAsync(randomAccessStream, new PdfPageRenderOptions { BitmapEncoderId = BitmapEncoder.PngEncoderId });
            content = new byte[randomAccessStream.Size];
            await randomAccessStream.ReadAsync(content.AsBuffer(), (uint)randomAccessStream.Size, InputStreamOptions.None);
        }
    }
}
File.WriteAllBytes(@"C:\Users\...\b.png", content);

This is almost the entire code, but in case I missed something I also pushed it to https://github.com/marv51/PDFRendererNotExiting

Thank you very much in advance for any help on this.

Update:

I just observed, that the process does eventually exit after about 2 minutes! Probably related, I found in the event viewer:

Faulting application name: WpfApp3.exe, version: 1.0.0.0, time stamp: 0x5b291e3a
Faulting module name: ntdll.dll, version: 10.0.17134.1000, time stamp: 0xcfe5bd82
Exception code: 0xc0000005
Fault offset: 0x000420c8
Faulting process id: 0x1ee0
Faulting application start time: 0x01d407e07f1c3295
Faulting application path: C:\Users\ruehe\source\repos\WpfApp3\WpfApp3\bin\Release\WpfApp3.exe
Faulting module path: C:\WINDOWS\SYSTEM32\ntdll.dll
Report Id: 3a51f5ff-52b3-47fb-b00d-bf002f932c41
Faulting package full name: 
Faulting package-relative application ID: 

So I assume some background thread crashed during termination and Windows Error Reporting keeps the process alive to collect information. Or some kind of timeout kills open resources?

I'm sorry, I just found this. Can I do anything with this crash Information?

Update 2

After spending a lot of time on this and learning some windbg, I think this might be an Nvidia graphics driver crash:

Attaching to the app and then closing the window shows this exception:

(2134.1cac): Unknown exception - code 0000071a (first chance)
(2134.1cac): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
*** ERROR: Symbol file could not be found.  Defaulted to export symbols for C:\WINDOWS\System32\DriverStore\FileRepository\nv_dispi.inf_amd64_040c7acb04cee565\nvwgf2um.dll - 
eax=00000000 ebx=0be09f10 ecx=00000013 edx=00000000 esi=00000000 edi=00000030
eip=779f38e8 esp=007decd0 ebp=007ded10 iopl=0         nv up ei pl zr na pe nc
cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00210246
ntdll!RtlFreeHeap+0x28:
779f38e8 817e08eeddeedd  cmp     dword ptr [esi+8],0DDEEDDEEh ds:002b:00000008=????????

I posted the analyze -v output here: https://gist.github.com/Marv51/d3ad0e9a49b6c985d9116600fd0475b7

From looking at the stacktrace I believe the destructor for the internal class Windows.Data.Pdf.CPdfStatics tries to release some DirectX resources and the Nvidia driver corrupts the heap during this process.

I tried updating to the latest Nvidia driver, which changed nothing.

Not sure where I can go from here. I'm still a windbg beginner, so I might interpret this output wrong.

Maybe the 'solution' for now is that I will do this work in an external process to avoid these issues from 'infecting' the main app.

Upvotes: 1

Views: 961

Answers (0)

Related Questions