diff --git a/src/printsupport/kernel/qprintengine_win.cpp b/src/printsupport/kernel/qprintengine_win.cpp index 427af13edf..b97dc6d2c2 100644 --- a/src/printsupport/kernel/qprintengine_win.cpp +++ b/src/printsupport/kernel/qprintengine_win.cpp @@ -844,14 +844,13 @@ void QWin32PrintEngine::drawPolygon(const QPointF *points, int pointCount, Polyg QWin32PrintEnginePrivate::~QWin32PrintEnginePrivate() { - if (hdc) - release(); + release(); } void QWin32PrintEnginePrivate::initialize() { - if (hdc) - release(); + release(); + Q_ASSERT(!hPrinter); Q_ASSERT(!hdc); Q_ASSERT(!devMode); @@ -878,18 +877,19 @@ void QWin32PrintEnginePrivate::initialize() if (!ok) { qErrnoWarning("QWin32PrintEngine::initialize: GetPrinter failed"); - GlobalUnlock(pInfo); - GlobalFree(hMem); - ClosePrinter(hPrinter); - pInfo = 0; - hMem = 0; - hPrinter = 0; + release(); return; } devMode = pInfo->pDevMode; hdc = CreateDC(NULL, reinterpret_cast(m_printDevice.id().utf16()), 0, devMode); + if (!hdc) { + qErrnoWarning("QWin32PrintEngine::initialize: CreateDC failed"); + release(); + return; + } + Q_ASSERT(hPrinter); Q_ASSERT(pInfo); @@ -941,19 +941,17 @@ void QWin32PrintEnginePrivate::initHDC() void QWin32PrintEnginePrivate::release() { - if (hdc == 0) - return; - if (globalDevMode) { // Devmode comes from print dialog GlobalUnlock(globalDevMode); - } else { // Devmode comes from initialize... + } else if (hMem) { // Devmode comes from initialize... // devMode is a part of the same memory block as pInfo so one free is enough... GlobalUnlock(hMem); GlobalFree(hMem); } if (hPrinter) ClosePrinter(hPrinter); - DeleteDC(hdc); + if (hdc) + DeleteDC(hdc); hdc = 0; hPrinter = 0; diff --git a/src/printsupport/kernel/qprintengine_win_p.h b/src/printsupport/kernel/qprintengine_win_p.h index 9b944d5921..f698992425 100644 --- a/src/printsupport/kernel/qprintengine_win_p.h +++ b/src/printsupport/kernel/qprintengine_win_p.h @@ -125,6 +125,7 @@ public: globalDevMode(0), devMode(0), pInfo(0), + hMem(0), hdc(0), mode(QPrinter::ScreenResolution), state(QPrinter::Idle),