From: timoore Date: Thu, 17 Jan 2008 08:28:15 +0000 (+0000) Subject: fix memory leaks in random object code X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=0c9013e60e5d7a9762fdd93180d02725d6873e57;p=simgear.git fix memory leaks in random object code Don't allocate mt structures (for the random number generator) on the heap. --- diff --git a/simgear/scene/tgdb/SGTexturedTriangleBin.hxx b/simgear/scene/tgdb/SGTexturedTriangleBin.hxx index bec81978..bbeb6244 100644 --- a/simgear/scene/tgdb/SGTexturedTriangleBin.hxx +++ b/simgear/scene/tgdb/SGTexturedTriangleBin.hxx @@ -98,8 +98,7 @@ class SGTexturedTriangleBin : public SGTriangleBin { public: SGTexturedTriangleBin() { - seed = new mt; - mt_init(seed, 123); + mt_init(&seed, 123); } // Computes and adds random surface points to the points list. @@ -107,7 +106,7 @@ public: // The points are offsetted away from the triangles in // offset * positive normal direction. void addRandomSurfacePoints(float coverage, float offset, - std::vector& points) const + std::vector& points) { unsigned num = getNumTriangles(); for (unsigned i = 0; i < num; ++i) { @@ -125,13 +124,13 @@ public: // For partial units of area, use a zombie door method to // create the proper random chance of a light being created // for this triangle - float unit = area + mt_rand(seed)*coverage; + float unit = area + mt_rand(&seed)*coverage; SGVec3f offsetVector = offset*normalize(normal); // generate a light point for each unit of area while ( coverage < unit ) { - float a = mt_rand(seed); - float b = mt_rand(seed); + float a = mt_rand(&seed); + float b = mt_rand(&seed); if ( a + b > 1 ) { a = 1 - a; b = 1 - b; @@ -145,7 +144,7 @@ public: } void addRandomPoints(float coverage, - std::vector& points) const + std::vector& points) { unsigned num = getNumTriangles(); for (unsigned i = 0; i < num; ++i) { @@ -163,12 +162,12 @@ public: // for partial units of area, use a zombie door method to // create the proper random chance of an object being created // for this triangle. - double num = area / coverage + mt_rand(seed); + double num = area / coverage + mt_rand(&seed); // place an object each unit of area while ( num > 1.0 ) { - float a = mt_rand(seed); - float b = mt_rand(seed); + float a = mt_rand(&seed); + float b = mt_rand(&seed); if ( a + b > 1 ) { a = 1 - a; b = 1 - b; @@ -243,7 +242,7 @@ public: private: // Random seed for the triangle. - mt* seed; + mt seed; }; #endif diff --git a/simgear/scene/tgdb/obj.cxx b/simgear/scene/tgdb/obj.cxx index 4e904415..88275137 100644 --- a/simgear/scene/tgdb/obj.cxx +++ b/simgear/scene/tgdb/obj.cxx @@ -400,11 +400,11 @@ struct SGTileGeometryBin { void computeRandomSurfaceLights(SGMaterialLib* matlib) { - SGMaterialTriangleMap::const_iterator i; + SGMaterialTriangleMap::iterator i; // generate a repeatable random seed - mt* seed = new mt; - mt_init(seed, unsigned(123)); + mt seed; + mt_init(&seed, unsigned(123)); for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) { SGMaterial *mat = matlib->find(i->first); @@ -424,9 +424,9 @@ struct SGTileGeometryBin { i->second.addRandomSurfacePoints(coverage, 3, randomPoints); std::vector::iterator j; for (j = randomPoints.begin(); j != randomPoints.end(); ++j) { - float zombie = mt_rand(seed); + float zombie = mt_rand(&seed); // factor = sg_random() ^ 2, range = 0 .. 1 concentrated towards 0 - float factor = mt_rand(seed); + float factor = mt_rand(&seed); factor *= factor; float bright = 1; @@ -451,7 +451,7 @@ struct SGTileGeometryBin { void computeRandomObjects(SGMaterialLib* matlib) { - SGMaterialTriangleMap::const_iterator i; + SGMaterialTriangleMap::iterator i; for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) { SGMaterial *mat = matlib->find(i->first); if (!mat) @@ -528,8 +528,8 @@ SGLoadBTG(const std::string& path, SGMaterialLib *matlib, bool calc_lights, bool if (tileGeometryBin.randomModels.getNumModels() > 0) { // Generate a repeatable random seed - mt* seed = new mt; - mt_init(seed, unsigned(123)); + mt seed; + mt_init(&seed, unsigned(123)); // Determine an rotation matrix for the models to place them // perpendicular to the earth's surface. We use the same matrix, @@ -555,7 +555,7 @@ SGLoadBTG(const std::string& path, SGMaterialLib *matlib, bool calc_lights, bool if (obj.model->get_heading_type() == SGMatModel::HEADING_RANDOM) { // Rotate the object around the z axis. - double hdg = mt_rand(seed) * M_PI * 2; + double hdg = mt_rand(&seed) * M_PI * 2; osg::Matrix rot(cos(hdg), -sin(hdg), 0, 0, sin(hdg), cos(hdg), 0, 0, 0, 0, 1, 0,