From 5e9c70858242ad98c5cafbd2391bd636b588ac74 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Sat, 19 Jan 2008 18:25:45 +0000 Subject: [PATCH] Added use of ref_ptr<> throughout geometry setup code to prevent memory leaks --- src/osgTerrain/GeometryTechnique.cpp | 37 ++++++++++++++-------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/osgTerrain/GeometryTechnique.cpp b/src/osgTerrain/GeometryTechnique.cpp index 6687188ab..62752060b 100644 --- a/src/osgTerrain/GeometryTechnique.cpp +++ b/src/osgTerrain/GeometryTechnique.cpp @@ -255,14 +255,14 @@ void GeometryTechnique::generateGeometry(Locator* masterLocator, const osg::Vec3 unsigned int numVertices = numVerticesInBody+numVerticesInSkirt; // allocate and assign vertices - osg::Vec3Array* _vertices = new osg::Vec3Array; - if (buffer._geometry.valid()) buffer._geometry->setVertexArray(_vertices); + osg::ref_ptr _vertices = new osg::Vec3Array; + if (buffer._geometry.valid()) buffer._geometry->setVertexArray(_vertices.get()); // allocate and assign normals - osg::Vec3Array* _normals = new osg::Vec3Array; + osg::ref_ptr _normals = new osg::Vec3Array; if (buffer._geometry.valid()) { - buffer._geometry->setNormalArray(_normals); + buffer._geometry->setNormalArray(_normals.get()); buffer._geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); } @@ -274,7 +274,7 @@ void GeometryTechnique::generateGeometry(Locator* masterLocator, const osg::Vec3 float scaleHeight = 1.0; // allocate and assign tex coords - osg::Vec2Array* _texcoords = 0; + osg::ref_ptr _texcoords; if (colorLayer) { color_index = texcoord_index; @@ -282,10 +282,10 @@ void GeometryTechnique::generateGeometry(Locator* masterLocator, const osg::Vec3 _texcoords = new osg::Vec2Array; - if (buffer._geometry.valid()) buffer._geometry->setTexCoordArray(color_index, _texcoords); + if (buffer._geometry.valid()) buffer._geometry->setTexCoordArray(color_index, _texcoords.get()); } - osg::FloatArray* _elevations = new osg::FloatArray; + osg::ref_ptr _elevations = new osg::FloatArray; osg::TransferFunction1D* tf = dynamic_cast(colorTF); if (tf) { @@ -295,25 +295,25 @@ void GeometryTechnique::generateGeometry(Locator* masterLocator, const osg::Vec3 if (!colorLayer) { // _elevations = new osg::FloatArray(numVertices); - if (buffer._geometry.valid()) buffer._geometry->setTexCoordArray(tf_index, _elevations); + if (buffer._geometry.valid()) buffer._geometry->setTexCoordArray(tf_index, _elevations.get()); minHeight = tf->getMinimum(); scaleHeight = 1.0f/(tf->getMaximum()-tf->getMinimum()); } } - if (_vertices) _vertices->reserve(numVertices); - if (_texcoords) _texcoords->reserve(numVertices); - if (_elevations) _elevations->reserve(numVertices); - if (_normals) _normals->reserve(numVertices); + if (_vertices.valid()) _vertices->reserve(numVertices); + if (_texcoords.valid()) _texcoords->reserve(numVertices); + if (_elevations.valid()) _elevations->reserve(numVertices); + if (_normals.valid()) _normals->reserve(numVertices); // allocate and assign color - osg::Vec4Array* _colors = new osg::Vec4Array(1); + osg::ref_ptr _colors = new osg::Vec4Array(1); (*_colors)[0].set(1.0f,1.0f,1.0f,1.0f); if (buffer._geometry.valid()) { - buffer._geometry->setColorArray(_colors); + buffer._geometry->setColorArray(_colors.get()); buffer._geometry->setColorBinding(osg::Geometry::BIND_OVERALL); } @@ -365,7 +365,7 @@ void GeometryTechnique::generateGeometry(Locator* masterLocator, const osg::Vec3 } - if (_elevations) + if (_elevations.valid()) { (*_elevations).push_back((ndc.z()-minHeight)*scaleHeight); } @@ -389,10 +389,10 @@ void GeometryTechnique::generateGeometry(Locator* masterLocator, const osg::Vec3 // bool optimizeOrientations = _elevations!=0; bool swapOrientation = !(masterLocator->orientationOpenGL()); - osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_TRIANGLES); + osg::ref_ptr elements = new osg::DrawElementsUInt(GL_TRIANGLES); elements->reserve((numRows-1) * (numColumns-1) * 6); - if (buffer._geometry.valid()) buffer._geometry->addPrimitiveSet(elements); + if (buffer._geometry.valid()) buffer._geometry->addPrimitiveSet(elements.get()); for(j=0; jsetUseDisplayList(false); + // if (buffer._geometry.valid()) _terrainGeometry->setUseDisplayList(false); if (buffer._geometry.valid()) buffer._geometry->setUseVertexBufferObjects(true); }