Cleanup: set description to null in free_[weightsystem|cylinder]

Currently, the caller is responsible for not reusing a freed
weightsystem / cylinder or resetting the description field to
null. This is very unfriendly. Set the description field to null,
because that allows us to call free_* repeatedly on the same
object. Use the new behavior to make the weightsystem model code
a bit cleaner.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-11-24 09:27:50 +01:00 committed by Dirk Hohndel
parent a4c95fd8e8
commit e114522a44
2 changed files with 3 additions and 2 deletions

View file

@ -26,11 +26,13 @@
void free_weightsystem(weightsystem_t ws) void free_weightsystem(weightsystem_t ws)
{ {
free((void *)ws.description); free((void *)ws.description);
ws.description = NULL;
} }
static void free_cylinder(cylinder_t c) static void free_cylinder(cylinder_t c)
{ {
free((void *)c.type.description); free((void *)c.type.description);
c.type.description = NULL;
} }
void copy_weights(const struct weightsystem_table *s, struct weightsystem_table *d) void copy_weights(const struct weightsystem_table *s, struct weightsystem_table *d)

View file

@ -82,14 +82,12 @@ void WeightModel::setTempWS(int row, weightsystem_t ws)
return; return;
clearTempWS(); // Shouldn't be necessary, just in case: Reset old temporary row. clearTempWS(); // Shouldn't be necessary, just in case: Reset old temporary row.
free_weightsystem(tempWS);
// It is really hard to get the editor-close-hints and setModelData calls under // It is really hard to get the editor-close-hints and setModelData calls under
// control. Therefore, if the row is set to the already existing entry, don't // control. Therefore, if the row is set to the already existing entry, don't
// enter temporary mode. // enter temporary mode.
if (same_string(d->weightsystems.weightsystems[row].description, ws.description)) { if (same_string(d->weightsystems.weightsystems[row].description, ws.description)) {
free_weightsystem(ws); free_weightsystem(ws);
tempWS.description = nullptr;
} else { } else {
tempRow = row; tempRow = row;
tempWS = ws; tempWS = ws;
@ -103,6 +101,7 @@ void WeightModel::clearTempWS()
return; return;
int oldRow = tempRow; int oldRow = tempRow;
tempRow = -1; tempRow = -1;
free_weightsystem(tempWS);
dataChanged(index(oldRow, TYPE), index(oldRow, WEIGHT)); dataChanged(index(oldRow, TYPE), index(oldRow, WEIGHT));
} }