Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Codechange] Fixes various memory leaks #913

Merged
merged 2 commits into from
May 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion source/main/MainThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,9 @@ void MainThread::Go()
delete gEnv->frameListener;
gEnv->frameListener = nullptr;

delete gEnv->cameraManager;
gEnv->cameraManager = nullptr;

delete gEnv;
gEnv = nullptr;

Expand Down Expand Up @@ -638,7 +641,7 @@ bool MainThread::SetupGameplayLoop(bool enable_network, Ogre::String preselected
if (!m_base_resource_loaded)
{
// init camera manager after mygui and after we have a character
new CameraManager(gEnv->frameListener->m_dof);
gEnv->cameraManager = new CameraManager(gEnv->frameListener->m_dof);
}

// ============================================================================
Expand Down
6 changes: 6 additions & 0 deletions source/main/gfx/camera/CameraBehaviorVehicleSpline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ CameraBehaviorVehicleSpline::CameraBehaviorVehicleSpline() :
{
}

CameraBehaviorVehicleSpline::~CameraBehaviorVehicleSpline()
{
if (spline) delete spline;
if (splineObject) delete splineObject;
}

void CameraBehaviorVehicleSpline::update(const CameraManager::CameraContext &ctx)
{
if ( ctx.mCurrTruck->free_camerarail <= 0 )
Expand Down
1 change: 1 addition & 0 deletions source/main/gfx/camera/CameraBehaviorVehicleSpline.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class CameraBehaviorVehicleSpline : public CameraBehaviorVehicle
public:

CameraBehaviorVehicleSpline();
~CameraBehaviorVehicleSpline();

void update(const CameraManager::CameraContext &ctx);

Expand Down
2 changes: 0 additions & 2 deletions source/main/gfx/camera/CameraManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ CameraManager::CameraManager(DOFManager *dof) :
, m_config_exit_vehicle_keep_fixedfreecam(false)
, mRotateSpeed(100.0f)
{
gEnv->cameraManager = this;

createGlobalBehaviors();

ctx.mCurrTruck = 0;
Expand Down
35 changes: 28 additions & 7 deletions source/main/physics/Beam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
#include "IHeightFinder.h"
#include "InputEngine.h"
#include "Language.h"
#include "MaterialReplacer.h"
#include "MeshObject.h"
#include "MovableText.h"
#include "Network.h"
Expand All @@ -77,6 +78,7 @@ along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
#include "Triangle.h"
#include "TurboJet.h"
#include "TurboProp.h"
#include "VideoCamera.h"
#include "Water.h"
#include "GUIManager.h"

Expand Down Expand Up @@ -125,11 +127,11 @@ Beam::~Beam()
// destruct and remove every tiny bit of stuff we created :-|
if (engine) delete engine; engine=0;
if (buoyance) delete buoyance; buoyance=0;
if (autopilot) delete autopilot;
if (fuseAirfoil) delete fuseAirfoil;
if (cabMesh) delete cabMesh;
if (materialFunctionMapper) delete materialFunctionMapper;
if (replay) delete replay;
if (autopilot) delete autopilot; autopilot=0;
if (fuseAirfoil) delete fuseAirfoil; fuseAirfoil=0;
if (cabMesh) delete cabMesh; cabMesh=0;
if (materialFunctionMapper) delete materialFunctionMapper; materialFunctionMapper=0;
if (replay) delete replay; replay=0;

// TODO: Make sure we catch everything here
// remove all scene nodes
Expand Down Expand Up @@ -161,7 +163,7 @@ Beam::~Beam()
for (int i=0; i<free_wing;i++)
{
// flexAirfoil, airfoil
if (wings[i].fa) delete wings[i].fa; wings[i].fa=0;
if (wings[i].fa) delete wings[i].fa;
if (wings[i].cnode)
{
wings[i].cnode->removeAndDestroyAllChildren();
Expand Down Expand Up @@ -198,7 +200,7 @@ Beam::~Beam()
for (int i=0; i<free_wheel;i++)
{

//if (vwheels[i].fm) delete vwheels[i].fm; // CRASH!
if (vwheels[i].fm) delete vwheels[i].fm;
if (vwheels[i].cnode)
{
vwheels[i].cnode->removeAndDestroyAllChildren();
Expand Down Expand Up @@ -314,6 +316,25 @@ Beam::~Beam()
delete netMT;
netMT = 0;
}

for (VideoCamera* v : vidcams)
{
delete v;
}

if (materialReplacer) delete materialReplacer;

if (intraPointCD) delete intraPointCD;
if (interPointCD) delete interPointCD;

if (cmdInertia) delete cmdInertia;
if (hydroInertia) delete hydroInertia;
if (rotaInertia) delete rotaInertia;

for (int i = 0; i < free_axle; ++i)
{
if (axles[i] != nullptr) delete (axles[i]);
}
}

// This method scales trucks. Stresses should *NOT* be scaled, they describe
Expand Down
13 changes: 8 additions & 5 deletions source/main/physics/air/AirBrake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,21 @@ Airbrake::Airbrake(char* basename, int num, node_t *ndref, node_t *ndx, node_t *
char entname[256];
sprintf(entname, "airbrakenode-%s-%i", basename, num);
ec = gEnv->sceneManager->createEntity(entname, meshname);
snode=gEnv->sceneManager->getRootSceneNode()->createChildSceneNode();
snode = gEnv->sceneManager->getRootSceneNode()->createChildSceneNode();
snode->attachObject(ec);

updatePosition(0.0);

free (vertices);
free (faces);
}

Airbrake::~Airbrake()
{
ec->setVisible(false);
delete ec;
snode->setVisible(false);
delete snode;
if (!msh.isNull()) msh->unload();

if (ec) ec->setVisible(false);
if (snode) snode->setVisible(false);
}

void Airbrake::updatePosition(float amount)
Expand Down
21 changes: 9 additions & 12 deletions source/main/physics/air/TurboJet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.

using namespace Ogre;

Turbojet::Turbojet(char* propname, int tnumber, int trucknum, node_t *nd, int tnodefront, int tnodeback, int tnoderef, float tmaxdrythrust, bool treversable, bool tafterburnable, float tafterburnthrust, float diskdiam, float nozdiam, float nozlength, bool disable_smoke, bool _heathaze, MaterialFunctionMapper *mfm, Skin *usedSkin, MaterialReplacer *mr) : mr(mr)
Turbojet::Turbojet(char* propname, int tnumber, int trucknum, node_t *nd, int tnodefront, int tnodeback, int tnoderef, float tmaxdrythrust, bool treversable, bool tafterburnable, float tafterburnthrust, float diskdiam, float nozdiam, float nozlength, bool disable_smoke, bool _heathaze, MaterialFunctionMapper *mfm, Skin *usedSkin, MaterialReplacer *mr)
{
heathaze=_heathaze;
nodes=nd;
Expand Down Expand Up @@ -133,18 +133,15 @@ Turbojet::~Turbojet()
SoundScriptManager::getSingleton().trigStop(trucknum, ab_id);
SoundScriptManager::getSingleton().trigStop(trucknum, src_id);

delete heathazePS;

delete smokePS;
delete smokeNode;

flameMesh->setVisible(false);
delete flameMesh;
delete absnode;
if (flameMesh != nullptr)
{
flameMesh->setVisible(false);
}

nozzleMesh->setVisible(false);
delete nozzleMesh;
delete nzsnode;
if (nozzleMesh != nullptr)
{
nozzleMesh->setVisible(false);
}
}

void Turbojet::updateVisuals()
Expand Down
1 change: 0 additions & 1 deletion source/main/physics/air/TurboJet.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ class Turbojet: public AeroEngine, public ZeroedMemoryAllocator

private:

MaterialReplacer *mr;
Ogre::ParticleSystem* heathazePS;
Ogre::ParticleSystem* smokePS;
Ogre::SceneNode *absnode;
Expand Down
15 changes: 5 additions & 10 deletions source/main/physics/air/TurboProp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,21 @@ Turboprop::~Turboprop()
SoundScriptManager::getSingleton().modulate(trucknum, mod_id, 0);
SoundScriptManager::getSingleton().trigStop(trucknum, src_id);

delete heathazePS;

delete smokePS;
delete smokeNode;
if (airfoil != nullptr) delete airfoil;
}

void Turboprop::updateVisuals()
{
//visuals
if (rpm>200)
{
int i;
for (i=0; i<free_vpale; i++) vpales[i]->setVisible(false);
vspinner->setVisible(true);
for (int i=0; i<free_vpale; i++) vpales[i]->setVisible(false);
if (vspinner) vspinner->setVisible(true);
}
else
{
int i;
for (i=0; i<free_vpale; i++) vpales[i]->setVisible(true);
vspinner->setVisible(false);
for (int i=0; i<free_vpale; i++) vpales[i]->setVisible(true);
if (vspinner) vspinner->setVisible(false);
}
//smoke
if (smokeNode)
Expand Down
15 changes: 11 additions & 4 deletions source/main/physics/flex/FlexAirfoil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ FlexAirfoil::FlexAirfoil(Ogre::String const & name, node_t *nds, int pnfld, int
/// Create submeshes
subface = msh->createSubMesh();
subband = msh->createSubMesh();
subcup=msh->createSubMesh();
subcdn=msh->createSubMesh();
subcup = msh->createSubMesh();
subcdn = msh->createSubMesh();

//materials
subface->setMaterialName(texband);
Expand Down Expand Up @@ -410,7 +410,6 @@ FlexAirfoil::FlexAirfoil(Ogre::String const & name, node_t *nds, int pnfld, int
//MeshManager::getSingleton().setPrepareAllMeshesForShadowVolumes()
}


Vector3 FlexAirfoil::updateVertices()
{
int i;
Expand Down Expand Up @@ -849,6 +848,14 @@ void FlexAirfoil::updateForces()

FlexAirfoil::~FlexAirfoil()
{
if (airfoil) delete airfoil; airfoil=0;
if (airfoil) delete airfoil;
if (!msh.isNull()) msh->unload();

if (vertices != nullptr) { free (vertices); }
if (shadownorvertices != nullptr) { free (shadownorvertices); }
if (shadowposvertices != nullptr) { free (shadowposvertices); }
if (facefaces != nullptr) { free (facefaces); }
if (bandfaces != nullptr) { free (bandfaces); }
if (cupfaces != nullptr) { free (cupfaces); }
if (cdnfaces != nullptr) { free (cdnfaces); }
}
10 changes: 10 additions & 0 deletions source/main/physics/flex/FlexMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,16 @@ FlexMesh::FlexMesh(
//msh->buildEdgeList();
}

FlexMesh::~FlexMesh()
{
if (vertices != nullptr) { free (vertices); }
if (shadownorvertices != nullptr) { free (shadownorvertices); }
if (shadowposvertices != nullptr) { free (shadowposvertices); }
if (nodeIDs != nullptr) { free (nodeIDs); }
if (facefaces != nullptr) { free (facefaces); }
if (bandfaces != nullptr) { free (bandfaces); }
}

Vector3 FlexMesh::updateVertices()
{
Vector3 center = (nodes[nodeIDs[0]].smoothpos + nodes[nodeIDs[1]].smoothpos) / 2.0;
Expand Down
2 changes: 2 additions & 0 deletions source/main/physics/flex/FlexMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class FlexMesh: public Flexable
float rimratio = 1.f
);

~FlexMesh ();

Ogre::Vector3 updateVertices();
Ogre::Vector3 updateShadowVertices();

Expand Down
8 changes: 8 additions & 0 deletions source/main/physics/flex/FlexMeshWheel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@ FlexMeshWheel::FlexMeshWheel(
//msh->buildEdgeList();
}

FlexMeshWheel::~FlexMeshWheel()
{
if (vertices != nullptr) { free (vertices); }
if (shadownorvertices != nullptr) { free (shadownorvertices); }
if (shadowposvertices != nullptr) { free (shadowposvertices); }
if (faces != nullptr) { free (faces); }
}

Vector3 FlexMeshWheel::updateVertices()
{
Vector3 center = (nodes[id0].smoothpos + nodes[id1].smoothpos) / 2.0;
Expand Down
2 changes: 2 additions & 0 deletions source/main/physics/flex/FlexMeshWheel.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class FlexMeshWheel: public Flexable
MaterialReplacer *material_replacer
);

~FlexMeshWheel();

Ogre::Entity *getRimEntity() { return rimEnt; };

Ogre::Vector3 updateVertices();
Expand Down
8 changes: 8 additions & 0 deletions source/main/physics/flex/FlexObj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,12 @@ Vector3 FlexObj::flexit()
FlexObj::~FlexObj()
{
if (!msh.isNull()) msh->unload();

if (subs != nullptr) { free (subs); }
if (vertices != nullptr) { free (vertices); }
if (shadownorvertices != nullptr) { free (shadownorvertices); }
if (shadowposvertices != nullptr) { free (shadowposvertices); }
if (nodeIDs != nullptr) { free (nodeIDs); }
if (faces != nullptr) { free (faces); }
if (sref != nullptr) { free (sref); }
}
1 change: 1 addition & 0 deletions source/main/physics/flex/Flexable.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
class Flexable
{
public:
virtual ~Flexable() {};

virtual bool flexitPrepare() = 0;
virtual void flexitCompute() = 0;
Expand Down
5 changes: 5 additions & 0 deletions source/main/terrain/TerrainObjectManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ TerrainObjectManager::TerrainObjectManager(TerrainManager *terrainManager) :

TerrainObjectManager::~TerrainObjectManager()
{
for (MeshObject* mo : meshObjects)
{
if (mo) delete mo;
}
#ifdef USE_PAGED
for (std::vector<paged_geometry_t>::iterator it=pagedGeometry.begin(); it!=pagedGeometry.end(); it++)
{
Expand Down Expand Up @@ -790,6 +794,7 @@ void TerrainObjectManager::loadObject(const Ogre::String &name, const Ogre::Vect
if (String(mesh) != "none")
{
mo = new MeshObject(mesh, entity_name, tenode, NULL, background_loading);
meshObjects.push_back(mo);
}

//mo->setQueryFlags(OBJECTS_MASK);
Expand Down
1 change: 1 addition & 0 deletions source/main/terrain/TerrainObjectManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class TerrainObjectManager : public IManager
int free_localizer;

std::vector<animated_object_t> animatedObjects;
std::vector<MeshObject*> meshObjects;

typedef struct loadedObject_t
{
Expand Down