Make sure we always have copies of equipment descriptions

Having pointers copied around that might get freed elsewhere could be a
problem.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2014-11-14 13:33:12 -08:00
parent 1055b5afd3
commit 8d766e13e2
4 changed files with 13 additions and 5 deletions

8
dive.c
View file

@ -434,6 +434,10 @@ void clear_dive(struct dive *d)
taglist_free(d->tag_list);
STRUCTURED_LIST_FREE(struct divecomputer, d->dc.next, free_dc);
STRUCTURED_LIST_FREE(struct picture, d->picture_list, free_pic);
for (int i = 0; i < MAX_CYLINDERS; i++)
free((void *)d->cylinder[i].type.description);
for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++)
free((void *)d->weightsystem[i].description);
memset(d, 0, sizeof(struct dive));
}
@ -452,6 +456,10 @@ void copy_dive(struct dive *s, struct dive *d)
d->location = copy_string(s->location);
d->notes = copy_string(s->notes);
d->suit = copy_string(s->suit);
for (int i = 0; i < MAX_CYLINDERS; i++)
d->cylinder[i].type.description = copy_string(s->cylinder[i].type.description);
for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++)
d->weightsystem[i].description = copy_string(s->weightsystem[i].description);
STRUCTURED_LIST_COPY(struct picture, s->picture_list, d->picture_list, copy_pl);
STRUCTURED_LIST_COPY(struct tag_entry, s->tag_list, d->tag_list, copy_tl);
STRUCTURED_LIST_COPY(struct divecomputer, s->dc.next, d->dc.next, copy_dc);

View file

@ -23,7 +23,7 @@ void add_cylinder_description(cylinder_type_t *type)
return;
}
if (i < 100) {
tank_info[i].name = desc;
tank_info[i].name = strdup(desc);
tank_info[i].ml = type->size.mliter;
tank_info[i].bar = type->workingpressure.mbar / 1000;
}
@ -43,7 +43,7 @@ void add_weightsystem_description(weightsystem_t *weightsystem)
}
}
if (i < 100) {
ws_info[i].name = desc;
ws_info[i].name = strdup(desc);
ws_info[i].grams = weightsystem->weight.grams;
}
}

View file

@ -271,7 +271,7 @@ QWidget *TankInfoDelegate::createEditor(QWidget *parent, const QStyleOptionViewI
QWidget *delegate = ComboBoxDelegate::createEditor(parent, option, index);
CylindersModel *mymodel = qobject_cast<CylindersModel *>(currCombo.model);
cylinder_t *cyl = mymodel->cylinderAt(index);
currCylinderData.type = cyl->type.description;
currCylinderData.type = copy_string(cyl->type.description);
currCylinderData.pressure = cyl->type.workingpressure.mbar;
currCylinderData.size = cyl->type.size.mliter;
return delegate;
@ -323,7 +323,7 @@ QWidget *WSInfoDelegate::createEditor(QWidget *parent, const QStyleOptionViewIte
QWidget *editor = ComboBoxDelegate::createEditor(parent, option, index);
WeightModel *mymodel = qobject_cast<WeightModel *>(currCombo.model);
weightsystem_t *ws = mymodel->weightSystemAt(index);
currWeight.type = ws->description;
currWeight.type = copy_string(ws->description);
currWeight.weight = ws->weight.grams;
return editor;
}

View file

@ -632,7 +632,7 @@ bool WeightModel::setData(const QModelIndex &index, const QVariant &value, int r
int i = -1;
while (ws_info[++i].name) {
if (gettextFromC::instance()->tr(ws_info[i].name) == vString) {
ws->description = ws_info[i].name;
ws->description = copy_string(ws_info[i].name);
break;
}
}