Hi guys, I'm having a bit of trouble with printing in my application. My biggest problem is that I know NOTHING about printing. I've poked around here and there, but for the most part, I leave it as is. This code has been in our program for 3 years now, and it's worked great until now. Now, we've updated to Visual Studio 2008 (and MFC 9), and we're now getting issues with the code. I was hoping that somebody could take a peek at it and let me know if there's anything obvious that jumps off of the page at you. I hate just posting code and saying 'fix this', but I'm hoping that a more skilled eye will be able to spot something that I'll miss 100 times over. :/

The issue we're having is that this works fine the first few times. Never fails the first time. But if you try to print the same page again, without doing anything else, it crashes. The line it fails at is the 'int ret = ...' line about 10 lines down. It throws an access violation exception. But again, only on the second or consecutive times. Never on the first try. It makes me think that there are resources that aren't being freed somewhere, but I can't figure out where. Any help on this would be much appreciated.

void CReportWnd::OnPrint() 
	CDC dc;
	CPrintDialog dlg (FALSE);
	CPrintDialog defaults(FALSE);
	DEVMODE *ldev_printinfo;
	int li_first = 0;
	int li_last;

	int ret = defaults.GetDefaults();
	ldev_printinfo = defaults.GetDevMode();
	//ldev_printinfo->dmOrientation = DMORIENT_LANDSCAPE;
	dc.Attach (defaults.GetPrinterDC ());
	PROPrint(1, NULL, &dc, NULL, &li_last, true);
	dlg.m_pd.hDevMode = ldev_printinfo;
	dlg.m_pd.Flags &= ~PD_NOPAGENUMS;
	dlg.m_pd.nMinPage = 1;
	dlg.m_pd.nFromPage = 1;
	dlg.m_pd.nMaxPage = li_last;
	dlg.m_pd.nToPage = li_last;		

	if (dlg.DoModal () == IDOK)	{
		dc.Attach (dlg.GetPrinterDC ());		
	} else {

	//Set up document info (need to set the name)
	::ZeroMemory (&di, sizeof (DOCINFO));
	di.cbSize = sizeof (DOCINFO);
	di.lpszDocName = "Report";

	if(dc.StartDoc(&di) <= 0) {

	if(dlg.PrintRange()) {
		li_first = dlg.m_pd.nFromPage - 1;
		li_last = dlg.m_pd.nToPage - 1;

	//Now do the actual print job to the printing device
	PROPrint(1, NULL, &dc, &li_first, &li_last, false);