summaryrefslogtreecommitdiffstats
path: root/dive.c
diff options
context:
space:
mode:
authorGravatar Lubomir I. Ivanov <neolit123@gmail.com>2012-12-24 03:53:25 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2012-12-27 08:08:59 -0800
commit906fd400e774e3db5ecdfd4956406d8396387942 (patch)
treee0d19c0b78cbc79d2c0402affac64683f18384d6 /dive.c
parentcc3b87c04492051146c01e4339f09065ef754db2 (diff)
downloadsubsurface-906fd400e774e3db5ecdfd4956406d8396387942.tar.gz
Small changes in the memory management when dive-merging
This patch makes a couple of modifications: 1) divelist.c:delete_single_dive() now tries to free all memory associated with a dive, such as the string values for divemaster, location, notes & etc. 2) dive.c:merge_text(), now always makes a copy in memory for the returned string - either combined or one of the two which are passed to the function. The reason for the above two changes is that when (say) importing the same data over and over, technically a merge will occur for the contained dives, but mapped pointers can go out of scope. main.c:report_dives() calls try_to_merge() and if succeeds the two dives that were merged are deleted from the table. when we delete a dive, we now make sure all string data is cleared with it, but also in the actual merge itself, which precedes, copies of the merged texts are made (with merge_text()), so that the new, resulted dive has his own text allocations. Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'dive.c')
-rw-r--r--dive.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/dive.c b/dive.c
index a59bb5fc0..b5fb1382e 100644
--- a/dive.c
+++ b/dive.c
@@ -734,13 +734,14 @@ add_sample_b:
static char *merge_text(const char *a, const char *b)
{
char *res;
-
+ if (!a && !b)
+ return NULL;
if (!a || !*a)
- return (char *)b;
+ return strdup(b);
if (!b || !*b)
- return (char *)a;
+ return strdup(a);
if (!strcmp(a,b))
- return (char *)a;
+ return strdup(a);
res = malloc(strlen(a) + strlen(b) + 9);
if (!res)
return (char *)a;