From b0e5f436cd0bcaea2a2bdb9888e55a5f63248684 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 29 Jan 2006 01:55:36 +0000 Subject: [PATCH] - lots of memory leaks fixed. --- src/common/gamecode/economy.c | 37 ++++++++++---------- src/common/kernel/battle.c | 66 ++++++++++++++++++----------------- src/common/kernel/building.c | 29 +++++++-------- src/common/kernel/eressea.c | 7 ++-- src/common/kernel/give.c | 4 +-- src/common/kernel/magic.c | 17 ++++----- src/common/kernel/message.c | 38 ++++++++++---------- src/common/kernel/save.c | 17 +++++---- src/common/kernel/unit.c | 35 +++++++++---------- src/mapper/map_partei.c | 1 - 10 files changed, 125 insertions(+), 126 deletions(-) diff --git a/src/common/gamecode/economy.c b/src/common/gamecode/economy.c index 1f5440e6c..b98a6c850 100644 --- a/src/common/gamecode/economy.c +++ b/src/common/gamecode/economy.c @@ -499,7 +499,7 @@ recruit(unit * u, struct order * ord, request ** recruitorders) if (recruitcost) { pl = getplane(r); if (pl && fval(pl, PFL_NORECRUITS)) { - add_message(&u->faction->msgs, + ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "error_pflnorecruit", "")); return; } @@ -943,9 +943,8 @@ maintain(building * b, boolean first) assert(cost==0); } } else { - message * msg = add_message(&u->faction->msgs, + ADDMSG(&u->faction->msgs, msg_message("maintenancefail", "unit building", u, b)); - msg_release(msg); return false; } return true; @@ -1214,7 +1213,7 @@ allocate_resource(unit * u, const resource_type * rtype, int want) && !alliedunit(u2, u->faction, HELP_GUARD) && armedmen(u2) ) { - add_message(&u->faction->msgs, + ADDMSG(&u->faction->msgs, msg_feedback(u, u->thisorder, "region_guarded", "guard", u2)); return; } @@ -1233,7 +1232,7 @@ allocate_resource(unit * u, const resource_type * rtype, int want) && u2->number && !alliedunit(u2, u->faction, HELP_GUARD)) { - add_message(&u->faction->msgs, + ADDMSG(&u->faction->msgs, msg_feedback(u, u->thisorder, "region_guarded", "guard", u2)); return; } @@ -1518,7 +1517,7 @@ split_allocations(region * r) } if (al->want==INT_MAX) al->want = al->get; if (fval(al, AFL_LOWSKILL)) { - add_message(&al->unit->faction->msgs, + ADDMSG(&al->unit->faction->msgs, msg_message("produce_lowskill", "unit region resource", al->unit, al->unit->region, rtype)); } else { @@ -1924,7 +1923,7 @@ buy(unit * u, request ** buyorders, struct order * ord) } } if (r_demand(r, ltype)) { - add_message(&u->faction->msgs, + ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "luxury_notsold", "")); return; } @@ -2158,7 +2157,7 @@ sell(unit * u, request ** sellorders, struct order * ord) /* Belagerte Einheiten können nichts verkaufen. */ if (besieged(u)) { - add_message(&u->faction->msgs, + ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "error60", "")); return false; } @@ -2327,21 +2326,21 @@ plant(region *r, unit *u, int raw) skill = eff_skill(u, SK_HERBALISM, r); itype = rherbtype(r); if (skill < 6) { - add_message(&u->faction->msgs, + ADDMSG(&u->faction->msgs, msg_feedback(u, u->thisorder, "plant_skills", "skill minskill product", SK_HERBALISM, 6, itype->rtype, 1)); return; } /* Wasser des Lebens prüfen */ if (get_pooled(u, rt_water, GET_DEFAULT) == 0) { - add_message(&u->faction->msgs, + ADDMSG(&u->faction->msgs, msg_feedback(u, u->thisorder, "resource_missing", "missing", rt_water)); return; } n = get_pooled(u, itype->rtype, GET_DEFAULT); /* Kräuter prüfen */ if (n==0) { - add_message(&u->faction->msgs, + ADDMSG(&u->faction->msgs, msg_feedback(u, u->thisorder, "resource_missing", "missing", itype->rtype)); return; @@ -2383,13 +2382,13 @@ planttrees(region *r, unit *u, int raw) /* Skill prüfen */ skill = eff_skill(u, SK_HERBALISM, r); if (skill < 6) { - add_message(&u->faction->msgs, + ADDMSG(&u->faction->msgs, msg_feedback(u, u->thisorder, "plant_skills", "skill minskill product", SK_HERBALISM, 6, rtype, 1)); return; } if (fval(r, RF_MALLORN) && skill < 7 ) { - add_message(&u->faction->msgs, + ADDMSG(&u->faction->msgs, msg_feedback(u, u->thisorder, "plant_skills", "skill minskill product", SK_HERBALISM, 7, rtype, 1)); return; @@ -2398,7 +2397,7 @@ planttrees(region *r, unit *u, int raw) n = get_pooled(u, rtype, GET_DEFAULT); /* Samen prüfen */ if (n==0) { - add_message(&u->faction->msgs, + ADDMSG(&u->faction->msgs, msg_feedback(u, u->thisorder, "resource_missing", "missing", rtype)); return; } @@ -2457,7 +2456,7 @@ breedtrees(region *r, unit *u, int raw) n = get_pooled(u, rtype, GET_DEFAULT); /* Samen prüfen */ if (n==0) { - add_message(&u->faction->msgs, + ADDMSG(&u->faction->msgs, msg_feedback(u, u->thisorder, "resource_missing", "missing", rtype)); return; } @@ -2718,7 +2717,7 @@ steal_cmd(unit * u, struct order * ord, request ** stealorders) } if (u2->faction->age < NewbieImmunity()) { - add_message(&u->faction->msgs, + ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "newbie_immunity_error", "turns", NewbieImmunity())); return; @@ -2731,7 +2730,7 @@ steal_cmd(unit * u, struct order * ord, request ** stealorders) assert(u->region==u2->region); if (!can_contact(r, u, u2)) { - add_message(&u->faction->msgs, msg_feedback(u, ord, "error60", "")); + ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "error60", "")); return; } @@ -3025,7 +3024,7 @@ tax_cmd(unit * u, struct order * ord, request ** taxorders) u2 = is_guarded(r, u, GUARD_TAX); if (u2) { - add_message(&u->faction->msgs, + ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "region_guarded", "guard", u2)); return; } @@ -3135,7 +3134,7 @@ produce(void) case K_WORK: if (playerrace(u->race)) work(u, u->thisorder); else if (playerrace(u->faction->race)) { - add_message(&u->faction->msgs, + ADDMSG(&u->faction->msgs, msg_feedback(u, u->thisorder, "race_cantwork", "race", u->race)); } break; diff --git a/src/common/kernel/battle.c b/src/common/kernel/battle.c index ab6d85263..31a4a1545 100644 --- a/src/common/kernel/battle.c +++ b/src/common/kernel/battle.c @@ -3183,7 +3183,8 @@ make_battle(region * r) static void free_side(side * si) { - cv_kill(&si->fighters); + cv_kill(&si->fighters); + cv_kill(&si->leader.fighters); } static void @@ -3205,40 +3206,41 @@ free_fighter(fighter * fig) static void free_battle(battle * b) { - side *side; - fighter *fighter; - meffect *meffect; - int max_fac_no = 0; - - if (bdebug) { - fclose(bdebug); - } - - while (b->factions) { + side *side; + fighter *fighter; + meffect *meffect; + int max_fac_no = 0; + + if (bdebug) { + fclose(bdebug); + } + + while (b->factions) { bfaction * bf = b->factions; - faction * f = bf->faction; + faction * f = bf->faction; b->factions = bf->next; - max_fac_no = max(max_fac_no, f->no); + max_fac_no = max(max_fac_no, f->no); free(bf); - } - - cv_foreach(side, b->sides) { - free_side(side); - free(side); - } - cv_next(side); - cv_kill(&b->sides); - cv_foreach(fighter, b->fighters) { - free_fighter(fighter); - free(fighter); - } - cv_next(fighter); - cv_kill(&b->fighters); - cv_foreach(meffect, b->meffects) { - free(meffect); - } - cv_next(meffect); - cv_kill(&b->meffects); + } + + cv_foreach(side, b->sides) { + free_side(side); + free(side); + } + cv_next(side); + cv_kill(&b->sides); + cv_kill(&b->leaders); + cv_foreach(fighter, b->fighters) { + free_fighter(fighter); + free(fighter); + } + cv_next(fighter); + cv_kill(&b->fighters); + cv_foreach(meffect, b->meffects) { + free(meffect); + } + cv_next(meffect); + cv_kill(&b->meffects); } static int * diff --git a/src/common/kernel/building.c b/src/common/kernel/building.c index 97c16a31d..95d131d9a 100644 --- a/src/common/kernel/building.c +++ b/src/common/kernel/building.c @@ -481,7 +481,7 @@ new_building(const struct building_type * btype, region * r, const struct locale void destroy_building(building * b) { - unit *u; + unit *u; direction_t d; static const struct building_type * bt_caravan, * bt_dam, * bt_tunnel; boolean init = false; @@ -493,15 +493,15 @@ destroy_building(building * b) bt_tunnel = bt_find("tunnel"); } - if (!bfindhash(b->no)) return; - for (u=b->region->units; u; u=u->next) { - if (u->building == b) leave(b->region, u); - } - - b->size = 0; - update_lighthouse(b); - bunhash(b); - + if (!bfindhash(b->no)) return; + for (u=b->region->units; u; u=u->next) { + if (u->building == b) leave(b->region, u); + } + + b->size = 0; + update_lighthouse(b); + bunhash(b); + /* Falls Karawanserei, Damm oder Tunnel einstürzen, wird die schon * gebaute Straße zur Hälfte vernichtet */ if (b->type == bt_caravan || b->type == bt_dam || b->type == bt_tunnel) { @@ -511,12 +511,9 @@ destroy_building(building * b) } } -#if 0 /* TODO: Memoryleak. Aber ohne klappt das Rendern nicht! */ - removelist(&b->region->buildings, b); -#endif - /* Stattdessen nur aus Liste entfernen, aber im Speicher halten. */ - choplist(&b->region->buildings, b); - handle_event(&b->attribs, "destroy", b); + /* Stattdessen nur aus Liste entfernen, aber im Speicher halten. */ + choplist(&b->region->buildings, b); + handle_event(&b->attribs, "destroy", b); } extern attrib_type at_icastle; diff --git a/src/common/kernel/eressea.c b/src/common/kernel/eressea.c index a234e296f..442eb1e7e 100644 --- a/src/common/kernel/eressea.c +++ b/src/common/kernel/eressea.c @@ -693,9 +693,10 @@ stripfaction (faction * f) /* TODO: inhalt auch löschen */ if (f->msgs) free_messagelist(f->msgs); while (f->battles) { - struct bmsg * b = f->battles; - f->battles = b->next; - if (b->msgs) free_messagelist(b->msgs); + struct bmsg * bm = f->battles; + f->battles = bm->next; + if (bm->msgs) free_messagelist(bm->msgs); + free(bm); } freelist(f->allies); diff --git a/src/common/kernel/give.c b/src/common/kernel/give.c index 4cf9f475d..78ac4cbcd 100644 --- a/src/common/kernel/give.c +++ b/src/common/kernel/give.c @@ -66,11 +66,11 @@ add_give(unit * u, unit * u2, int n, const resource_type * rtype, struct order * } else if (!u2 || u2->faction!=u->faction) { assert(rtype); - add_message(&u->faction->msgs, + ADDMSG(&u->faction->msgs, msg_message("give", "unit target resource amount", u, u2?ucansee(u->faction, u2, u_unknown()):u_peasants(), rtype, n)); if (u2) { - add_message(&u2->faction->msgs, + ADDMSG(&u2->faction->msgs, msg_message("give", "unit target resource amount", ucansee(u2->faction, u, u_unknown()), u2, rtype, n)); } diff --git a/src/common/kernel/magic.c b/src/common/kernel/magic.c index 95e3e56e1..1b051ff6b 100644 --- a/src/common/kernel/magic.c +++ b/src/common/kernel/magic.c @@ -187,7 +187,7 @@ extern int dice(int count, int value); static void init_mage(attrib * a) { - a->data.v = calloc(sizeof(sc_mage), 1); + a->data.v = calloc(sizeof(sc_mage), 1); } static void @@ -336,15 +336,12 @@ create_mage(unit * u, magic_t mtyp) sc_mage *mage; attrib *a; +#ifndef NDEBUG a = a_find(u->attribs, &at_mage); - if (a==NULL) { - a = a_add(&u->attribs, a_new(&at_mage)); - mage = calloc(1, sizeof(sc_mage)); - a->data.v = mage; - } else { - mage = a->data.v; - memset(mage, 0, sizeof(sc_mage)); - } + assert(a==NULL); +#endif + a = a_add(&u->attribs, a_new(&at_mage)); + mage = a->data.v; mage->magietyp = mtyp; createspelllist(u, mtyp); @@ -966,7 +963,7 @@ cancast(unit * u, const spell * sp, int level, int range, struct order * ord) res->number = itemanz; res->type = rtype; res->next = reslist; - reslist = res->next; + reslist = res; } } } diff --git a/src/common/kernel/message.c b/src/common/kernel/message.c index 14c9e5ab0..4adae0b30 100644 --- a/src/common/kernel/message.c +++ b/src/common/kernel/message.c @@ -164,27 +164,27 @@ message * msg_message(const char * name, const char* sig, ...) /* msg_message("oops_error", "unit region command", u, r, cmd) */ { - va_list marker; - const message_type * mtype = mt_find(name); - char paramname[64]; - const char *ic = sig; - variant args[16]; - memset(args, 0, sizeof(args)); - - if (!mtype) { - log_warning(("trying to create message of unknown type \"%s\"\n", name)); - return NULL; - } - - va_start(marker, sig); - while (*ic && !isalnum(*ic)) ic++; - while (*ic) { - char * oc = paramname; + va_list marker; + const message_type * mtype = mt_find(name); + char paramname[64]; + const char *ic = sig; + variant args[16]; + memset(args, 0, sizeof(args)); + + if (!mtype) { + log_warning(("trying to create message of unknown type \"%s\"\n", name)); + return NULL; + } + + va_start(marker, sig); + while (*ic && !isalnum(*ic)) ic++; + while (*ic) { + char * oc = paramname; int i; - + while (isalnum(*ic)) *oc++ = *ic++; - *oc = '\0'; - + *oc = '\0'; + for (i=0;i!=mtype->nparameters;++i) { if (!strcmp(paramname, mtype->pnames[i])) break; } diff --git a/src/common/kernel/save.c b/src/common/kernel/save.c index 3ef24f2c3..ab998e2ca 100644 --- a/src/common/kernel/save.c +++ b/src/common/kernel/save.c @@ -977,13 +977,18 @@ readunit(FILE * F) rs(F, buf); space = strchr(buf, ' '); - while (space!=NULL) { - strcpy(space, space+1); - space=strchr(space, ' '); + if (space!=NULL) { + char * inc = space+1; + char * outc = space; + do { + while (*inc==' ') ++inc; + while (*inc) { + *outc++ = *inc++; + if (*inc==' ') break; + } + } while (*inc); + *outc = 0; } -/* if (strcmp(buf, "giant turtle")==0) strcpy(buf, "giantturtle"); - if (strcmp(buf, "young dragon")==0) strcpy(buf, "youngdragon"); - if (strcmp(buf, "young dragon")==0) strcpy(buf, "youngdragon"); */ u->race = rc_find(buf); assert(u->race); rs(F, buf); diff --git a/src/common/kernel/unit.c b/src/common/kernel/unit.c index f00c584dc..4dd2be045 100644 --- a/src/common/kernel/unit.c +++ b/src/common/kernel/unit.c @@ -134,7 +134,7 @@ distribute_items(unit * u) if (u->items==NULL) return; - for (au=r->units;au;au=au->next) if (au->faction!=f) { + for (au=r->units;au;au=au->next) if (au->faction!=f && au->number>0) { if (alliedunit(u, au->faction, HELP_MONEY) && alliedunit(au, f, HELP_GIVE)) { struct friend * nf, ** fr = &friends; @@ -153,9 +153,9 @@ distribute_items(unit * u) } } - if (friends && number) { - struct friend * nf = friends; - while (nf) { + if (friends) { + while (friends) { + struct friend * nf = friends; unit * u2 = nf->unit; item * itm = u->items; while (itm!=NULL) { @@ -170,9 +170,8 @@ distribute_items(unit * u) itm = itn; } number -= nf->number; - nf = nf->next; - free(friends); - friends = nf; + friends = nf->next; + free(nf); } friends = NULL; } @@ -1197,18 +1196,18 @@ invisible(const unit *target, const unit * viewer) void stripunit(unit * u) { - free(u->name); - free(u->display); + free(u->name); + free(u->display); free_order(u->thisorder); - free_orders(&u->orders); - if(u->skills) free(u->skills); - while (u->items) { - item * it = u->items->next; - u->items->next = NULL; - i_free(u->items); - u->items = it; - } - while (u->attribs) a_remove (&u->attribs, u->attribs); + free_orders(&u->orders); + if(u->skills) free(u->skills); + while (u->items) { + item * it = u->items->next; + u->items->next = NULL; + i_free(u->items); + u->items = it; + } + while (u->attribs) a_remove (&u->attribs, u->attribs); while (u->reservations) { struct reservation *res = u->reservations; u->reservations = res->next; diff --git a/src/mapper/map_partei.c b/src/mapper/map_partei.c index 0900b7e0b..f7157f73d 100644 --- a/src/mapper/map_partei.c +++ b/src/mapper/map_partei.c @@ -28,7 +28,6 @@ #include #include #include -#include #include /* util includes */