Planner: correctly free divedatapoints

Simply setting the pointer to NULL leaks memory.
And that C++ recursive two function implementation... oh boy.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2014-08-19 11:41:09 -05:00
parent 6ed189f32c
commit 77f9bf06fd
4 changed files with 10 additions and 16 deletions

View file

@ -348,13 +348,17 @@ gas_error_exit:
return; return;
} }
void free_dps(struct divedatapoint *dp) void free_dps(struct diveplan *diveplan)
{ {
if (!diveplan)
return;
struct divedatapoint *dp = diveplan->dp;
while (dp) { while (dp) {
struct divedatapoint *ndp = dp->next; struct divedatapoint *ndp = dp->next;
free(dp); free(dp);
dp = ndp; dp = ndp;
} }
diveplan->dp = NULL;
} }
struct divedatapoint *create_dp(int time_incr, int depth, struct gasmix gasmix, int po2) struct divedatapoint *create_dp(int time_incr, int depth, struct gasmix gasmix, int po2)

View file

@ -20,6 +20,7 @@ extern void get_gas_at_time(struct dive *dive, struct divecomputer *dc, duration
extern int get_gasidx(struct dive *dive, struct gasmix *mix); extern int get_gasidx(struct dive *dive, struct gasmix *mix);
extern bool diveplan_empty(struct diveplan *diveplan); extern bool diveplan_empty(struct diveplan *diveplan);
extern void free_dps(struct diveplan *diveplan);
extern struct dive *planned_dive; extern struct dive *planned_dive;
extern char *cache_data; extern char *cache_data;
extern const char *disclaimer; extern const char *disclaimer;

View file

@ -1000,7 +1000,7 @@ void DivePlannerPointsModel::cancelPlan()
} }
} }
setPlanMode(NOTHING); setPlanMode(NOTHING);
diveplan.dp = NULL; free_dps(&diveplan);
emit planCanceled(); emit planCanceled();
} }
@ -1089,7 +1089,7 @@ void DivePlannerPointsModel::clear()
void DivePlannerPointsModel::createTemporaryPlan() void DivePlannerPointsModel::createTemporaryPlan()
{ {
// Get the user-input and calculate the dive info // Get the user-input and calculate the dive info
diveplan.dp = NULL; free_dps(&diveplan);
int lastIndex = -1; int lastIndex = -1;
for (int i = 0; i < rowCount(); i++) { for (int i = 0; i < rowCount(); i++) {
divedatapoint p = at(i); divedatapoint p = at(i);
@ -1137,17 +1137,7 @@ void DivePlannerPointsModel::createTemporaryPlan()
void DivePlannerPointsModel::deleteTemporaryPlan() void DivePlannerPointsModel::deleteTemporaryPlan()
{ {
deleteTemporaryPlan(diveplan.dp); free_dps(&diveplan);
diveplan.dp = NULL;
}
void DivePlannerPointsModel::deleteTemporaryPlan(struct divedatapoint *dp)
{
if (!dp)
return;
deleteTemporaryPlan(dp->next);
free(dp);
} }
void DivePlannerPointsModel::createPlan() void DivePlannerPointsModel::createPlan()
@ -1166,7 +1156,7 @@ void DivePlannerPointsModel::createPlan()
// Remove and clean the diveplan, so we don't delete // Remove and clean the diveplan, so we don't delete
// the dive by mistake. // the dive by mistake.
diveplan.dp = NULL; free_dps(&diveplan);
setPlanMode(NOTHING); setPlanMode(NOTHING);
planCreated(); planCreated();
} }

View file

@ -101,7 +101,6 @@ private:
Mode mode; Mode mode;
bool recalc; bool recalc;
QVector<divedatapoint> divepoints; QVector<divedatapoint> divepoints;
void deleteTemporaryPlan(struct divedatapoint *dp);
QVector<sample> backupSamples; // For editing added dives. QVector<sample> backupSamples; // For editing added dives.
QVector<QPair<int, int> > oldGases; QVector<QPair<int, int> > oldGases;
QDateTime startTime; QDateTime startTime;