From 5ab2f2eaf9b4fa6c530b657c812880bffe811187 Mon Sep 17 00:00:00 2001 From: Anakin Date: Sat, 26 Nov 2016 15:39:59 +0100 Subject: [PATCH] fixed the memory garbage problem --- MshViewer/Header/Object.h | 2 +- MshViewer/Source/Object.cpp | 24 +++++++++++++----------- MshViewer/Source/OpenGlController.cpp | 18 ++++++++++-------- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/MshViewer/Header/Object.h b/MshViewer/Header/Object.h index 03c84d4..7aeca22 100644 --- a/MshViewer/Header/Object.h +++ b/MshViewer/Header/Object.h @@ -24,7 +24,7 @@ struct Segment { std::uint32_t textureIndex = 0; float* vertex = nullptr; float* uv = nullptr; - std::vector*> meshIndices; // indices into vertex array + std::vector> meshIndices; // indices into vertex array }; struct Modl { diff --git a/MshViewer/Source/Object.cpp b/MshViewer/Source/Object.cpp index 83cd921..c0b7855 100644 --- a/MshViewer/Source/Object.cpp +++ b/MshViewer/Source/Object.cpp @@ -434,7 +434,7 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list& fsMesh.read(reinterpret_cast(&tempSize), sizeof(tempSize)); int highBitCount(0); - std::vector* tempPoly = new std::vector; + std::vector tempPoly; // = new std::vector; for (unsigned int i = 0; i < tempSize; i++) { @@ -449,7 +449,7 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list& tempValue = (std::uint16_t(tempValue << 1) >> 1); } - tempPoly->push_back((std::uint32_t)tempValue); + tempPoly.push_back((std::uint32_t)tempValue); // new Polygon found if (highBitCount == 2) @@ -461,18 +461,20 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list& std::uint32_t saveData[2]; for (int i = 0; i < 2; i++) { - saveData[i] = tempPoly->back(); - tempPoly->pop_back(); + saveData[i] = tempPoly.back(); + tempPoly.pop_back(); } // ..and save them in the new vector - std::vector* newPointer = new std::vector; + tempData->meshIndices.push_back(tempPoly); + + tempPoly.clear(); for (int i = 1; i >= 0; i--) - newPointer->push_back(saveData[i]); + tempPoly.push_back(saveData[i]); // save the old vector and set the pointer to the new one - tempData->meshIndices.push_back(tempPoly); - tempPoly = newPointer; + //tempData->meshIndices.push_back(tempPoly); + //tempPoly = newPointer; } // if high bit set } // for all values @@ -542,19 +544,19 @@ void Object::analyseClthChunks(Modl * dataDestination, std::list& fsMesh.seekg((*it)->position); fsMesh.read(reinterpret_cast(&tempSize), sizeof(tempSize)); - std::vector* tempPoly; + std::vector tempPoly; // for every triangle.. for (unsigned int i = 0; i < tempSize; i += 3) { - tempPoly = new std::vector; + tempPoly.clear(); // ..get the 3 indices and save them for (int j = 0; j < 3; j++) { std::uint32_t tempValue; fsMesh.read(reinterpret_cast(&tempValue), sizeof(std::uint32_t)); - tempPoly->push_back(tempValue); + tempPoly.push_back(tempValue); } tempData->meshIndices.push_back(tempPoly); } diff --git a/MshViewer/Source/OpenGlController.cpp b/MshViewer/Source/OpenGlController.cpp index c438a1a..0fcdba2 100644 --- a/MshViewer/Source/OpenGlController.cpp +++ b/MshViewer/Source/OpenGlController.cpp @@ -126,10 +126,10 @@ void OpenGLController::deleteVectors() while (!segmCuror->meshIndices.empty()) { - std::vector* meshCursor = segmCuror->meshIndices.back(); - meshCursor->clear(); + while (!segmCuror->meshIndices.back().empty()) + segmCuror->meshIndices.back().pop_back(); + segmCuror->meshIndices.pop_back(); - delete meshCursor; } delete segmCuror; @@ -350,7 +350,7 @@ void OpenGLController::updateScene() // calculate the number of vertex unsigned int vertexCount(0); for (auto& it : segIt->meshIndices) - vertexCount += (it->size() - 2) * 3; + vertexCount += (it.size() - 2) * 3; glDrawArrays(GL_TRIANGLES, instanceOffset, vertexCount); @@ -393,10 +393,10 @@ void OpenGLController::loadMsh(const char * path) { for (auto& mshIt : segIt->meshIndices) // for every polygon { - if (mshIt->size() >= 3) // multipoly + if (mshIt.size() >= 3) // multipoly { // for every triangle of the multi polygon - for (unsigned int tri = 0; tri < mshIt->size() - 2; tri++) + for (unsigned int tri = 0; tri < mshIt.size() - 2; tri++) { // for every edge of the triangle for (int triEdge = 0; triEdge < 3; triEdge++) @@ -404,7 +404,7 @@ void OpenGLController::loadMsh(const char * path) Vertex tempVertex; // every edge has 3 coordinates for (int j = 0; j < 3; j++) - tempVertex.position[j] = (GLfloat)segIt->vertex[(*mshIt)[tri + triEdge - ((tri % 2) * (triEdge - 1) * 2)] * 3 + j]; + tempVertex.position[j] = (GLfloat)segIt->vertex[mshIt[tri + triEdge - ((tri % 2) * (triEdge - 1) * 2)] * 3 + j]; // and 2 UV if (segIt->uv == NULL) @@ -415,7 +415,7 @@ void OpenGLController::loadMsh(const char * path) else { for (int j = 0; j < 2; j++) - tempVertex.uv[j] = (GLfloat)segIt->uv[(*mshIt)[tri + triEdge - ((tri % 2) * (triEdge - 1) * 2)] * 2 + j]; + tempVertex.uv[j] = (GLfloat)segIt->uv[mshIt[tri + triEdge - ((tri % 2) * (triEdge - 1) * 2)] * 2 + j]; } tempBufferData.push_back(tempVertex); } @@ -441,6 +441,8 @@ void OpenGLController::loadMsh(const char * path) ); glBindBuffer(GL_ARRAY_BUFFER, 0); + tempBufferData.clear(); + // get textures path std::string tempPath = path;