From 86f3c2a855ca62db74129e7d40263cdd064b42c7 Mon Sep 17 00:00:00 2001 From: CHEMIN Sebastien Date: Mon, 4 Dec 2023 09:25:35 +0100 Subject: [PATCH] Fix memory freed --- src/GLViewer/GLViewer_BaseObjects.cxx | 34 ++++++++++++++++++++------- src/GLViewer/GLViewer_BaseObjects.h | 2 +- src/GLViewer/GLViewer_Context.cxx | 12 +++++++--- src/GLViewer/GLViewer_Object.cxx | 23 ++++++++++-------- src/GLViewer/GLViewer_Viewer2d.cxx | 7 ++++++ 5 files changed, 55 insertions(+), 23 deletions(-) diff --git a/src/GLViewer/GLViewer_BaseObjects.cxx b/src/GLViewer/GLViewer_BaseObjects.cxx index 17a8a983c..ee04f2c10 100644 --- a/src/GLViewer/GLViewer_BaseObjects.cxx +++ b/src/GLViewer/GLViewer_BaseObjects.cxx @@ -59,10 +59,11 @@ GLViewer_MarkerSet::GLViewer_MarkerSet( int number, float size, const QString& t */ GLViewer_MarkerSet::~GLViewer_MarkerSet() { - if ( myXCoord ) - delete[] myXCoord; - if ( myYCoord ) - delete[] myYCoord; + delete[] myXCoord; + delete[] myYCoord; + + myXCoord = nullptr; + myYCoord = nullptr; } /*! @@ -590,6 +591,9 @@ GLViewer_Rect* GLViewer_MarkerSet::getUpdateRect() */ void GLViewer_MarkerSet::setXCoord( GLfloat* xCoord, int size ) { + delete[] myXCoord; + myXCoord = nullptr; + myXCoord = new GLfloat[ size ]; for( int i = 0; i < size; i++ ) myXCoord[i] = xCoord[i]; @@ -602,6 +606,9 @@ void GLViewer_MarkerSet::setXCoord( GLfloat* xCoord, int size ) */ void GLViewer_MarkerSet::setYCoord( GLfloat* yCoord, int size ) { + delete[] myYCoord; + myYCoord = nullptr; + myYCoord = new GLfloat[ size ]; for( int i = 0; i < size; i++ ) myYCoord[i] = yCoord[i]; @@ -840,10 +847,11 @@ GLViewer_Polyline::GLViewer_Polyline( int number, float /*size*/, const QString& */ GLViewer_Polyline::~GLViewer_Polyline() { - if ( myXCoord ) - delete[] myXCoord; - if ( myYCoord ) - delete[] myYCoord; + delete[] myXCoord; + delete[] myYCoord; + + myXCoord = nullptr; + myYCoord = nullptr; } /*! @@ -993,7 +1001,9 @@ GLViewer_Rect* GLViewer_Polyline::getUpdateRect() GLViewer_Drawer* GLViewer_Polyline::createDrawer() { // cout << "GLViewer_MarkerSet::createDrawer" << endl; - return myDrawer = new GLViewer_PolylineDrawer(); + delete myDrawer; + myDrawer = nullptr; + return myDrawer = new GLViewer_PolylineDrawer(); } /*! @@ -1177,6 +1187,9 @@ GLboolean GLViewer_Polyline::unselect() */ void GLViewer_Polyline::setXCoord( GLfloat* xCoord, int size ) { + delete[] myXCoord; + myXCoord = nullptr; + myXCoord = new GLfloat[ size ]; for( int i = 0; i < size; i++ ) myXCoord[i] = xCoord[i]; @@ -1189,6 +1202,9 @@ void GLViewer_Polyline::setXCoord( GLfloat* xCoord, int size ) */ void GLViewer_Polyline::setYCoord( GLfloat* yCoord, int size ) { + delete[] myYCoord; + myYCoord = nullptr; + myYCoord = new GLfloat[ size ]; for( int i = 0; i < size; i++ ) myYCoord[i] = yCoord[i]; diff --git a/src/GLViewer/GLViewer_BaseObjects.h b/src/GLViewer/GLViewer_BaseObjects.h index fe4f6c886..609db9acc 100644 --- a/src/GLViewer/GLViewer_BaseObjects.h +++ b/src/GLViewer/GLViewer_BaseObjects.h @@ -127,7 +127,7 @@ class GLVIEWER_API GLViewer_Polyline: public GLViewer_Object { public: GLViewer_Polyline( int number = 1, float size = 5.0, const QString& toolTip = "GLPolyline" ); - ~GLViewer_Polyline(); + virtual ~GLViewer_Polyline(); // redefined methods virtual void compute(); diff --git a/src/GLViewer/GLViewer_Context.cxx b/src/GLViewer/GLViewer_Context.cxx index b3e5c4705..6c25630a3 100644 --- a/src/GLViewer/GLViewer_Context.cxx +++ b/src/GLViewer/GLViewer_Context.cxx @@ -69,9 +69,13 @@ GLViewer_Context::GLViewer_Context( GLViewer_Viewer2d* v ) : */ GLViewer_Context::~GLViewer_Context() { - myActiveObjects.clear(); - myInactiveObjects.clear(); - mySelectedObjects.clear(); + qDeleteAll(myActiveObjects); + myActiveObjects.clear(); + + qDeleteAll(myInactiveObjects); + myInactiveObjects.clear(); + + mySelectedObjects.clear(); } /*! @@ -115,6 +119,8 @@ int GLViewer_Context::MoveTo( int xi, int yi, bool byCircle ) object->highlight( x, y, myTolerance, GL_FALSE ); isHigh = object->isHighlighted(); } + delete rect; + rect = nullptr; if( isHigh ) { diff --git a/src/GLViewer/GLViewer_Object.cxx b/src/GLViewer/GLViewer_Object.cxx index 748561b56..1bd868e65 100644 --- a/src/GLViewer/GLViewer_Object.cxx +++ b/src/GLViewer/GLViewer_Object.cxx @@ -42,8 +42,8 @@ GLViewer_Object::GLViewer_Object() myIsHigh = GL_FALSE; myIsSel = GL_FALSE; - myRect = new GLViewer_Rect();; - myUpdateRect = new GLViewer_Rect();; + myRect = new GLViewer_Rect(); + myUpdateRect = new GLViewer_Rect(); myGLText = new GLViewer_Text( 0, 0, 0, QColor(0,0,0) ); myAspectLine = new GLViewer_AspectLine(); @@ -64,17 +64,20 @@ GLViewer_Object::GLViewer_Object() */ GLViewer_Object::~GLViewer_Object() { - if( myRect ) - delete myRect; + delete myRect; + myRect = nullptr; - if( myUpdateRect ) - delete myUpdateRect; + delete myUpdateRect; + myUpdateRect = nullptr; - if( myGLText ) - delete myGLText; + delete myGLText; + myGLText = nullptr; - if( myAspectLine ) - delete myAspectLine; + delete myAspectLine; + myAspectLine = nullptr; + + delete myOwner; + myOwner = nullptr; } /*! diff --git a/src/GLViewer/GLViewer_Viewer2d.cxx b/src/GLViewer/GLViewer_Viewer2d.cxx index 11028eb0a..ad1164e3e 100644 --- a/src/GLViewer/GLViewer_Viewer2d.cxx +++ b/src/GLViewer/GLViewer_Viewer2d.cxx @@ -69,6 +69,13 @@ GLViewer_Viewer2d::~GLViewer_Viewer2d() { //myGLSketcher = 0; //delete myGLSketcher; + + qDeleteAll(myDrawers); + myDrawers.clear(); + + delete myGLContext; + myGLContext = nullptr; + GLViewer_TexFont::clearTextBases(); } -- 2.39.2