clean a little bit use of new/delete operator.

- replace 1 free by delete

- return NULL inside of a new object noone delete

- remove some unnecessary check for ==NULL before delete

- call delete for each new call (not true : still one new with no
  delete i've added a FIXME)

- some formating change
This commit is contained in:
Serge Martin 2006-11-05 18:39:23 +00:00
parent 6352d7a39a
commit cefdd66e5a
18 changed files with 108 additions and 104 deletions

View file

@ -715,9 +715,7 @@ void attack::refresh_bc()
attack::~attack()
{
if(bc_) {
delete bc_;
}
delete bc_;
}
attack::attack(display& gui, const gamemap& map,

View file

@ -907,6 +907,11 @@ static void run(unsigned specific_battle)
#else
printf("Total combats: %i\n", NUM_UNITS*(NUM_UNITS-1)*(NUM_UNITS-2));
#endif
for (i = 0; i < NUM_UNITS; i++) {
delete u[i];
}
exit(0);
}
@ -1011,6 +1016,11 @@ int main(int argc, char *argv[])
def->print("Defender", 0);
for (i = 0; att[i]; i++)
att[i]->print("Attacker", 0);
delete def;
for (i = 0; att[i]; i++)
delete att[i];
return 0;
}
#endif // Standalone program

View file

