Crash when showing print setup dialog from Windows forms

W

WilS

Hi,
I have a simple Windows Forms app that has printing support. The app
uses System.Drawing.Printing.PrintDocument support to launch the print
dialog, and subsequently print. On several machines, this all works fine.
But on a couple of machines, it crashes itermittantly, and on a couple more
machines, it crashes pretty consistently. The errors/exceptions I get vary
from <poof> app disapperas, to OutOfMemoryException to native access
violation (0xC0000005), and even occasionally StackOverflowException. My
thinking is that there must be a bug in .NET 3.5 SP1 in the print wrapper
stuff (System.Drawing.Printing.PrintDocument) that is effectively a wild
write which manifests itself differently at different times and on different
machines.

Here's the basic code that I'm trying to run (omittng the basic Form that
hosts my 'print' button)

public Form1()
{
InitializeComponent();
m_PageSettings = new PageSettings();
m_PrinterSettings = new PrinterSettings();
printDocument1.DefaultPageSettings = m_PageSettings;
printDocument1.PrinterSettings = m_PrinterSettings;
printDocument1.PrintPage += new
PrintPageEventHandler(m_PrintDoc_PrintPage);
}

private PageSettings m_PageSettings;
private PrinterSettings m_PrinterSettings;

private void button1_Click(object sender, EventArgs e)
{
printDialog1.Document = printDocument1;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
}


}
It crashes the first time you load it up, hit the print button, and select
(in my case) the Microsoft XPS Document Writer printer. If you're trying to
debug, it often takes out Visual Studio altogether. (Note that
m-PrintDoc_PrintPage never gets called - the crash occurs before it gets to
any of my actual printing logic.)

One other test case: I have a Windows 7 32-bit machine that I Remote
Desktop into for testing. I see the failure when I use the 'local print'
resources (so my local printers are "exported" to the Windows7 machine), but
I don't see the failure if I don't export the printers. (Both the host and
target machines have the MS XPS Document printer.) So perhaps having a
longer list of installed printers or a particular list of installed printers
contributes to the bug?

Does anybody have any suggestions about what this might be, or anything to
try to work around this? I'm very suspicious that the CopyToHdevmode call
is the one crashing, as this is the function that copies the managed printer
setup state into the native DEVMODE. Could this function have a wild write
bug?

Thanks,
Wil

Here's a callstack from one of the crashes where VS didn't get killed:

ntdll.dll!_RtlpAnalyzeHeapFailure@12() + 0x20a bytes
ntdll.dll!_RtlpCoalesceFreeBlocks@16() + 0x349a2 bytes
ntdll.dll!@RtlpFreeHeap@16() + 0x10b bytes
ntdll.dll!_RtlFreeHeap@12() + 0xe37 bytes
ntdll.dll!_RtlDebugFreeHeap@12() + 0x1f9 bytes
ntdll.dll!@RtlpFreeHeap@16() + 0x34d58 bytes
ntdll.dll!_RtlFreeHeap@12() + 0xe37 bytes
rpcrt4.dll!FreeWrapper() + 0x35 bytes
rpcrt4.dll!operator delete() + 0xe bytes
rpcrt4.dll!DCE_BINDING::`scalar deleting destructor'() + 0x2c bytes
rpcrt4.dll!LRPC_BINDING_HANDLE::~LRPC_BINDING_HANDLE() + 0x22 bytes
rpcrt4.dll!LRPC_BINDING_HANDLE::`vector deleting destructor'() + 0xf
bytes
rpcrt4.dll!LRPC_BASE_BINDING_HANDLE::FreeObject() + 0x18 bytes
rpcrt4.dll!LRPC_BIND_CCALL::~LRPC_BIND_CCALL() + 0x15 bytes
rpcrt4.dll!LRPC_CCALL::~LRPC_CCALL() + 0x2c bytes
rpcrt4.dll!LRPC_CCALL::`scalar deleting destructor'() + 0xd bytes
rpcrt4.dll!LRPC_BASE_CCALL::FreeObject() + 0x241c bytes
rpcrt4.dll!LRPC_BASE_CCALL::FreeBuffer() + 0x48 bytes
rpcrt4.dll!LRPC_CCALL::FreeBuffer() + 0x2c bytes
rpcrt4.dll!_I_RpcFreeBuffer@4() + 0x11 bytes
rpcrt4.dll!_NdrFreeBuffer@4() + 0x1f bytes
rpcrt4.dll!@NdrpClientFinally@8() + 0x4fc bytes
rpcrt4.dll!_NdrClientCall2() + 0x187 bytes
rpcrt4.dll!_NdrClientCall2() + 0x165 bytes
rpcrt4.dll!@NdrpSendReceive@4()
[Managed to Native Transition]
System.Drawing.dll!System.Drawing.Printing.PageSettings.CopyToHdevmode(System.IntPtr
hdevmode = 64684148) Line 396 C#
System.Drawing.dll!System.Drawing.Printing.PrinterSettings.GetHdevmode(System.Drawing.Printing.PageSettings
pageSettings) Line 1041 C#
System.Drawing.dll!System.Drawing.Printing.PrintController.OnStartPrint(System.Drawing.Printing.PrintDocument
document, System.Drawing.Printing.PrintEventArgs e) Line 166 + 0x21 bytes C#
System.Windows.Forms.dll!System.Windows.Forms.PrintControllerWithStatusDialog.OnStartPrint(System.Drawing.Printing.PrintDocument
document = {System.Drawing.Printing.PrintDocument},
System.Drawing.Printing.PrintEventArgs e =
{System.Drawing.Printing.PrintEventArgs}) Line 72 C#
System.Drawing.dll!System.Drawing.Printing.PrintController.Print(System.Drawing.Printing.PrintDocument
document = {System.Drawing.Printing.PrintDocument}) Line 76 C#
System.Drawing.dll!System.Drawing.Printing.PrintDocument.Print() Line 334
C#
 
Top