From 2e21435f432191afa1b0f609daa8ca9d4cde1e93 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sun, 16 Sep 2012 20:00:25 +0200 Subject: [PATCH] Some memory leaks fixed --- src/app/app.cpp | 1 + src/graphics/engine/text.cpp | 14 +++++---- src/object/robotmain.cpp | 32 +++++++++++++++++---- src/ui/edit.cpp | 56 +++++++++++++++++++++++++----------- src/ui/interface.cpp | 1 + src/ui/maindialog.cpp | 2 +- 6 files changed, 77 insertions(+), 29 deletions(-) diff --git a/src/app/app.cpp b/src/app/app.cpp index 1bf9a54f..610cdbbb 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -224,6 +224,7 @@ bool CApplication::ParseArguments(int argc, char *argv[]) GetLogger()->Message(" -debug enable debug mode (more info printed in logs)\n"); GetLogger()->Message(" -loglevel level set log level to level (one of: trace, debug, info, warn, error, none)\n"); GetLogger()->Message(" -language lang set language (one of: en, de, fr, pl)\n"); + return true; } else { diff --git a/src/graphics/engine/text.cpp b/src/graphics/engine/text.cpp index 19ef57be..8fc87091 100644 --- a/src/graphics/engine/text.cpp +++ b/src/graphics/engine/text.cpp @@ -785,18 +785,20 @@ Gfx::CharTexture Gfx::CText::CreateCharTexture(Gfx::UTF8Char ch, Gfx::CachedFont data.surface = nullptr; - SDL_FreeSurface(textSurface); - SDL_FreeSurface(textureSurface); - if (! tex.Valid()) { m_error = "Texture create error"; return texture; } + else + { + texture.id = tex.id; + texture.texSize = m_engine->WindowToInterfaceSize(Math::IntPoint(textureSurface->w, textureSurface->h)); + texture.charSize = m_engine->WindowToInterfaceSize(Math::IntPoint(textSurface->w, textSurface->h)); + } - texture.id = tex.id; - texture.texSize = m_engine->WindowToInterfaceSize(Math::IntPoint(textureSurface->w, textureSurface->h)); - texture.charSize = m_engine->WindowToInterfaceSize(Math::IntPoint(textSurface->w, textSurface->h)); + SDL_FreeSurface(textSurface); + SDL_FreeSurface(textureSurface); return texture; } diff --git a/src/object/robotmain.cpp b/src/object/robotmain.cpp index 1c615749..b6aedaab 100644 --- a/src/object/robotmain.cpp +++ b/src/object/robotmain.cpp @@ -642,6 +642,7 @@ CRobotMain::CRobotMain(CInstanceManager* iMan, CApplication* app) m_selectObject = 0; m_infoUsed = 0; + m_immediatSatCom = false; m_beginSatCom = false; m_movieLock = false; m_satComLock = false; @@ -815,11 +816,32 @@ CRobotMain::CRobotMain(CInstanceManager* iMan, CApplication* app) //! Destructor of robot application CRobotMain::~CRobotMain() { - delete m_movie; - delete m_dialog; - delete m_short; - delete m_map; + delete m_displayText; + m_displayText = nullptr; + + delete m_interface; + m_interface = nullptr; + delete m_terrain; + m_terrain = nullptr; + + delete m_camera; + m_camera = nullptr; + + delete m_displayText; + m_displayText = nullptr; + + delete m_movie; + m_movie = nullptr; + + delete m_dialog; + m_dialog = nullptr; + + delete m_short; + m_short = nullptr; + + delete m_map; + m_map = nullptr; m_iMan = nullptr; m_app = nullptr; @@ -2707,7 +2729,7 @@ void CRobotMain::HiliteObject(Math::Point pos) if (obj == nullptr) { - bool inMap; + bool inMap = false; obj = m_map->DetectMap(pos, inMap); if (obj == nullptr) { diff --git a/src/ui/edit.cpp b/src/ui/edit.cpp index cde3aee3..a4d59611 100644 --- a/src/ui/edit.cpp +++ b/src/ui/edit.cpp @@ -79,10 +79,13 @@ CEdit::CEdit () : CControl () int i; m_maxChar = 100; - m_text = new char[sizeof(char)*(m_maxChar+1)]; + m_text = new char[m_maxChar+1]; + memset(m_text, 0, m_maxChar+1); m_len = 0; m_app = CApplication::GetInstancePointer(); + memset(m_lineOffset, 0, sizeof(int) * EDITLINEMAX); + m_fontType = Gfx::FONT_COURIER; m_scroll = 0; m_bEdit = true; @@ -102,7 +105,7 @@ CEdit::CEdit () : CControl () for ( i=0 ; i 0 ) @@ -1934,15 +1951,18 @@ bool CEdit::WriteText(const char *filename) void CEdit::SetMaxChar(int max) { - m_maxChar = max; FreeImage(); - delete m_text; - m_text = (char*)malloc(sizeof(char)*(m_maxChar+1)); - if ( m_format.size() > 0 ) - { + if (m_text != nullptr) + delete[] m_text; + + m_maxChar = max; + + m_text = new char[m_maxChar+1]; + memset(m_text, 0, m_maxChar+1); + + if (m_format.size() > 0) m_format.clear(); - } m_len = 0; m_cursor1 = 0; @@ -3062,8 +3082,9 @@ void CEdit::Justif() if ( m_format.size() == 0 ) { // TODO check if good + i += m_engine->GetText()->Justify(m_text+i, m_fontType, - m_fontSize, width); + m_fontSize, width); } else { @@ -3208,7 +3229,7 @@ void CEdit::UndoFlush() for ( i=0 ; i=1 ; i-- ) { @@ -3238,7 +3260,7 @@ void CEdit::UndoMemorize(OperUndo oper) len = m_len; if ( len == 0 ) len ++; - m_undo[0].text = (char*)malloc(sizeof(char)*(len+1)); + m_undo[0].text = new char[len+1]; memcpy(m_undo[0].text, m_text, m_len); m_undo[0].len = m_len; @@ -3253,7 +3275,7 @@ bool CEdit::UndoRecall() { int i; - if ( m_undo[0].text == 0 ) return false; + if ( m_undo[0].text == nullptr ) return false; m_len = m_undo[0].len; memcpy(m_text, m_undo[0].text, m_len); @@ -3266,7 +3288,7 @@ bool CEdit::UndoRecall() { m_undo[i] = m_undo[i+1]; } - m_undo[EDITUNDOMAX-1].text = 0; + m_undo[EDITUNDOMAX-1].text = nullptr; m_bUndoForce = true; Justif(); diff --git a/src/ui/interface.cpp b/src/ui/interface.cpp index a0428993..1c624565 100644 --- a/src/ui/interface.cpp +++ b/src/ui/interface.cpp @@ -29,6 +29,7 @@ CInterface::CInterface() m_iMan->AddInstance(CLASS_INTERFACE, this); m_event = static_cast( m_iMan->SearchInstance(CLASS_EVENT) ); m_engine = static_cast( m_iMan->SearchInstance(CLASS_ENGINE) ); + m_camera = nullptr; for (int i = 0; i < MAXCONTROL; i++ ) { diff --git a/src/ui/maindialog.cpp b/src/ui/maindialog.cpp index 5ed1216f..4523b2ce 100644 --- a/src/ui/maindialog.cpp +++ b/src/ui/maindialog.cpp @@ -56,7 +56,7 @@ const int KEY_TOTAL = 13; // total number of keys redefinable #else*/ const int KEY_TOTAL = 21; // total number of keys redefinable -const int WELCOME_LENGTH = 6.0f; +const int WELCOME_LENGTH = 2.0f;