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
dev-ui
Piotr Dziwinski 2012-12-28 13:37:08 +01:00
parent 4cbb63f5b7
commit 3e4c1a1ad8
5 changed files with 69 additions and 53 deletions

View File

@ -127,7 +127,8 @@ CBotString::CBotString()
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; m_ptr = NULL;
if (m_lg>0) if (m_lg>0)
{ {
m_ptr = static_cast<char*>(malloc(m_lg+1)); m_ptr = new char[m_lg+1];
strcpy(m_ptr, p); strcpy(m_ptr, p);
} }
} }
@ -150,7 +151,7 @@ CBotString::CBotString(const CBotString& srcString)
m_ptr = NULL; m_ptr = NULL;
if (m_lg>0) if (m_lg>0)
{ {
m_ptr = static_cast<char*>(malloc(m_lg+1)); m_ptr = new char[m_lg+1];
strcpy(m_ptr, srcString.m_ptr); strcpy(m_ptr, srcString.m_ptr);
} }
} }
@ -285,12 +286,12 @@ CBotString CBotString::Mid(int start, int lg)
if ( lg < 0 ) lg = m_lg - start; if ( lg < 0 ) lg = m_lg - start;
char* p = static_cast<char*>(malloc(m_lg+1)); char* p = new char[m_lg+1];
strcpy(p, m_ptr+start); strcpy(p, m_ptr+start);
p[lg] = 0; p[lg] = 0;
res = p; res = p;
free(p); delete[] p;
return res; return res;
} }
@ -314,15 +315,16 @@ void CBotString::MakeLower()
bool CBotString::LoadString(unsigned int id) bool CBotString::LoadString(unsigned int id)
{ {
const char * str = NULL; const char * str = nullptr;
str = MapIdToString(static_cast<EID>(id)); str = MapIdToString(static_cast<EID>(id));
if (m_ptr != NULL) free(m_ptr); if (m_ptr != nullptr)
delete[] m_ptr;
m_lg = strlen(str); m_lg = strlen(str);
m_ptr = NULL; m_ptr = NULL;
if (m_lg > 0) if (m_lg > 0)
{ {
m_ptr = static_cast<char*>(malloc(m_lg+1)); m_ptr = new char[m_lg+1];
strcpy(m_ptr, str); strcpy(m_ptr, str);
return true; return true;
} }
@ -332,14 +334,14 @@ bool CBotString::LoadString(unsigned int id)
const CBotString& CBotString::operator=(const CBotString& stringSrc) const CBotString& CBotString::operator=(const CBotString& stringSrc)
{ {
free(m_ptr); delete[] m_ptr;
m_ptr = NULL; m_ptr = nullptr;
m_lg = stringSrc.m_lg; m_lg = stringSrc.m_lg;
if (m_lg > 0) if (m_lg > 0)
{ {
m_ptr = static_cast<char*>(malloc(m_lg+1)); m_ptr = new char[m_lg+1];
strcpy(m_ptr, stringSrc.m_ptr); 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) const CBotString& CBotString::operator+(const CBotString& stringSrc)
{ {
char* p = static_cast<char*>(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); if (m_ptr!=NULL) strcpy(p, m_ptr);
char* pp = p + m_lg; char* pp = p + m_lg;
if (stringSrc.m_ptr!=NULL) strcpy(pp, stringSrc.m_ptr); if (stringSrc.m_ptr!=NULL) strcpy(pp, stringSrc.m_ptr);
free(m_ptr); delete[] m_ptr;
m_ptr = p; m_ptr = p;
m_lg += stringSrc.m_lg; m_lg += stringSrc.m_lg;
@ -370,11 +372,11 @@ const CBotString& CBotString::operator+(const CBotString& stringSrc)
const CBotString& CBotString::operator=(const char ch) const CBotString& CBotString::operator=(const char ch)
{ {
free(m_ptr); delete[] m_ptr;
m_lg = 1; m_lg = 1;
m_ptr = static_cast<char*>(malloc(2)); m_ptr = new char[2];
m_ptr[0] = ch; m_ptr[0] = ch;
m_ptr[1] = 0; m_ptr[1] = 0;
@ -383,16 +385,16 @@ const CBotString& CBotString::operator=(const char ch)
const CBotString& CBotString::operator=(const char* pString) const CBotString& CBotString::operator=(const char* pString)
{ {
free(m_ptr); delete[] m_ptr;
m_ptr = NULL; m_ptr = nullptr;
if (pString != NULL) if (pString != nullptr)
{ {
m_lg = strlen(pString); m_lg = strlen(pString);
if (m_lg != 0) if (m_lg != 0)
{ {
m_ptr = static_cast<char*>(malloc(m_lg+1)); m_ptr = new char[m_lg+1];
strcpy(m_ptr, pString); strcpy(m_ptr, pString);
} }
} }
@ -403,13 +405,13 @@ const CBotString& CBotString::operator=(const char* pString)
const CBotString& CBotString::operator+=(const char ch) const CBotString& CBotString::operator+=(const char ch)
{ {
char* p = static_cast<char*>(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++] = ch;
p[m_lg] = 0; p[m_lg] = 0;
free(m_ptr); delete[] m_ptr;
m_ptr = p; m_ptr = p;
@ -418,7 +420,7 @@ const CBotString& CBotString::operator+=(const char ch)
const CBotString& CBotString::operator+=(const CBotString& str) const CBotString& CBotString::operator+=(const CBotString& str)
{ {
char* p = static_cast<char*>(malloc(m_lg+str.m_lg+1)); char* p = new char[m_lg+str.m_lg+1];
strcpy(p, m_ptr); strcpy(p, m_ptr);
char* pp = p + m_lg; char* pp = p + m_lg;
@ -426,7 +428,7 @@ const CBotString& CBotString::operator+=(const CBotString& str)
m_lg = m_lg + str.m_lg; m_lg = m_lg + str.m_lg;
free(m_ptr); delete[] m_ptr;
m_ptr = p; m_ptr = p;
@ -500,8 +502,8 @@ bool CBotString::IsEmpty() const
void CBotString::Empty() void CBotString::Empty()
{ {
free(m_ptr); delete[] m_ptr;
m_ptr = NULL; m_ptr = nullptr;
m_lg = 0; m_lg = 0;
} }

View File

@ -124,14 +124,14 @@ bool PNGSaveSurface(const char *filename, SDL_Surface *surf)
png_write_info(png_ptr, info_ptr); png_write_info(png_ptr, info_ptr);
png_set_packing(png_ptr); png_set_packing(png_ptr);
row_pointers = static_cast<png_bytep*>( malloc(sizeof(png_bytep)*surf->h) ); row_pointers = new png_bytep[surf->h];
for (i = 0; i < surf->h; i++) for (i = 0; i < surf->h; i++)
row_pointers[i] = static_cast<png_bytep>( static_cast<Uint8 *>(surf->pixels) ) + i*surf->pitch; row_pointers[i] = static_cast<png_bytep>( static_cast<Uint8 *>(surf->pixels) ) + i*surf->pitch;
png_write_image(png_ptr, row_pointers); png_write_image(png_ptr, row_pointers);
png_write_end(png_ptr, info_ptr); png_write_end(png_ptr, info_ptr);
/* Cleaning out... */ /* Cleaning out... */
free(row_pointers); delete[] row_pointers;
png_destroy_write_struct(&png_ptr, &info_ptr); png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp); fclose(fp);

View File

@ -99,7 +99,7 @@ CBrain::CBrain(CInstanceManager* iMan, CObject* object)
m_selScript = 0; m_selScript = 0;
m_bTraceRecord = false; m_bTraceRecord = false;
m_traceRecordBuffer = 0; m_traceRecordBuffer = nullptr;
} }
// Object's destructor. // Object's destructor.
@ -111,12 +111,21 @@ CBrain::~CBrain()
for ( i=0 ; i<BRAINMAXSCRIPT ; i++ ) for ( i=0 ; i<BRAINMAXSCRIPT ; i++ )
{ {
delete m_script[i]; delete m_script[i];
m_script[i] = nullptr;
} }
delete m_primaryTask; delete m_primaryTask;
m_primaryTask = nullptr;
delete m_secondaryTask; delete m_secondaryTask;
m_secondaryTask = nullptr;
delete m_studio; delete m_studio;
delete m_traceRecordBuffer; m_studio = nullptr;
delete[] m_traceRecordBuffer;
m_traceRecordBuffer = nullptr;
m_iMan->DeleteInstance(CLASS_BRAIN, this); m_iMan->DeleteInstance(CLASS_BRAIN, this);
} }
@ -2791,8 +2800,8 @@ void CBrain::TraceRecordStart()
m_traceColor = -1; m_traceColor = -1;
} }
delete m_traceRecordBuffer; delete[] m_traceRecordBuffer;
m_traceRecordBuffer = static_cast<TraceRecord*>(malloc(sizeof(TraceRecord)*MAXTRACERECORD)); m_traceRecordBuffer = new TraceRecord[MAXTRACERECORD];
m_traceRecordIndex = 0; m_traceRecordIndex = 0;
} }
@ -2858,10 +2867,10 @@ void CBrain::TraceRecordStop()
int max, i; int max, i;
char* buffer; char* buffer;
if ( m_traceRecordBuffer == 0 ) return; if ( m_traceRecordBuffer == nullptr ) return;
max = 10000; max = 10000;
buffer = static_cast<char*>(malloc(max)); buffer = new char[max];
*buffer = 0; *buffer = 0;
strncat(buffer, "extern void object::AutoDraw()\n{\n", max-1); strncat(buffer, "extern void object::AutoDraw()\n{\n", max-1);
@ -2892,8 +2901,8 @@ void CBrain::TraceRecordStop()
} }
TraceRecordPut(buffer, max, lastOper, lastParam); TraceRecordPut(buffer, max, lastOper, lastParam);
delete m_traceRecordBuffer; delete[] m_traceRecordBuffer;
m_traceRecordBuffer = 0; m_traceRecordBuffer = nullptr;
strncat(buffer, "}\n", max-1); strncat(buffer, "}\n", max-1);
buffer[max-1] = 0; 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] = new CScript(m_iMan, m_object, &m_secondaryTask);
} }
m_script[i]->SendScript(buffer); m_script[i]->SendScript(buffer);
delete buffer; delete[] buffer;
} }
// Saves an instruction CBOT. // Saves an instruction CBOT.

