From 14448d70d1d48a50bf2e0b29e2de2c92eac5b30c Mon Sep 17 00:00:00 2001 From: tmj-fstate Date: Sat, 16 Jun 2018 15:59:28 +0200 Subject: [PATCH] maintenance: memory leak fix, default constructors, minor syntax cleanups --- Driver.cpp | 12 ++++++------ Event.cpp | 41 +++++++++++++++-------------------------- Event.h | 2 ++ McZapkie/Mover.cpp | 32 ++++++++++++++++++-------------- Model3d.h | 1 + Timer.h | 4 ++++ Track.cpp | 2 +- Traction.cpp | 4 ++-- audiorenderer.h | 5 ++++- frustum.h | 2 +- openglgeometrybank.h | 2 ++ renderer.h | 6 ++++-- scene.cpp | 1 - scene.h | 4 ++++ sky.h | 8 +++++--- stars.h | 2 +- uilayer.h | 4 ++-- uitranscripts.h | 3 ++- 18 files changed, 74 insertions(+), 61 deletions(-) diff --git a/Driver.cpp b/Driver.cpp index dac6baf9..685647d0 100644 --- a/Driver.cpp +++ b/Driver.cpp @@ -521,7 +521,7 @@ void TController::TableTraceRoute(double fDistance, TDynamicObject *pVehicle) pTrack = lastspeedpoint.trTrack; assert( pTrack != nullptr ); // flaga ustawiona, gdy Point2 toru jest blizej - fLastDir = lastspeedpoint.iFlags & spReverse ? -1.0 : 1.0; + fLastDir = ( ( ( lastspeedpoint.iFlags & spReverse ) != 0 ) ? -1.0 : 1.0 ); fCurrentDistance = lastspeedpoint.fDist; // aktualna odleglosc do jego Point1 fTrackLength = pTrack->Length(); } @@ -1286,7 +1286,7 @@ TCommandType TController::TableUpdate(double &fVelDes, double &fDist, double &fN && ( true == TestFlag( sSpeedTable[ i ].iFlags, ( spEnabled | spEvent | spPassengerStopPoint ) ) ) && ( false == isatpassengerstop ) ) { // ma podjechać bliżej - czy na pewno w tym miejscu taki warunek? - a = ( iDrivigFlags & moveStopCloser ? + a = ( ( ( iDrivigFlags & moveStopCloser ) != 0 ) ? fAcc : 0.0 ); } @@ -4575,14 +4575,14 @@ TController::UpdateSituation(double dt) { else { // jeśli oba jadą, to przyhamuj lekko i ogranicz prędkość if( vehicle->fTrackBlock < ( - mvOccupied->CategoryFlag & 2 ? + ( mvOccupied->CategoryFlag & 2 ) ? fMaxProximityDist + 0.5 * vel : // cars 2.0 * fMaxProximityDist + 2.0 * vel ) ) { //others // jak tamten jedzie wolniej a jest w drodze hamowania AccPreferred = std::min( -0.9, AccPreferred ); VelNext = min_speed( std::round( k ) - 5.0, VelDesired ); if( vehicle->fTrackBlock <= ( - mvOccupied->CategoryFlag & 2 ? + ( mvOccupied->CategoryFlag & 2 ) ? fMaxProximityDist : // cars 2.0 * fMaxProximityDist ) ) { //others // try to force speed change if obstacle is really close @@ -4826,7 +4826,7 @@ TController::UpdateSituation(double dt) { else { // jeśli daleko jechać nie można if( ActualProximityDist > ( - mvOccupied->CategoryFlag & 2 ? + ( mvOccupied->CategoryFlag & 2 ) ? fMinProximityDist : // cars fMaxProximityDist ) ) { // trains and others // ale ma kawałek do sygnalizatora @@ -5134,7 +5134,7 @@ TController::UpdateSituation(double dt) { VelDesired - fVelMinus ) ) { // ...jeśli prędkość w kierunku czoła jest mniejsza od dozwolonej o margines if( ( ActualProximityDist > ( - mvOccupied->CategoryFlag & 2 ? + ( mvOccupied->CategoryFlag & 2 ) ? fMinProximityDist : // cars are allowed to move within min proximity distance fMaxProximityDist ) ? // other vehicle types keep wider margin true : diff --git a/Event.cpp b/Event.cpp index 7721cd92..eb9053a6 100644 --- a/Event.cpp +++ b/Event.cpp @@ -39,7 +39,6 @@ TEvent::~TEvent() { switch (Type) { // sprzątanie case tp_Multiple: - // SafeDeleteArray(Params[9].asText); //nie usuwać - nazwa obiektu powiązanego zamieniana na wskaźnik if (iFlags & conditional_memstring) // o ile jest łańcuch do porównania w memcompare SafeDeleteArray(Params[10].asText); break; @@ -71,12 +70,6 @@ void TEvent::Conditions(cParser *parser, std::string s) if (s == "condition") { // jesli nie "endevent" std::string token, str; - if (!asNodeName.empty()) - { // podczepienie łańcucha, jeśli nie jest pusty - // BUG: source of a memory leak -- the array never gets deleted. fix the destructor - Params[9].asText = new char[asNodeName.size() + 1]; // usuwane i zamieniane na wskaźnik - strcpy(Params[9].asText, asNodeName.c_str()); - } parser->getTokens(); *parser >> token; str = token; @@ -1673,26 +1666,22 @@ event_manager::InitEvents() { break; } case tp_Multiple: { - std::string cellastext; - if( event->Params[ 9 ].asText != nullptr ) { // przepisanie nazwy do bufora - cellastext = event->Params[ 9 ].asText; - SafeDeleteArray( event->Params[ 9 ].asText ); - event->Params[ 9 ].asPointer = nullptr; // zerowanie wskaźnika, aby wykryć brak obeiktu - } - if( event->iFlags & ( conditional_trackoccupied | conditional_trackfree ) ) { - // jeśli chodzi o zajetosc toru - event->Params[ 9 ].asTrack = simulation::Paths.find( cellastext ); - if( event->Params[ 9 ].asTrack == nullptr ) { - ErrorLog( "Bad event: multi-event \"" + event->asName + "\" cannot find track \"" + cellastext + "\"" ); - event->iFlags &= ~( conditional_trackoccupied | conditional_trackfree ); // zerowanie flag + if( false == event->asNodeName.empty() ) { + if( event->iFlags & ( conditional_trackoccupied | conditional_trackfree ) ) { + // jeśli chodzi o zajetosc toru + event->Params[ 9 ].asTrack = simulation::Paths.find( event->asNodeName ); + if( event->Params[ 9 ].asTrack == nullptr ) { + ErrorLog( "Bad event: multi-event \"" + event->asName + "\" cannot find track \"" + event->asNodeName + "\"" ); + event->iFlags &= ~( conditional_trackoccupied | conditional_trackfree ); // zerowanie flag + } } - } - else if( event->iFlags & ( conditional_memstring | conditional_memval1 | conditional_memval2 ) ) { - // jeśli chodzi o komorke pamieciową - event->Params[ 9 ].asMemCell = simulation::Memory.find( cellastext ); - if( event->Params[ 9 ].asMemCell == nullptr ) { - ErrorLog( "Bad event: multi-event \"" + event->asName + "\" cannot find memory cell \"" + cellastext + "\"" ); - event->iFlags &= ~( conditional_memstring | conditional_memval1 | conditional_memval2 ); + else if( event->iFlags & ( conditional_memstring | conditional_memval1 | conditional_memval2 ) ) { + // jeśli chodzi o komorke pamieciową + event->Params[ 9 ].asMemCell = simulation::Memory.find( event->asNodeName ); + if( event->Params[ 9 ].asMemCell == nullptr ) { + ErrorLog( "Bad event: multi-event \"" + event->asName + "\" cannot find memory cell \"" + event->asNodeName + "\"" ); + event->iFlags &= ~( conditional_memstring | conditional_memval1 | conditional_memval2 ); + } } } for( auto &childevent : event->m_children ) { diff --git a/Event.h b/Event.h index 3fcf87bc..0882f420 100644 --- a/Event.h +++ b/Event.h @@ -120,6 +120,8 @@ private: class event_manager { public: +// constructors + event_manager() = default; // destructor ~event_manager(); // methods diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index 56e2505e..4aa42181 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -295,10 +295,10 @@ TMoverParameters::TMoverParameters(double VelInitial, std::string TypeNameInit, int Cab) ://: T_MoverParameters(VelInitial, TypeNameInit, //NameInit, LoadInitial, LoadTypeInitial, Cab) TypeName( TypeNameInit ), +Name( NameInit ), ActiveCab( Cab ), -LoadType( LoadTypeInitial ), Load( LoadInitial ), -Name( NameInit ) +LoadType( LoadTypeInitial ) { WriteLog( "------------------------------------------------------"); @@ -1579,9 +1579,10 @@ void TMoverParameters::WaterHeaterCheck( double const Timestep ) { WaterHeater.is_active = false; } - WaterHeater.is_damaged |= ( - ( true == WaterHeater.is_active ) - && ( false == WaterPump.is_active ) ); + WaterHeater.is_damaged = ( + ( true == WaterHeater.is_damaged ) + || ( ( true == WaterHeater.is_active ) + && ( false == WaterPump.is_active ) ) ); } // fuel pump status update @@ -5988,16 +5989,19 @@ bool TMoverParameters::dizel_AutoGearCheck(void) if (MotorParam[ScndCtrlActualPos].AutoSwitch && (dizel_automaticgearstatus == 0)) // sprawdz czy zmienic biegi { - if ((Vel > MotorParam[ScndCtrlActualPos].mfi) && - (ScndCtrlActualPos < ScndCtrlPosNo)) - { - dizel_automaticgearstatus = 1; - OK = true; + if( Vel > MotorParam[ ScndCtrlActualPos ].mfi ) { + // shift up + if( ScndCtrlActualPos < ScndCtrlPosNo ) { + dizel_automaticgearstatus = 1; + OK = true; + } } - else if ((Vel < MotorParam[ScndCtrlActualPos].fi) && (ScndCtrlActualPos > 0)) - { - dizel_automaticgearstatus = -1; - OK = true; + else if( Vel < MotorParam[ ScndCtrlActualPos ].fi ) { + // shift down + if( ScndCtrlActualPos > 0 ) { + dizel_automaticgearstatus = -1; + OK = true; + } } } } diff --git a/Model3d.h b/Model3d.h index 73e18d8d..0b74e49a 100644 --- a/Model3d.h +++ b/Model3d.h @@ -154,6 +154,7 @@ public: static float fSquareDist; static TModel3d *pRoot; static std::string *pasText; // tekst dla wyświetlacza (!!!! do przemyślenia) + TSubModel() = default; ~TSubModel(); int Load(cParser &Parser, TModel3d *Model, /*int Pos,*/ bool dynamic); void ChildAdd(TSubModel *SubModel); diff --git a/Timer.h b/Timer.h index ffcb45e0..ffc15cb7 100644 --- a/Timer.h +++ b/Timer.h @@ -29,6 +29,9 @@ void UpdateTimers(bool pause); class stopwatch { public: +// constructors + stopwatch() = default; +// methods void start() { m_start = std::chrono::steady_clock::now(); } @@ -40,6 +43,7 @@ public: return m_accumulator / 20.f;} private: +// members std::chrono::time_point m_start { std::chrono::steady_clock::now() }; float m_accumulator { 1000.f / 30.f * 20.f }; // 20 last samples, initial 'neutral' rate of 30 fps }; diff --git a/Track.cpp b/Track.cpp index cc7b9ec9..c436e6c0 100644 --- a/Track.cpp +++ b/Track.cpp @@ -1189,7 +1189,7 @@ void TTrack::create_geometry( gfx::geometrybank_handle const &Bank ) { { szyna[ i ].texture.x, szyna[ i ].texture.y } }; - if( false == iTrapezoid ) { continue; } + if( iTrapezoid == 0 ) { continue; } // trapez albo przechyłki, to oddzielne punkty na końcu rpts1[ 12 + i ] = { diff --git a/Traction.cpp b/Traction.cpp index e993ff0e..a155e5a7 100644 --- a/Traction.cpp +++ b/Traction.cpp @@ -91,8 +91,6 @@ jawnie nazwę sekcji, ewentualnie nazwę zasilacza (zostanie zastąpiona wskazan sekcji z sąsiedniego przęsła). */ -TTraction::TTraction( scene::node_data const &Nodedata ) : basic_node( Nodedata ) {} - glm::dvec3 LoadPoint( cParser &Input ) { // pobranie współrzędnych punktu glm::dvec3 point; @@ -104,6 +102,8 @@ glm::dvec3 LoadPoint( cParser &Input ) { return point; } +TTraction::TTraction( scene::node_data const &Nodedata ) : basic_node( Nodedata ) {} + void TTraction::Load( cParser *parser, glm::dvec3 const &pOrigin ) { diff --git a/audiorenderer.h b/audiorenderer.h index 54bcc5e9..89b81784 100644 --- a/audiorenderer.h +++ b/audiorenderer.h @@ -53,7 +53,8 @@ struct openal_source { bool is_looping { false }; sound_properties properties; sync_state sync { sync_state::good }; - +// constructors + openal_source() = default; // methods template openal_source & @@ -102,6 +103,8 @@ class openal_renderer { friend class opengl_renderer; public: +// constructors + openal_renderer() = default; // destructor ~openal_renderer(); // methods diff --git a/frustum.h b/frustum.h index 96b456ce..87d2b7fe 100644 --- a/frustum.h +++ b/frustum.h @@ -24,7 +24,7 @@ class cFrustum { public: // constructors: - + cFrustum() = default; // methods: // update the frustum to match current view orientation void diff --git a/openglgeometrybank.h b/openglgeometrybank.h index dd937204..f832bc2f 100644 --- a/openglgeometrybank.h +++ b/openglgeometrybank.h @@ -267,6 +267,8 @@ typedef geometry_handle geometrybank_handle; class geometrybank_manager { public: +// constructors + geometrybank_manager() = default; // methods: // performs a resource sweep void update(); diff --git a/renderer.h b/renderer.h index 61fb1ab9..c0f4ef5f 100644 --- a/renderer.h +++ b/renderer.h @@ -52,6 +52,8 @@ struct opengl_technique { class opengl_camera { public: +// constructors + opengl_camera() = default; // methods: inline void @@ -113,10 +115,10 @@ class opengl_renderer { public: // types - +// constructors + opengl_renderer() = default; // destructor ~opengl_renderer() { gluDeleteQuadric( m_quadric ); } - // methods bool Init( GLFWwindow *Window ); diff --git a/scene.cpp b/scene.cpp index 59de3c15..46bcd022 100644 --- a/scene.cpp +++ b/scene.cpp @@ -124,7 +124,6 @@ basic_cell::update_events() { void basic_cell::update_sounds() { - auto const deltatime = Timer::GetDeltaRenderTime(); for( auto *sound : m_sounds ) { sound->play_event(); } diff --git a/scene.h b/scene.h index 8a435ef1..9ec4a714 100644 --- a/scene.h +++ b/scene.h @@ -63,6 +63,8 @@ class basic_cell { friend class opengl_renderer; public: +// constructors + basic_cell() = default; // methods // legacy method, finds and assigns traction piece to specified pantograph of provided vehicle void @@ -183,6 +185,8 @@ class basic_section { friend class opengl_renderer; public: +// constructors + basic_section() = default; // methods // legacy method, finds and assigns traction piece to specified pantograph of provided vehicle void diff --git a/sky.h b/sky.h index a2e8b76f..e583ff97 100644 --- a/sky.h +++ b/sky.h @@ -16,11 +16,13 @@ class TSky { friend class opengl_renderer; +public: + TSky() = default; + + void Init(); + private: TModel3d *mdCloud { nullptr }; - -public: - void Init(); }; //--------------------------------------------------------------------------- diff --git a/stars.h b/stars.h index 5619a0c1..0de93ea6 100644 --- a/stars.h +++ b/stars.h @@ -16,7 +16,7 @@ public: // methods: void init(); // constructors: - + cStars() = default; // deconstructor: // members: diff --git a/uilayer.h b/uilayer.h index 4b25d88c..7e27aab8 100644 --- a/uilayer.h +++ b/uilayer.h @@ -10,8 +10,8 @@ struct ui_panel { struct text_line { - glm::vec4 color; std::string data; + glm::vec4 color; text_line( std::string const &Data, glm::vec4 const &Color): data(Data), color(Color) @@ -33,7 +33,7 @@ public: // parameters: // constructors: - + ui_layer() = default; // destructor: ~ui_layer(); diff --git a/uitranscripts.h b/uitranscripts.h index 56c27f08..7db201a1 100644 --- a/uitranscripts.h +++ b/uitranscripts.h @@ -19,13 +19,14 @@ struct TTranscript { class TTranscripts { public: +// constructors + TTranscripts() = default; // methods void AddLine( std::string const &txt, float show, float hide, bool it ); // dodanie tekstów, długość dźwięku, czy istotne void Add( std::string const &txt, bool background = false ); // usuwanie niepotrzebnych (ok. 10 razy na sekundę) void Update(); - // members std::deque aLines;