From 3e4c1a1ad88456ebf201b257b91847bd995c8773 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Fri, 28 Dec 2012 13:37:08 +0100 Subject: Replaced malloc/free with new/delete - now new/delete used everywhere except for CBotStack, which has to be fixed in other way - some segfaults should be fixed with this --- src/CBot/CBotString.cpp | 54 +++++++++++++++++++++++--------------------- src/common/image.cpp | 4 ++-- src/object/brain.cpp | 27 ++++++++++++++-------- src/object/task/taskgoto.cpp | 4 ++-- src/script/script.cpp | 33 +++++++++++++++------------ 5 files changed, 69 insertions(+), 53 deletions(-) diff --git a/src/CBot/CBotString.cpp b/src/CBot/CBotString.cpp index 4795b63..b1b5fc4 100644 --- a/src/CBot/CBotString.cpp +++ b/src/CBot/CBotString.cpp @@ -127,7 +127,8 @@ CBotString::CBotString() CBotString::~CBotString() { - free(m_ptr); //we can call free on null pointer as it's save + delete[] m_ptr; + m_ptr = nullptr; } @@ -138,7 +139,7 @@ CBotString::CBotString(const char* p) m_ptr = NULL; if (m_lg>0) { - m_ptr = static_cast(malloc(m_lg+1)); + m_ptr = new char[m_lg+1]; strcpy(m_ptr, p); } } @@ -150,7 +151,7 @@ CBotString::CBotString(const CBotString& srcString) m_ptr = NULL; if (m_lg>0) { - m_ptr = static_cast(malloc(m_lg+1)); + m_ptr = new char[m_lg+1]; strcpy(m_ptr, srcString.m_ptr); } } @@ -285,12 +286,12 @@ CBotString CBotString::Mid(int start, int lg) if ( lg < 0 ) lg = m_lg - start; - char* p = static_cast(malloc(m_lg+1)); + char* p = new char[m_lg+1]; strcpy(p, m_ptr+start); p[lg] = 0; res = p; - free(p); + delete[] p; return res; } @@ -314,15 +315,16 @@ void CBotString::MakeLower() bool CBotString::LoadString(unsigned int id) { - const char * str = NULL; + const char * str = nullptr; str = MapIdToString(static_cast(id)); - if (m_ptr != NULL) free(m_ptr); + if (m_ptr != nullptr) + delete[] m_ptr; m_lg = strlen(str); m_ptr = NULL; if (m_lg > 0) { - m_ptr = static_cast(malloc(m_lg+1)); + m_ptr = new char[m_lg+1]; strcpy(m_ptr, str); return true; } @@ -332,14 +334,14 @@ bool CBotString::LoadString(unsigned int id) const CBotString& CBotString::operator=(const CBotString& stringSrc) { - free(m_ptr); - m_ptr = NULL; + delete[] m_ptr; + m_ptr = nullptr; m_lg = stringSrc.m_lg; if (m_lg > 0) { - m_ptr = static_cast(malloc(m_lg+1)); + m_ptr = new char[m_lg+1]; strcpy(m_ptr, stringSrc.m_ptr); } @@ -355,13 +357,13 @@ CBotString operator+(const CBotString& string, const char * lpsz) const CBotString& CBotString::operator+(const CBotString& stringSrc) { - char* p = static_cast(malloc(m_lg+stringSrc.m_lg+1)); + char* p = new char[m_lg+stringSrc.m_lg+1]; if (m_ptr!=NULL) strcpy(p, m_ptr); char* pp = p + m_lg; if (stringSrc.m_ptr!=NULL) strcpy(pp, stringSrc.m_ptr); - free(m_ptr); + delete[] m_ptr; m_ptr = p; m_lg += stringSrc.m_lg; @@ -370,11 +372,11 @@ const CBotString& CBotString::operator+(const CBotString& stringSrc) const CBotString& CBotString::operator=(const char ch) { - free(m_ptr); + delete[] m_ptr; m_lg = 1; - m_ptr = static_cast(malloc(2)); + m_ptr = new char[2]; m_ptr[0] = ch; m_ptr[1] = 0; @@ -383,16 +385,16 @@ const CBotString& CBotString::operator=(const char ch) const CBotString& CBotString::operator=(const char* pString) { - free(m_ptr); - m_ptr = NULL; + delete[] m_ptr; + m_ptr = nullptr; - if (pString != NULL) + if (pString != nullptr) { m_lg = strlen(pString); if (m_lg != 0) { - m_ptr = static_cast(malloc(m_lg+1)); + m_ptr = new char[m_lg+1]; strcpy(m_ptr, pString); } } @@ -403,13 +405,13 @@ const CBotString& CBotString::operator=(const char* pString) const CBotString& CBotString::operator+=(const char ch) { - char* p = static_cast(malloc(m_lg+2)); + char* p = new char[m_lg+2]; - if (m_ptr!=NULL) strcpy(p, m_ptr); + if (m_ptr != nullptr) strcpy(p, m_ptr); p[m_lg++] = ch; p[m_lg] = 0; - free(m_ptr); + delete[] m_ptr; m_ptr = p; @@ -418,7 +420,7 @@ const CBotString& CBotString::operator+=(const char ch) const CBotString& CBotString::operator+=(const CBotString& str) { - char* p = static_cast(malloc(m_lg+str.m_lg+1)); + char* p = new char[m_lg+str.m_lg+1]; strcpy(p, m_ptr); char* pp = p + m_lg; @@ -426,7 +428,7 @@ const CBotString& CBotString::operator+=(const CBotString& str) m_lg = m_lg + str.m_lg; - free(m_ptr); + delete[] m_ptr; m_ptr = p; @@ -500,8 +502,8 @@ bool CBotString::IsEmpty() const void CBotString::Empty() { - free(m_ptr); - m_ptr = NULL; + delete[] m_ptr; + m_ptr = nullptr; m_lg = 0; } diff --git a/src/common/image.cpp b/src/common/image.cpp index f3cfa34..ef8097e 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -124,14 +124,14 @@ bool PNGSaveSurface(const char *filename, SDL_Surface *surf) png_write_info(png_ptr, info_ptr); png_set_packing(png_ptr); - row_pointers = static_cast( malloc(sizeof(png_bytep)*surf->h) ); + row_pointers = new png_bytep[surf->h]; for (i = 0; i < surf->h; i++) row_pointers[i] = static_cast( static_cast(surf->pixels) ) + i*surf->pitch; png_write_image(png_ptr, row_pointers); png_write_end(png_ptr, info_ptr); /* Cleaning out... */ - free(row_pointers); + delete[] row_pointers; png_destroy_write_struct(&png_ptr, &info_ptr); fclose(fp); diff --git a/src/object/brain.cpp b/src/object/brain.cpp index 4ce1bf8..ef7309d 100644 --- a/src/object/brain.cpp +++ b/src/object/brain.cpp @@ -99,7 +99,7 @@ CBrain::CBrain(CInstanceManager* iMan, CObject* object) m_selScript = 0; m_bTraceRecord = false; - m_traceRecordBuffer = 0; + m_traceRecordBuffer = nullptr; } // Object's destructor. @@ -111,12 +111,21 @@ CBrain::~CBrain() for ( i=0 ; iDeleteInstance(CLASS_BRAIN, this); } @@ -2791,8 +2800,8 @@ void CBrain::TraceRecordStart() m_traceColor = -1; } - delete m_traceRecordBuffer; - m_traceRecordBuffer = static_cast(malloc(sizeof(TraceRecord)*MAXTRACERECORD)); + delete[] m_traceRecordBuffer; + m_traceRecordBuffer = new TraceRecord[MAXTRACERECORD]; m_traceRecordIndex = 0; } @@ -2858,10 +2867,10 @@ void CBrain::TraceRecordStop() int max, i; char* buffer; - if ( m_traceRecordBuffer == 0 ) return; + if ( m_traceRecordBuffer == nullptr ) return; max = 10000; - buffer = static_cast(malloc(max)); + buffer = new char[max]; *buffer = 0; strncat(buffer, "extern void object::AutoDraw()\n{\n", max-1); @@ -2892,8 +2901,8 @@ void CBrain::TraceRecordStop() } TraceRecordPut(buffer, max, lastOper, lastParam); - delete m_traceRecordBuffer; - m_traceRecordBuffer = 0; + delete[] m_traceRecordBuffer; + m_traceRecordBuffer = nullptr; strncat(buffer, "}\n", max-1); buffer[max-1] = 0; @@ -2904,7 +2913,7 @@ void CBrain::TraceRecordStop() m_script[i] = new CScript(m_iMan, m_object, &m_secondaryTask); } m_script[i]->SendScript(buffer); - delete buffer; + delete[] buffer; } // Saves an instruction CBOT. diff --git a/src/object/task/taskgoto.cpp b/src/object/task/taskgoto.cpp index ce778ef..cab57f1 100644 --- a/src/object/task/taskgoto.cpp +++ b/src/object/task/taskgoto.cpp @@ -2119,7 +2119,7 @@ bool CTaskGoto::BitmapOpen() BitmapClose(); m_bmSize = static_cast(3200.0f/BM_DIM_STEP); - m_bmArray = static_cast(malloc(m_bmSize*m_bmSize/8*2)); + m_bmArray = new unsigned char[m_bmSize*m_bmSize/8*2]; memset(m_bmArray, 0, m_bmSize*m_bmSize/8*2); m_bmOffset = m_bmSize/2; @@ -2137,7 +2137,7 @@ bool CTaskGoto::BitmapOpen() bool CTaskGoto::BitmapClose() { - free(m_bmArray); + delete[] m_bmArray; m_bmArray = 0; return true; } diff --git a/src/script/script.cpp b/src/script/script.cpp index 8471df5..57d638e 100644 --- a/src/script/script.cpp +++ b/src/script/script.cpp @@ -1571,7 +1571,7 @@ bool CScript::rGrab(CBotVar* var, CBotVar* result, int& exception, void* user) if ( script->m_primaryTask == 0 ) // no task in progress? { script->m_primaryTask = new CTaskManager(script->m_iMan, script->m_object); - if ( var == 0 ) + if ( var == 0 ) { type = TMA_FFRONT; } @@ -2753,9 +2753,14 @@ void CScript::InitFonctions() CScript::~CScript() { delete m_botProg; + m_botProg = nullptr; + delete m_primaryTask; - delete m_script; - m_script = 0; + m_primaryTask = nullptr; + + delete[] m_script; + m_script = nullptr; + m_len = 0; m_iMan->DeleteInstance(CLASS_SCRIPT, this); @@ -2766,7 +2771,7 @@ CScript::~CScript() void CScript::PutScript(Ui::CEdit* edit, const char* name) { - if ( m_script == 0 ) + if ( m_script == nullptr ) { New(edit, name); } @@ -2785,11 +2790,11 @@ bool CScript::GetScript(Ui::CEdit* edit) { int len; - delete m_script; - m_script = 0; + delete[] m_script; + m_script = nullptr; len = edit->GetTextLength(); - m_script = static_cast(malloc(sizeof(char)*(len+1))); + m_script = new char[len+1]; edit->GetText(m_script, len+1); edit->GetCursor(m_cursor2, m_cursor1); @@ -2997,7 +3002,7 @@ void CScript::SetStepMode(bool bStep) bool CScript::Run() { if( m_botProg == 0 ) return false; - if ( m_script == 0 || m_len == 0 ) return false; + if ( m_script == nullptr || m_len == 0 ) return false; if ( !m_botProg->Start(m_title) ) return false; @@ -3475,9 +3480,9 @@ bool CScript::IntroduceVirus() start = found[i+1]; i = found[i+0]; - newScript = static_cast(malloc(sizeof(char)*(m_len+strlen(names[i+1])+1))); + newScript = new char[m_len+strlen(names[i+1])+1]; strcpy(newScript, m_script); - delete m_script; + delete[] m_script; m_script = newScript; DeleteToken(m_script, start, strlen(names[i])); @@ -3638,7 +3643,7 @@ void CScript::New(Ui::CEdit* edit, const char* name) bool CScript::SendScript(char* text) { m_len = strlen(text); - m_script = static_cast(malloc(sizeof(char)*(m_len+1))); + m_script = new char[m_len+1]; strcpy(m_script, text); if ( !CheckToken() ) return false; if ( !Compile() ) return false; @@ -3669,8 +3674,8 @@ bool CScript::ReadScript(const char* filename) if ( file == NULL ) return false; fclose(file); - delete m_script; - m_script = 0; + delete[] m_script; + m_script = nullptr; edit = m_interface->CreateEdit(Math::Point(0.0f, 0.0f), Math::Point(0.0f, 0.0f), 0, EVENT_EDIT9); edit->SetMaxChar(Ui::EDITSTUDIOMAX); @@ -3697,7 +3702,7 @@ bool CScript::WriteScript(const char* filename) name = filename; } - if ( m_script == 0 ) + if ( m_script == nullptr ) { remove(filename); return false; -- cgit v1.2.3-1-g7c22