View File

@ -2119,7 +2119,7 @@ bool CTaskGoto::BitmapOpen()
BitmapClose(); BitmapClose();
m_bmSize = static_cast<int>(3200.0f/BM_DIM_STEP); m_bmSize = static_cast<int>(3200.0f/BM_DIM_STEP);
m_bmArray = static_cast<unsigned char*>(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); memset(m_bmArray, 0, m_bmSize*m_bmSize/8*2);
m_bmOffset = m_bmSize/2; m_bmOffset = m_bmSize/2;
@ -2137,7 +2137,7 @@ bool CTaskGoto::BitmapOpen()
bool CTaskGoto::BitmapClose() bool CTaskGoto::BitmapClose()
{ {
free(m_bmArray); delete[] m_bmArray;
m_bmArray = 0; m_bmArray = 0;
return true; return true;
} }

View File

@ -2753,9 +2753,14 @@ void CScript::InitFonctions()
CScript::~CScript() CScript::~CScript()
{ {
delete m_botProg; delete m_botProg;
m_botProg = nullptr;
delete m_primaryTask; delete m_primaryTask;
delete m_script; m_primaryTask = nullptr;
m_script = 0;
delete[] m_script;
m_script = nullptr;
m_len = 0; m_len = 0;
m_iMan->DeleteInstance(CLASS_SCRIPT, this); m_iMan->DeleteInstance(CLASS_SCRIPT, this);
@ -2766,7 +2771,7 @@ CScript::~CScript()
void CScript::PutScript(Ui::CEdit* edit, const char* name) void CScript::PutScript(Ui::CEdit* edit, const char* name)
{ {
if ( m_script == 0 ) if ( m_script == nullptr )
{ {
New(edit, name); New(edit, name);
} }
@ -2785,11 +2790,11 @@ bool CScript::GetScript(Ui::CEdit* edit)
{ {
int len; int len;
delete m_script; delete[] m_script;
m_script = 0; m_script = nullptr;
len = edit->GetTextLength(); len = edit->GetTextLength();
m_script = static_cast<char*>(malloc(sizeof(char)*(len+1))); m_script = new char[len+1];
edit->GetText(m_script, len+1); edit->GetText(m_script, len+1);
edit->GetCursor(m_cursor2, m_cursor1); edit->GetCursor(m_cursor2, m_cursor1);
@ -2997,7 +3002,7 @@ void CScript::SetStepMode(bool bStep)
bool CScript::Run() bool CScript::Run()
{ {
if( m_botProg == 0 ) return false; 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; if ( !m_botProg->Start(m_title) ) return false;
@ -3475,9 +3480,9 @@ bool CScript::IntroduceVirus()
start = found[i+1]; start = found[i+1];
i = found[i+0]; i = found[i+0];
newScript = static_cast<char*>(malloc(sizeof(char)*(m_len+strlen(names[i+1])+1))); newScript = new char[m_len+strlen(names[i+1])+1];
strcpy(newScript, m_script); strcpy(newScript, m_script);
delete m_script; delete[] m_script;
m_script = newScript; m_script = newScript;
DeleteToken(m_script, start, strlen(names[i])); 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) bool CScript::SendScript(char* text)
{ {
m_len = strlen(text); m_len = strlen(text);
m_script = static_cast<char*>(malloc(sizeof(char)*(m_len+1))); m_script = new char[m_len+1];
strcpy(m_script, text); strcpy(m_script, text);
if ( !CheckToken() ) return false; if ( !CheckToken() ) return false;
if ( !Compile() ) return false; if ( !Compile() ) return false;
@ -3669,8 +3674,8 @@ bool CScript::ReadScript(const char* filename)
if ( file == NULL ) return false; if ( file == NULL ) return false;
fclose(file); fclose(file);
delete m_script; delete[] m_script;
m_script = 0; m_script = nullptr;
edit = m_interface->CreateEdit(Math::Point(0.0f, 0.0f), Math::Point(0.0f, 0.0f), 0, EVENT_EDIT9); edit = m_interface->CreateEdit(Math::Point(0.0f, 0.0f), Math::Point(0.0f, 0.0f), 0, EVENT_EDIT9);
edit->SetMaxChar(Ui::EDITSTUDIOMAX); edit->SetMaxChar(Ui::EDITSTUDIOMAX);
@ -3697,7 +3702,7 @@ bool CScript::WriteScript(const char* filename)
name = filename; name = filename;
} }
if ( m_script == 0 ) if ( m_script == nullptr )
{ {
remove(filename); remove(filename);
return false; return false;