aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2018-09-27 21:55:03 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2018-09-29 15:24:08 -0700
commitd3d06bc580b2a344806396622edd6b7adbdd08cc (patch)
treea01e60a760532364e017b9a97e3e3d62c874e285
parent574065b31430245a66808561b1b95139c4cd19f2 (diff)
downloadsubsurface-d3d06bc580b2a344806396622edd6b7adbdd08cc.tar.gz
Cleanup: split out free_dive() function from delete_single_dive()
Currently, we can only delete dives that are indexed in the main dive table. In the future, we will have to delete dives outside of this table (e.g. for undo). Therefore, split out the free_dive() function from delete_single_dive(), which takes an index into the main dive table. In the process, adopt the dive freeing-code from clear_dive(), which frees more data than the code in delete_single_dive(). This potentially fixes a memory-leak. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r--core/dive.c23
-rw-r--r--core/dive.h1
-rw-r--r--core/divelist.c9
3 files changed, 20 insertions, 13 deletions
diff --git a/core/dive.c b/core/dive.c
index 0c185efa5..83789f686 100644
--- a/core/dive.c
+++ b/core/dive.c
@@ -528,11 +528,7 @@ static void copy_tl(struct tag_entry *st, struct tag_entry *dt)
*_dptr = 0; \
}
-/* copy_dive makes duplicates of many components of a dive;
- * in order not to leak memory, we need to free those .
- * copy_dive doesn't play with the divetrip and forward/backward pointers
- * so we can ignore those */
-void clear_dive(struct dive *d)
+static void free_dive_structures(struct dive *d)
{
if (!d)
return;
@@ -550,6 +546,23 @@ void clear_dive(struct dive *d)
free((void *)d->cylinder[i].type.description);
for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++)
free((void *)d->weightsystem[i].description);
+}
+
+void free_dive(struct dive *d)
+{
+ free_dive_structures(d);
+ free(d);
+}
+
+/* copy_dive makes duplicates of many components of a dive;
+ * in order not to leak memory, we need to free those .
+ * copy_dive doesn't play with the divetrip and forward/backward pointers
+ * so we can ignore those */
+void clear_dive(struct dive *d)
+{
+ if (!d)
+ return;
+ free_dive_structures(d);
memset(d, 0, sizeof(struct dive));
}
diff --git a/core/dive.h b/core/dive.h
index f8ccbf824..ccc15c782 100644
--- a/core/dive.h
+++ b/core/dive.h
@@ -544,6 +544,7 @@ extern timestamp_t utc_mktime(struct tm *tm);
extern void utc_mkdate(timestamp_t, struct tm *tm);
extern struct dive *alloc_dive(void);
+extern void free_dive(struct dive *);
extern void record_dive_to_table(struct dive *dive, struct dive_table *table);
extern void record_dive(struct dive *dive);
extern void clear_dive(struct dive *dive);
diff --git a/core/divelist.c b/core/divelist.c
index 62af3120c..84290e6b6 100644
--- a/core/divelist.c
+++ b/core/divelist.c
@@ -931,14 +931,7 @@ void delete_single_dive(int idx)
for (i = idx; i < dive_table.nr - 1; i++)
dive_table.dives[i] = dive_table.dives[i + 1];
dive_table.dives[--dive_table.nr] = NULL;
- /* free all allocations */
- free(dive->dc.sample);
- free((void *)dive->notes);
- free((void *)dive->divemaster);
- free((void *)dive->buddy);
- free((void *)dive->suit);
- taglist_free(dive->tag_list);
- free(dive);
+ free_dive(dive);
}
struct dive **grow_dive_table(struct dive_table *table)