From 595f023afbe4ddce0960ce4dd01098cee30f8f2e Mon Sep 17 00:00:00 2001 From: vsv Date: Mon, 22 Sep 2014 09:46:46 +0400 Subject: [PATCH] Avoid memory leaks --- src/XGUI/XGUI_ViewPort.cpp | 8 ++++---- src/XGUI/XGUI_ViewPort.h | 2 +- src/XGUI/XGUI_ViewWindow.cpp | 18 ++++++++++++++---- src/XGUI/XGUI_Workshop.cpp | 1 + 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/XGUI/XGUI_ViewPort.cpp b/src/XGUI/XGUI_ViewPort.cpp index d5f31152b..02ed79251 100644 --- a/src/XGUI/XGUI_ViewPort.cpp +++ b/src/XGUI/XGUI_ViewPort.cpp @@ -305,7 +305,7 @@ void XGUI_ViewPort::resizeEvent(QResizeEvent* theEvent) } //*********************************************** -QImage XGUI_ViewPort::dumpView(QRect theRect, bool toUpdate) +QImage XGUI_ViewPort::dumpView(unsigned char*& theData, QRect theRect, bool toUpdate) { Handle(V3d_View) view = getView(); if (view.IsNull()) @@ -322,7 +322,7 @@ QImage XGUI_ViewPort::dumpView(QRect theRect, bool toUpdate) } QApplication::syncX(); - unsigned char* data = new unsigned char[aWidth * aHeight * 4]; + theData = new unsigned char[aWidth * aHeight * 4]; QPoint p; if (theRect.isNull()) { @@ -334,9 +334,9 @@ QImage XGUI_ViewPort::dumpView(QRect theRect, bool toUpdate) view->Redraw(theRect.x(), theRect.y(), theRect.width(), theRect.height()); p = theRect.topLeft(); } - glReadPixels(p.x(), p.y(), aWidth, aHeight, GL_RGBA, GL_UNSIGNED_BYTE, data); + glReadPixels(p.x(), p.y(), aWidth, aHeight, GL_RGBA, GL_UNSIGNED_BYTE, theData); - QImage anImage(data, aWidth, aHeight, QImage::Format_ARGB32); + QImage anImage(theData, aWidth, aHeight, QImage::Format_ARGB32); anImage = anImage.mirrored(); anImage = anImage.rgbSwapped(); return anImage; diff --git a/src/XGUI/XGUI_ViewPort.h b/src/XGUI/XGUI_ViewPort.h index 7c2ad4b22..f22cc9d92 100644 --- a/src/XGUI/XGUI_ViewPort.h +++ b/src/XGUI/XGUI_ViewPort.h @@ -25,7 +25,7 @@ Q_OBJECT return 0; } - QImage dumpView(QRect theRect = QRect(), bool toUpdate = true); + QImage dumpView(unsigned char*& theData, QRect theRect = QRect(), bool toUpdate = true); Handle(V3d_View) getView() const { diff --git a/src/XGUI/XGUI_ViewWindow.cpp b/src/XGUI/XGUI_ViewWindow.cpp index b9c346009..2d2b4cc1c 100644 --- a/src/XGUI/XGUI_ViewWindow.cpp +++ b/src/XGUI/XGUI_ViewWindow.cpp @@ -98,7 +98,8 @@ void ViewerToolbar::paintEvent(QPaintEvent* theEvent) QRect aImgRect( QRect(aPnt.x(), aPnt.y() + aVPRect.height() - aRect.height(), aRect.width(), aRect.height())); - QImage aImg = myVPort->dumpView(aImgRect, myResize); + unsigned char* aData = 0; + QImage aImg = myVPort->dumpView(aData, aImgRect, myResize); if (!aImg.isNull()) aPainter.drawImage(aRect, aImg); myResize = false; @@ -111,6 +112,8 @@ void ViewerToolbar::paintEvent(QPaintEvent* theEvent) aOpt.rect = style->subElementRect(QStyle::SE_ToolBarHandle, &aOpt, this); if (aOpt.rect.isValid()) style->drawPrimitive(QStyle::PE_IndicatorToolBarHandle, &aOpt, &aPainter, this); + if (aData) + delete aData; } //************************************************************************** @@ -131,11 +134,14 @@ void ViewerLabel::paintEvent(QPaintEvent* theEvent) QRect aImgRect( QRect(aPnt.x(), aPnt.y() + aVPRect.height() - aRect.height(), aRect.width(), aRect.height())); - QImage aImg = myVPort->dumpView(aImgRect, myResize); + unsigned char* aData = 0; + QImage aImg = myVPort->dumpView(aData, aImgRect, myResize); if (!aImg.isNull()) QPainter(this).drawImage(aRect, aImg); myResize = false; QLabel::paintEvent(theEvent); + if (aData) + delete aData; } //************************************************************************** @@ -399,7 +405,8 @@ void XGUI_ViewWindow::onClose() //**************************************************************** void XGUI_ViewWindow::onMinimize() { - QPixmap aPMap = QPixmap::fromImage(myViewPort->dumpView()); + unsigned char* aData = 0; + QPixmap aPMap = QPixmap::fromImage(myViewPort->dumpView(aData)); int aW = width(); int aH = height(); double aR = aW / 100.; @@ -414,6 +421,7 @@ void XGUI_ViewWindow::onMinimize() parentWidget()->lower(); windowDeactivated(); myViewer->onWindowMinimized((QMdiSubWindow*) parentWidget()); + delete aData; } //**************************************************************** @@ -1045,7 +1053,8 @@ void XGUI_ViewWindow::dumpView() &aSelectedFilter); if (!aFileName.isNull()) { QApplication::setOverrideCursor(Qt::WaitCursor); - QImage aPicture = myViewPort->dumpView(); + unsigned char* aData = 0; + QImage aPicture = myViewPort->dumpView(aData); QString aFmt = XGUI_Tools::extension(aFileName).toUpper(); if (aFmt.isEmpty()) @@ -1068,6 +1077,7 @@ void XGUI_ViewWindow::dumpView() #endif else aPicture.save(aFileName, aFmt.toLatin1()); + delete aData; QApplication::restoreOverrideCursor(); } } diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index d337c1856..d0d12a789 100644 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -126,6 +126,7 @@ XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector) //****************************************************** XGUI_Workshop::~XGUI_Workshop(void) { + delete myDisplayer; } //****************************************************** -- 2.39.2