Added support for osg::MemoryManager which is based upon Paul Nettle's

memory manager published at flipcode.com.  This can be turned on
with the OSG_USE_MEMORY_MANGER option which then uses custom global
new and delete operators as well as provide osgNew and osgDelete macro's
which add ability to log line and file from which calls are made.

Updated osg,osgUtil,osgDB,osgText and osgPlugins/osg to use osgNew/osgDelete,
and fixed memory leaks highlighted by the new memory manager.
This commit is contained in:
Robert Osfield
2002-03-26 23:52:52 +00:00
parent 72ff3186df
commit 84d2d01163
107 changed files with 469 additions and 435 deletions

View File

@@ -433,7 +433,7 @@ void Camera::attachTransform(const TransformMode mode, Matrix* matrix)
if (_eyeToModelTransform.valid())
{
_attachedTransformMode = mode;
if (!_modelToEyeTransform.valid()) _modelToEyeTransform = new Matrix;
if (!_modelToEyeTransform.valid()) _modelToEyeTransform = osgNew Matrix;
if (!_modelToEyeTransform->invert(*_eyeToModelTransform))
{
notify(WARN)<<"Warning: Camera::attachTransform() failed to invert _modelToEyeTransform"<<std::endl;
@@ -452,7 +452,7 @@ void Camera::attachTransform(const TransformMode mode, Matrix* matrix)
if (_modelToEyeTransform.valid())
{
_attachedTransformMode = mode;
if (!_eyeToModelTransform.valid()) _eyeToModelTransform = new Matrix;
if (!_eyeToModelTransform.valid()) _eyeToModelTransform = osgNew Matrix;
if (!_eyeToModelTransform->invert(*_modelToEyeTransform))
{
notify(WARN)<<"Warning: Camera::attachTransform() failed to invert _modelToEyeTransform"<<std::endl;
@@ -651,7 +651,7 @@ void Camera::calculateMatricesAndClippingVolume() const
float ty = -(top+bottom)/(top-bottom);
float tz = -(_zFar+_zNear)/(_zFar-_zNear);
_projectionMatrix = new Matrix(
_projectionMatrix = osgNew Matrix(
A, 0.0f, 0.0f, 0.0f,
0.0f, B, 0.0f, 0.0f,
0.0f, 0.0f, C, 0.0f,
@@ -673,7 +673,7 @@ void Camera::calculateMatricesAndClippingVolume() const
float E = -(_zFar+_zNear) / (_zFar-_zNear);
float F = -(2.0*_zFar*_zNear) / (_zFar-_zNear);
_projectionMatrix = new Matrix(
_projectionMatrix = osgNew Matrix(
A, 0.0f, 0.0f, 0.0f,
0.0f, B, 0.0f, 0.0f,
C, D, E, -1.0f,
@@ -695,7 +695,7 @@ void Camera::calculateMatricesAndClippingVolume() const
}
else
{
_modelViewMatrix = new Matrix;
_modelViewMatrix = osgNew Matrix;
_modelViewMatrix->makeIdentity();
}
break;
@@ -711,7 +711,7 @@ void Camera::calculateMatricesAndClippingVolume() const
Vec3 u(s^f);
u.normalize();
ref_ptr<Matrix> matrix = new Matrix(
ref_ptr<Matrix> matrix = osgNew Matrix(
s[0], u[0], -f[0], 0.0f,
s[1], u[1], -f[1], 0.0f,
s[2], u[2], -f[2], 0.0f,
@@ -721,7 +721,7 @@ void Camera::calculateMatricesAndClippingVolume() const
if (_modelToEyeTransform.valid())
{
_modelViewMatrix = new Matrix;
_modelViewMatrix = osgNew Matrix;
(*_modelViewMatrix) = (*matrix) * (*_modelToEyeTransform);
}
else
@@ -791,10 +791,10 @@ void Camera::calculateMatricesAndClippingVolume() const
_clippingVolume.transformProvidingInverse(*_modelViewMatrix);
if (!_mp.valid()) _mp = new Matrix;
if (!_mp.valid()) _mp = osgNew Matrix;
_mp->mult(*_modelViewMatrix,*_projectionMatrix);
if (!_inversemp.valid()) _inversemp = new Matrix;
if (!_inversemp.valid()) _inversemp = osgNew Matrix;
if (!_inversemp->invert(*_mp))
{
notify(WARN)<<"Warning: Camera::calculateMatricesAndClippingVolume() failed to invert _mp"<<std::endl;

View File

@@ -20,7 +20,7 @@ class DisplaySettingsPtr
DisplaySettings* DisplaySettings::instance()
{
static DisplaySettingsPtr s_displaySettings = new DisplaySettings;
static DisplaySettingsPtr s_displaySettings = osgNew DisplaySettings;
return s_displaySettings.get();
}

View File

@@ -9,7 +9,7 @@ using namespace osg;
*/
EarthSky::EarthSky()
{
StateSet* stateset = new StateSet;
StateSet* stateset = osgNew StateSet;
stateset->setRenderBinDetails(-1,"RenderBin");
setStateSet(stateset);

View File

@@ -18,7 +18,7 @@ GeoSet::GeoSet()
// we will use the a default delete functor which
// assumes that users have allocated arrays with new only
// and that now sharing of attributes exists between GeoSet's.
_adf = new AttributeDeleteFunctor;
_adf = osgNew AttributeDeleteFunctor;
_coords = (Vec3 *)0;
@@ -66,7 +66,7 @@ GeoSet::GeoSet(const GeoSet& geoset,const CopyOp& copyop):
_flat_shaded_skip = geoset._flat_shaded_skip;
if (geoset._primLengths)
{
_primLengths = new int [_numprims];
_primLengths = osgNew int [_numprims];
memcpy(_primLengths,geoset._primLengths,_numprims*sizeof(int));
}
else
@@ -78,7 +78,7 @@ GeoSet::GeoSet(const GeoSet& geoset,const CopyOp& copyop):
_cindex = geoset._cindex;
if (geoset._coords)
{
_coords = new Vec3 [_numcoords];
_coords = osgNew Vec3 [_numcoords];
memcpy(_coords,geoset._coords,_numcoords*sizeof(Vec3));
}
else
@@ -91,7 +91,7 @@ GeoSet::GeoSet(const GeoSet& geoset,const CopyOp& copyop):
_nindex = geoset._nindex;
if (geoset._normals)
{
_normals = new Vec3 [_numnormals];
_normals = osgNew Vec3 [_numnormals];
memcpy(_normals,geoset._normals,_numnormals*sizeof(Vec3));
}
else
@@ -104,7 +104,7 @@ GeoSet::GeoSet(const GeoSet& geoset,const CopyOp& copyop):
_colindex = geoset._colindex;
if (geoset._colors)
{
_colors = new Vec4 [_numcolors];
_colors = osgNew Vec4 [_numcolors];
memcpy(_colors,geoset._colors,_numcolors*sizeof(Vec4));
}
else
@@ -117,7 +117,7 @@ GeoSet::GeoSet(const GeoSet& geoset,const CopyOp& copyop):
_tindex = geoset._tindex;
if (geoset._tcoords)
{
_tcoords = new Vec2 [_numtcoords];
_tcoords = osgNew Vec2 [_numtcoords];
memcpy(_tcoords,geoset._tcoords,_numtcoords*sizeof(Vec2));
}
else

View File

@@ -192,27 +192,27 @@ Geode* osg::createGeodeForImage(osg::Image* image,const float s,const float t)
float x = y*(s/t);
// set up the texture.
osg::Texture* texture = new osg::Texture;
osg::Texture* texture = osgNew osg::Texture;
texture->setImage(image);
// set up the drawstate.
osg::StateSet* dstate = new osg::StateSet;
osg::StateSet* dstate = osgNew osg::StateSet;
dstate->setMode(GL_CULL_FACE,osg::StateAttribute::OFF);
dstate->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
dstate->setAttributeAndModes(texture,osg::StateAttribute::ON);
// set up the geoset.
osg::GeoSet* gset = new osg::GeoSet;
osg::GeoSet* gset = osgNew osg::GeoSet;
gset->setStateSet(dstate);
osg::Vec3* coords = new Vec3[4];
osg::Vec3* coords = osgNew Vec3[4];
coords[0].set(-x,0.0f,y);
coords[1].set(-x,0.0f,-y);
coords[2].set(x,0.0f,-y);
coords[3].set(x,0.0f,y);
gset->setCoords(coords);
osg::Vec2* tcoords = new Vec2[4];
osg::Vec2* tcoords = osgNew Vec2[4];
tcoords[0].set(0.0f,1.0f);
tcoords[1].set(0.0f,0.0f);
tcoords[2].set(1.0f,0.0f);
@@ -220,7 +220,7 @@ Geode* osg::createGeodeForImage(osg::Image* image,const float s,const float t)
gset->setTextureCoords(tcoords);
gset->setTextureBinding(osg::GeoSet::BIND_PERVERTEX);
osg::Vec4* colours = new Vec4[1];
osg::Vec4* colours = osgNew Vec4[1];
colours->set(1.0f,1.0f,1.0,1.0f);
gset->setColors(colours);
gset->setColorBinding(osg::GeoSet::BIND_OVERALL);
@@ -229,7 +229,7 @@ Geode* osg::createGeodeForImage(osg::Image* image,const float s,const float t)
gset->setPrimType(osg::GeoSet::QUADS);
// set up the geode.
osg::Geode* geode = new osg::Geode;
osg::Geode* geode = osgNew osg::Geode;
geode->addDrawable(gset);
return geode;

View File

@@ -138,10 +138,10 @@ void ImpostorSprite::setTexture(Texture* tex,int s,int t)
ImpostorSpriteManager::ImpostorSpriteManager()
{
_texenv = new TexEnv;
_texenv = osgNew TexEnv;
_texenv->setMode(TexEnv::REPLACE);
_alphafunc = new osg::AlphaFunc;
_alphafunc = osgNew osg::AlphaFunc;
_alphafunc->setFunction( AlphaFunc::GREATER, 0.000f );
_first = NULL;
@@ -248,24 +248,24 @@ ImpostorSprite* ImpostorSpriteManager::createOrReuseImpostorSprite(int s,int t,i
// creating new impostor sprite.
StateSet* stateset = new StateSet;
StateSet* stateset = osgNew StateSet;
stateset->setMode(GL_CULL_FACE,osg::StateAttribute::OFF);
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
Texture* texture = new Texture;
Texture* texture = osgNew Texture;
stateset->setAttributeAndModes(texture,StateAttribute::ON);
stateset->setAttributeAndModes( _alphafunc.get(), StateAttribute::ON );
stateset->setAttribute(_texenv.get());
/*
TexEnv* texenv = new TexEnv;
TexEnv* texenv = osgNew TexEnv;
texenv->setMode(TexEnv::REPLACE);
stateset->setAttribute(texenv);
AlphaFunc* alphafunc = new osg::AlphaFunc;
AlphaFunc* alphafunc = osgNew osg::AlphaFunc;
alphafunc->setFunction( AlphaFunc::GREATER, 0.000f );
stateset->setAttributeAndModes( alphafunc, StateAttribute::ON );
*/
@@ -273,7 +273,7 @@ ImpostorSprite* ImpostorSpriteManager::createOrReuseImpostorSprite(int s,int t,i
// stateset->setMode( GL_ALPHA_TEST, StateAttribute::OFF );
ImpostorSprite* is = new ImpostorSprite;
ImpostorSprite* is = osgNew ImpostorSprite;
is->setStateSet(stateset);
is->setTexture(texture,s,t);

View File

@@ -198,6 +198,32 @@ static bool staticDeinitTime = false;
static sAllocUnit **reservoirBuffer = NULL;
static unsigned int reservoirBufferSize = 0;
// ---------------------------------------------------------------------------------------------------------------------------------
// We use a static class to let us know when we're in the midst of static deinitialization
// ---------------------------------------------------------------------------------------------------------------------------------
static void dumpLeakReport();
static void doCleanupLogOnFirstRun();
#ifdef OSG_USE_MEMORY_MANAGER
class MemStaticTimeTracker
{
public:
MemStaticTimeTracker()
{
doCleanupLogOnFirstRun();
}
~MemStaticTimeTracker()
{
staticDeinitTime = true;
dumpLeakReport();
}
};
static MemStaticTimeTracker mstt;
#endif
// ---------------------------------------------------------------------------------------------------------------------------------
// Local functions only
// ---------------------------------------------------------------------------------------------------------------------------------
@@ -459,6 +485,7 @@ static void dumpLeakReport()
// Open the report file
FILE *fp = fopen("memleaks.log", "w+b");
//FILE *fp = stderr;
// If you hit this assert, then the memory report generator is unable to log information to a file (can't open the file for
// some reason.)
@@ -510,21 +537,6 @@ static void dumpLeakReport()
fclose(fp);
}
// ---------------------------------------------------------------------------------------------------------------------------------
// We use a static class to let us know when we're in the midst of static deinitialization
// ---------------------------------------------------------------------------------------------------------------------------------
#ifdef OSG_USE_MEMORY_TRACKING
class MemStaticTimeTracker
{
public:
MemStaticTimeTracker() {doCleanupLogOnFirstRun();}
~MemStaticTimeTracker() {staticDeinitTime = true; dumpLeakReport();}
};
static MemStaticTimeTracker mstt;
#endif
// ---------------------------------------------------------------------------------------------------------------------------------
// -DOC- Flags & options -- Call these routines to enable/disable the following options
@@ -629,9 +641,9 @@ void m_setOwner(const char *file, const unsigned int line, const char *func)
// memory tracking routines.
// ---------------------------------------------------------------------------------------------------------------------------------
#ifdef OSG_USE_MEMORY_TRACKING
#ifdef OSG_USE_MEMORY_MANAGER
void *operator new(size_t reportedSize)
void *operator new(size_t reportedSize) throw (std::bad_alloc)
{
#ifdef TEST_MEMORY_MANAGER
log("ENTER: new");
@@ -683,7 +695,7 @@ void *operator new(size_t reportedSize)
// ---------------------------------------------------------------------------------------------------------------------------------
void *operator new[](size_t reportedSize)
void *operator new[](size_t reportedSize) throw (std::bad_alloc)
{
#ifdef TEST_MEMORY_MANAGER
log("ENTER: new[]");
@@ -741,7 +753,7 @@ void *operator new[](size_t reportedSize)
// our memory tracking routines.
// ---------------------------------------------------------------------------------------------------------------------------------
void *operator new(size_t reportedSize, const char *sourceFile, int sourceLine)
void *operator new(size_t reportedSize, const char *sourceFile, int sourceLine) throw (std::bad_alloc)
{
#ifdef TEST_MEMORY_MANAGER
log("ENTER: new");
@@ -793,7 +805,7 @@ void *operator new(size_t reportedSize, const char *sourceFile, int sourceLine)
// ---------------------------------------------------------------------------------------------------------------------------------
void *operator new[](size_t reportedSize, const char *sourceFile, int sourceLine)
void *operator new[](size_t reportedSize, const char *sourceFile, int sourceLine) throw (std::bad_alloc)
{
#ifdef TEST_MEMORY_MANAGER
log("ENTER: new[]");
@@ -850,7 +862,7 @@ void *operator new[](size_t reportedSize, const char *sourceFile, int sourceLine
// but use our memory tracking routines.
// ---------------------------------------------------------------------------------------------------------------------------------
void operator delete(void *reportedAddress)
void operator delete(void *reportedAddress) throw ()
{
#ifdef TEST_MEMORY_MANAGER
log("ENTER: delete");
@@ -869,7 +881,7 @@ void operator delete(void *reportedAddress)
// ---------------------------------------------------------------------------------------------------------------------------------
void operator delete[](void *reportedAddress)
void operator delete[](void *reportedAddress) throw ()
{
#ifdef TEST_MEMORY_MANAGER
log("ENTER: delete[]");
@@ -1599,3 +1611,4 @@ sMStats m_getMemoryStatistics()
// ---------------------------------------------------------------------------------------------------------------------------------
// mmgr.cpp - End of file
// ---------------------------------------------------------------------------------------------------------------------------------

View File

@@ -4,7 +4,7 @@
using namespace std;
osg::NotifySeverity osg::g_NotifyLevel = osg::NOTICE;
std::ofstream *osg::g_NotifyNulStream;
std::auto_ptr<ofstream> osg::g_NotifyNulStream;
bool osg::g_NotifyInit = false;
void osg::setNotifyLevel(osg::NotifySeverity severity)
@@ -29,9 +29,9 @@ bool osg::initNotifyLevel()
// set up global notify null stream for inline notify
#if defined(WIN32) && !defined(__CYGWIN__)
g_NotifyNulStream = new std::ofstream ("nul");
g_NotifyNulStream.reset(osgNew std::ofstream ("nul"));
#else
g_NotifyNulStream = new std::ofstream ("/dev/null");
g_NotifyNulStream.reset(osgNew std::ofstream ("/dev/null"));
#endif
// g_NotifyLevel

View File

@@ -130,10 +130,10 @@ void StateSet::setGlobalDefaults()
setRendingBinToInherit();
setAttributeAndModes(new AlphaFunc,StateAttribute::OFF);
setAttributeAndModes(new Transparency,StateAttribute::OFF);
setAttributeAndModes(osgNew AlphaFunc,StateAttribute::OFF);
setAttributeAndModes(osgNew Transparency,StateAttribute::OFF);
Material *material = new Material;
Material *material = osgNew Material;
material->setColorMode(Material::AMBIENT_AND_DIFFUSE);
setAttributeAndModes(material,StateAttribute::ON);
/*
@@ -148,17 +148,17 @@ void StateSet::setGlobalDefaults()
setMode(GL_TEXTURE_GEN_R,StateAttribute::OFF);
setMode(GL_TEXTURE_GEN_Q,StateAttribute::OFF);
setAttributeAndModes(new AlphaFunc,StateAttribute::OFF);
setAttributeAndModes(new CullFace,StateAttribute::ON);
setAttributeAndModes(new FrontFace,StateAttribute::ON);
setAttributeAndModes(osgNew AlphaFunc,StateAttribute::OFF);
setAttributeAndModes(osgNew CullFace,StateAttribute::ON);
setAttributeAndModes(osgNew FrontFace,StateAttribute::ON);
Material *material = new Material;
Material *material = osgNew Material;
material->setColorMode(Material::AMBIENT_AND_DIFFUSE);
setAttributeAndModes(material,StateAttribute::ON);
setAttributeAndModes(new PolygonMode,StateAttribute::OFF);
setAttributeAndModes(new Transparency,StateAttribute::OFF);
setAttributeAndModes(new Depth,StateAttribute::ON);
setAttributeAndModes(osgNew PolygonMode,StateAttribute::OFF);
setAttributeAndModes(osgNew Transparency,StateAttribute::OFF);
setAttributeAndModes(osgNew Depth,StateAttribute::ON);
*/
}

View File

@@ -7,8 +7,8 @@ Transform::Transform()
_type = DYNAMIC;
_mode = MODEL;
_matrix = new Matrix;
_inverse = new Matrix;
_matrix = osgNew Matrix;
_inverse = osgNew Matrix;
_inverseDirty = false;
}
@@ -17,8 +17,8 @@ Transform::Transform(const Transform& transform,const CopyOp& copyop):
_type(transform._type),
_mode(transform._mode),
_computeTransformCallback(_computeTransformCallback),
_matrix(new Matrix(*transform._matrix)),
_inverse(new Matrix(*transform._inverse)),
_matrix(osgNew Matrix(*transform._matrix)),
_inverse(osgNew Matrix(*transform._inverse)),
_inverseDirty(transform._inverseDirty)
{
}
@@ -28,8 +28,8 @@ Transform::Transform(const Matrix& mat )
_type = DYNAMIC;
_mode = MODEL;
_matrix = new Matrix(mat);
_inverse = new Matrix();
_matrix = osgNew Matrix(mat);
_inverse = osgNew Matrix();
_inverseDirty = false;
}