kjs335
06-12-2006 09:14:34
Hi there
I added MaterialID and Pairs to my game today.
When the program exits, the materials are destroyed using
delete ....;
But they made memory leaks in exit.
How do you make the memory leaks disappear?
abecam
06-12-2006 13:12:46
Can you give more details, how you create/delete the material, the result in the memory leak report? So it can help to find where the problem lies!
kjs335
08-12-2006 07:14:27
Variables with Mat at the start are MaterialID's
Variables with Mat at the end are MaterialPair's
Creating Materials
// Set MaterialIDs/Pairs
MatPlayerMoving = new OgreNewt::MaterialID( m_World );
MatPlayerStill = new OgreNewt::MaterialID( m_World );
MatProjectile = new OgreNewt::MaterialID( m_World );
OgreNewt::MaterialID *MatDefault = const_cast<OgreNewt::MaterialID*>(m_World->getDefaultMaterialID());
DefaultMat = new OgreNewt::MaterialPair(m_World, MatDefault, MatDefault);
DefaultMat->setDefaultElasticity(0);
DefaultMat->setDefaultFriction(0,0);
StillPlayerMat = new OgreNewt::MaterialPair( m_World, MatDefault, MatPlayerStill );
StillPlayerMat->setDefaultElasticity(0.0);
StillPlayerMat->setDefaultFriction(1, 1);
MovingPlayerMat = new OgreNewt::MaterialPair( m_World, MatDefault, MatPlayerMoving );
MovingPlayerMat->setDefaultElasticity(0);
MovingPlayerMat->setDefaultFriction(0,0);
Destroying materials
if(DefaultMat){
delete MovingPlayerMat;
MovingPlayerMat = 0;
}
if(MovingPlayerMat){
delete MovingPlayerMat;
MovingPlayerMat = 0;
}
if(StillPlayerMat){
delete MovingPlayerMat;
MovingPlayerMat = 0;
}
if(MatPlayerMoving){
delete MatPlayerMoving;
MatPlayerMoving = 0;
}
if(MatPlayerStill){
delete MatPlayerStill;
MatPlayerStill = 0;
}
if(MatProjectile){
delete MovingPlayerMat;
MovingPlayerMat = 0;
}
Memory leak log
016236 0x048587C0 0x00000008 0x048587B0 0x00000028 0x00000000 new N N playstate.cpp(80) PlayState::enter
016237 0x04858828 0x0000000C 0x04858818 0x0000002C 0x00000000 new N N playstate.cpp(84) PlayState::enter
016238 0x04858890 0x0000000C 0x04858880 0x0000002C 0x00000000 new N N playstate.cpp(89) PlayState::enter
And I'm quite certain this causes the leak because I had no memory leak until I added this code.
stoneCold.net
08-12-2006 07:37:18
Have you already tried to debug step-by-step through the "if" braces and see if every object is really deleted?
greetings
abecam
12-12-2006 13:45:27
There is also a tricky behavior with things like if(DefaultMat)
It works only if you declare DefaultMat=NULL; (then I am paranoid enough to only use if (PointerToTest != NULL), not if (PointerToTest)

).
But here are your leaks:
if(DefaultMat){
delete MovingPlayerMat;
MovingPlayerMat = 0;
}
if(StillPlayerMat){
delete MovingPlayerMat;
MovingPlayerMat = 0;
}
and here:
f(MatProjectile){
delete MovingPlayerMat;
MovingPlayerMat = 0;
}