@ -190,6 +190,7 @@ int get_first_human_team(const config::child_list::const_iterator& cfg, const co
return result;
}
//return NULL if theme is not found
const config* get_theme(const config& game_config, std::string theme_name){
const config* theme_cfg = NULL;
if(theme_name != "") {
@ -199,9 +200,7 @@ const config* get_theme(const config& game_config, std::string theme_name){
if(theme_cfg == NULL) {
theme_cfg = game_config.find_child("theme","name",preferences::theme());
}
if (theme_cfg == NULL){
theme_cfg = new config();
}
return theme_cfg;
}

View file

@ -104,7 +104,7 @@ dialog::dialog(display &disp, const std::string& title, const std::string& messa
const DIALOG_TYPE type, const std::string& dialog_style,
const std::string& help_topic) : disp_(disp), image_(NULL),
title_(title), style_(dialog_style), message_(NULL),
type_(type), menu_(NULL),
type_(type), menu_(NULL),
help_button_(disp, help_topic), text_widget_(NULL),
action_(NULL), bg_restore_(NULL), result_(CONTINUE_DIALOG)
{
@ -229,6 +229,7 @@ menu& dialog::get_menu() const
if(menu_ == NULL)
{
if(empty_menu == NULL) {
//FIXME create a small mem leak
empty_menu = new gui::menu(disp_.video(),empty_string_vector,false,-1,-1,NULL,&menu::simple_style);
empty_menu->leave();
}
@ -675,7 +676,7 @@ int dialog::process(dialog_process_info &info)
if(
(
//clicking outside of a drop-down or context-menu should close it
standard_buttons_.empty() &&
standard_buttons_.empty() &&
(
(
new_left_button && !info.left_button &&
@ -763,7 +764,7 @@ int standard_dialog_button::action(dialog_process_info &/*info*/) {
}
dialog::help_button::help_button(display& disp, const std::string &help_topic)
dialog::help_button::help_button(display& disp, const std::string &help_topic)
: dialog_button(disp.video(), _("Help")), disp_(disp), topic_(help_topic)
{}

View file

@ -89,7 +89,7 @@ private:
class dialog_button : public button {
public:
dialog_button(CVideo& video, const std::string& label, TYPE type=TYPE_PRESS,
dialog_button(CVideo& video, const std::string& label, TYPE type=TYPE_PRESS,
int simple_result=CONTINUE_DIALOG, dialog_button_action *handler=NULL)
: button(video,label,type,"",DEFAULT_SPACE,false), simple_result_(simple_result),
parent_(NULL), handler_(handler)

View file

@ -2233,7 +2233,7 @@ void display::set_grid(bool grid)
// timestring() returns the current date as a string.
// Uses preferences::clock_format() for formatting.
char *timestring ( void )
std::string timestring ( void )
{
#define TIME_SIZE 10
@ -2243,7 +2243,10 @@ char *timestring ( void )
char *tstring;
tstring = new char[TIME_SIZE];
strftime(tstring,TIME_SIZE,preferences::clock_format().c_str(),lt);
return tstring;
std::string time_string(tstring, TIME_SIZE);
delete[] tstring;
return time_string;
#undef TIME_SIZE
}

View file

@ -312,5 +312,8 @@ int main(int argc, char** argv)
return 0;
}
}
filesystem_close();
return 0;
}

View file

@ -109,7 +109,7 @@ bool filesystem_init()
#ifdef USE_ZIPIOS
if (the_collection != NULL) {
// this is a re-read, cleanup first !
free (the_collection);
delete the_collection;
}
the_collection = new xzipios::XCColl;
@ -132,6 +132,13 @@ bool filesystem_init()
return true;
}
void filesystem_close()
{
#ifdef USE_ZIPIOS
delete the_collection;
#endif
}
namespace {
const mode_t AccessMode = 00770;
}

View file

@ -61,6 +61,7 @@ bool delete_directory(const std::string& dirname);
//basic disk I/O
bool filesystem_init();
void filesystem_close();
std::string read_file(const std::string& fname);
std::istream *istream_file(std::string const &fname);
std::ostream *ostream_file(std::string const &fname);

View file

@ -1771,7 +1771,7 @@ int play_game(int argc, char** argv)
loadscreen::global_loadscreen->set_progress(100, _("Loading title screen."));
delete loadscreen::global_loadscreen;
loadscreen::global_loadscreen = 0;
loadscreen::global_loadscreen = NULL;
for(;;) {
//make sure the game config is always set to how it should be at the title screen
@ -1913,5 +1913,7 @@ int main(int argc, char** argv)
}
#endif
filesystem_close();
return 0;
}

View file

@ -861,7 +861,7 @@ void parse_config_internal(const config *help_cfg, const config *section_cfg,
}
std::vector<topic> generated_topics =
generate_topics(sort_generated,(*section_cfg)["generator"]);
generate_topics(sort_generated,(*section_cfg)["generator"]);
if (sort_topics) {
std::sort(topics.begin(),topics.end(), title_less());

View file

@ -93,7 +93,10 @@ void play_controller::init(CVideo& video){
LOG_NG << "initializing display... " << (SDL_GetTicks() - ticks_) << "\n";
const config* theme_cfg = get_theme(game_config_, level_["theme"]);
gui_ = new display(units_,video,map_,status_,teams_,*theme_cfg, game_config_, level_);
if (theme_cfg)
gui_ = new display(units_,video,map_,status_,teams_,*theme_cfg, game_config_, level_);
else
gui_ = new display(units_,video,map_,status_,teams_,config(), game_config_, level_);
mouse_handler_.set_gui(gui_);
menu_handler_.set_gui(gui_);
theme::set_known_themes(&game_config_);

View file

@ -787,8 +787,8 @@ std::string clock_format()
if(prefs["clock_format"].size())
return prefs["clock_format"];
else
prefs["clock_format"]="%H:%M:%S";
return "%H:%M:%S";
prefs["clock_format"]="%H:%M:%S";
return "%H:%M:%S";
}
const std::string& theme()

View file

@ -111,10 +111,13 @@ void replay_controller::init_gui(){
void replay_controller::init_replay_display(){
LOG_NG << "initializing replay-display... " << (SDL_GetTicks() - ticks_) << "\n";
const config* theme_cfg = get_theme(game_config_, level_["theme"]);
const config* replay_theme_cfg = theme_cfg->child("resolution")->child("replay");
if (NULL != replay_theme_cfg)
gui_->get_theme().modify(replay_theme_cfg);
gui_->invalidate_theme();
if (theme_cfg) {
const config* replay_theme_cfg = theme_cfg->child("resolution")->child("replay");
if (NULL != replay_theme_cfg)
gui_->get_theme().modify(replay_theme_cfg);
gui_->invalidate_theme();
}
LOG_NG << "done initializing replay-display... " << (SDL_GetTicks() - ticks_) << "\n";
}

View file

@ -179,8 +179,8 @@ private:
void set_object_location(theme::object& element, std::string rect_str, std::string ref_id);
static std::map<std::string, config> known_themes;
std::string cur_theme;
config cfg_;
std::string cur_theme;
config cfg_;
std::vector<panel> panels_;
std::vector<label> labels_;
std::vector<menu> menus_;

View file

@ -172,7 +172,6 @@ void list_directory(const std::string& directory,
}
struct Level {
Level(const std::string& itag,Level* p) {tag=itag;parent=p;is_tag=false;};
~Level() {for(child_list::iterator i = data.begin(); i != data.end(); ++i) {delete *i;}};
@ -286,11 +285,6 @@ struct Level {
std::vector<std::string> preproc_actions;
void init_preproc_actions()

View file

@ -151,7 +151,7 @@ unit::unit(const unit& o):
idle_animations_(o.idle_animations_),
levelin_animations_(o.levelin_animations_),
levelout_animations_(o.levelout_animations_),
anim_(o.anim_),
anim_(NULL),
frame_begin_time(o.frame_begin_time),
offset_(o.offset_),
@ -169,7 +169,6 @@ unit::unit(const unit& o):
gamestatus_(o.gamestatus_),
teams_(o.teams_)
{
anim_ = NULL;
unit_halo_ = 0;
unit_anim_halo_ = 0;
}
@ -293,9 +292,9 @@ unit::~unit()
if(unit_anim_halo_) {
halo::remove(unit_anim_halo_);
}
if(anim_) {
delete anim_;
}
delete anim_;
}
@ -1610,10 +1609,8 @@ void unit::set_standing(const display &disp,const gamemap::location& loc, bool w
state_ = STATE_STANDING;
draw_bars_ = with_bars;
offset_=0;
if(anim_) {
delete anim_;
anim_ = NULL;
}
delete anim_;
anim_ = new standing_animation(stand_animation(disp,loc));
anim_->start_animation(anim_->get_begin_time(),true,disp.turbo_speed());
frame_begin_time = anim_->get_begin_time() -1;
@ -1623,10 +1620,9 @@ void unit::set_defending(const display &disp,const gamemap::location& loc, int d
{
state_ = STATE_DEFENDING;
draw_bars_ = false;
if(anim_) {
delete anim_;
anim_ = NULL;
}
delete anim_;
fighting_animation::hit_type hit_type;
if(damage >= hitpoints()) {
hit_type = fighting_animation::KILL;
@ -1650,10 +1646,9 @@ void unit::set_extra_anim(const display &disp,const gamemap::location& loc, std:
{
state_ = STATE_EXTRA;
draw_bars_ = false;
if(anim_) {
delete anim_;
anim_ = NULL;
}
delete anim_;
if(!extra_animation(disp,loc,flag)) {
set_standing(disp,loc);
return;
@ -1667,10 +1662,9 @@ const unit_animation & unit::set_attacking(const display &disp,const gamemap::lo
{
state_ = STATE_ATTACKING;
draw_bars_ = false;
if(anim_) {
delete anim_;
anim_ = NULL;
}
delete anim_;
fighting_animation::hit_type hit_type;
if(damage >= hitpoints()) {
hit_type = fighting_animation::KILL;
@ -1688,10 +1682,9 @@ void unit::set_leading(const display &disp,const gamemap::location& loc)
{
state_ = STATE_LEADING;
draw_bars_ = true;
if(anim_) {
delete anim_;
anim_ = NULL;
}
delete anim_;
anim_ = new leading_animation(lead_animation(disp,loc));
anim_->start_animation(anim_->get_begin_time(), false, disp.turbo_speed());
frame_begin_time = anim_->get_begin_time() -1;
@ -1700,10 +1693,9 @@ void unit::set_leveling_in(const display &disp,const gamemap::location& loc)
{
state_ = STATE_LEVELIN;
draw_bars_ = false;
if(anim_) {
delete anim_;
anim_ = NULL;
}
delete anim_;
anim_ = new levelin_animation(levelingin_animation(disp,loc));
anim_->start_animation(anim_->get_begin_time(), false, disp.turbo_speed());
frame_begin_time = anim_->get_begin_time() -1;
@ -1712,10 +1704,9 @@ void unit::set_leveling_out(const display &disp,const gamemap::location& loc)
{
state_ = STATE_LEVELOUT;
draw_bars_ = false;
if(anim_) {
delete anim_;
anim_ = NULL;
}
delete anim_;
anim_ = new levelout_animation(levelingout_animation(disp,loc));
anim_->start_animation(anim_->get_begin_time(), false, disp.turbo_speed());
frame_begin_time = anim_->get_begin_time() -1;
@ -1724,10 +1715,9 @@ void unit::set_recruited(const display &disp,const gamemap::location& loc)
{
state_ = STATE_RECRUITED;
draw_bars_ = false;
if(anim_) {
delete anim_;
anim_ = NULL;
}
delete anim_;
anim_ = new recruit_animation(recruiting_animation(disp,loc));
// add a fade in effect
anim_->start_animation(anim_->get_begin_time(), false, disp.turbo_speed());
@ -1737,10 +1727,9 @@ void unit::set_healed(const display &disp,const gamemap::location& /*loc*/, int
{
state_ = STATE_HEALED;
draw_bars_ = true;
if(anim_) {
delete anim_;
anim_ = NULL;
}
delete anim_;
anim_ = new unit_animation(0,unit_frame(absolute_image(),240,"1.0","",display::rgb(255,255,255),"0:30,0.5:30,0:30,0.5:30,0:30,0.5:30,0:30,0.5:30"));
anim_->start_animation(anim_->get_begin_time(), false, disp.turbo_speed());
frame_begin_time = anim_->get_begin_time() -1;
@ -1749,10 +1738,9 @@ void unit::set_poisoned(const display &disp,const gamemap::location& /*loc*/, in
{
state_ = STATE_POISONED;
draw_bars_ = true;
if(anim_) {
delete anim_;
anim_ = NULL;
}
delete anim_;
anim_ = new unit_animation(0,unit_frame(absolute_image(),240,"1.0","",display::rgb(0,255,0),"0:30,0.5:30,0:30,0.5:30,0:30,0.5:30,0:30,0.5:30"));
anim_->start_animation(anim_->get_begin_time(), false, disp.turbo_speed());
frame_begin_time = anim_->get_begin_time() -1;
@ -1762,10 +1750,9 @@ void unit::set_teleporting(const display &disp,const gamemap::location& loc)
{
state_ = STATE_TELEPORT;
draw_bars_ = false;
if(anim_) {
delete anim_;
anim_ = NULL;
}
delete anim_;
anim_ = new unit_animation(teleport_animation(disp,loc));
anim_->start_animation(anim_->get_begin_time(),false,disp.turbo_speed());
frame_begin_time = anim_->get_begin_time() -1;
@ -1775,10 +1762,9 @@ void unit::set_dying(const display &disp,const gamemap::location& loc,const atta
{
state_ = STATE_DYING;
draw_bars_ = false;
if(anim_) {
delete anim_;
anim_ = NULL;
}
delete anim_;
anim_ = new death_animation(die_animation(disp,loc,fighting_animation::KILL,attack,secondary_attack));
image::locator tmp_image = anim_->get_last_frame().image();
anim_->add_frame(600,unit_frame(tmp_image,600,"1~0:600"));
@ -1789,10 +1775,9 @@ void unit::set_healing(const display &disp,const gamemap::location& loc)
{
state_ = STATE_HEALING;
draw_bars_ = true;
if(anim_) {
delete anim_;
anim_ = NULL;
}
delete anim_;
anim_ = new healing_animation(heal_animation(disp,loc));
anim_->start_animation(anim_->get_begin_time(), false, disp.turbo_speed());
frame_begin_time = anim_->get_begin_time() -1;
@ -1801,10 +1786,9 @@ void unit::set_victorious(const display &disp,const gamemap::location& loc)
{
state_ = STATE_VICTORIOUS;
draw_bars_ = false;
if(anim_) {
delete anim_;
anim_ = NULL;
}
delete anim_;
anim_ = new victory_animation(victorious_animation(disp,loc));
anim_->start_animation(anim_->get_begin_time(), false, disp.turbo_speed());
frame_begin_time = anim_->get_begin_time() -1;
@ -1818,10 +1802,9 @@ void unit::set_walking(const display &disp,const gamemap::location& loc)
}
state_ = STATE_WALKING;
draw_bars_ = false;
if(anim_) {
delete anim_;
anim_ = NULL;
}
delete anim_;
anim_ = new movement_animation(move_animation(disp,loc));
anim_->start_animation(anim_->get_begin_time(), false, disp.turbo_speed());
frame_begin_time = anim_->get_begin_time() -1;
@ -1832,10 +1815,9 @@ void unit::set_idling(const display &disp,const gamemap::location& loc)
{
state_ = STATE_IDLING;
draw_bars_ = true;
if(anim_) {
delete anim_;
anim_ = NULL;
}
delete anim_;
anim_ = new idle_animation(idling_animation(disp,loc));
anim_->start_animation(anim_->get_begin_time(), false, disp.turbo_speed());
frame_begin_time = anim_->get_begin_time() -1;
@ -1949,8 +1931,6 @@ void unit::redraw_unit(display& disp,gamemap::location hex)
}
surface ellipse_front(NULL);
surface ellipse_back(NULL);
if(preferences::show_side_colours() && draw_bars_) {

View file

@ -178,9 +178,9 @@ void upload_log::start(game_state &state, const team &team,
if (game_finished(game_))
config_.add_child("game", *game_);
if (game_)
delete game_;
//start could be call more than one
//so delete game_ to prevent memory leak
delete game_;
game_ = new config();
(*game_)["time"] = lexical_cast<std::string>(SDL_GetTicks() / 1000);
(*game_)["campaign"] = state.campaign_define;