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>
This commit is contained in:
Lubomir I. Ivanov 2012-12-24 03:53:25 +02:00 committed by Dirk Hohndel
parent cc3b87c044
commit 906fd400e7
2 changed files with 16 additions and 4 deletions

9
dive.c
View file

@ -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;

View file

@ -1947,7 +1947,18 @@ void delete_single_dive(int idx)
dive_table.nr--;
if (dive->selected)
amount_selected--;
/* free all allocations */
free(dive->dc.sample);
if (dive->location)
free((void *)dive->location);
if (dive->notes)
free((void *)dive->notes);
if (dive->divemaster)
free((void *)dive->divemaster);
if (dive->buddy)
free((void *)dive->buddy);
if (dive->suit)
free((void *)dive->suit);
free(dive);
}