From 37910751a005b9b28edcdf3170514c560af04f6c Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Tue, 3 Jun 2014 18:12:24 -0700 Subject: Dive edit: avoid memory leaks If we edit any of these fields, we create new strings via strdup (or a fresh tag_list). So if the edits are rejected, free all that memory. Signed-off-by: Dirk Hohndel --- qt-ui/maintab.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp index 9200f28f5..442136d7f 100644 --- a/qt-ui/maintab.cpp +++ b/qt-ui/maintab.cpp @@ -778,6 +778,10 @@ void MainTab::resetPallete() free(what); \ what = strdup(textByteArray.data()); +#define FREE_IF_DIFFERENT(what) \ + if (editedDive.what != cd->what) \ + free(editedDive.what) + void MainTab::rejectChanges() { EditMode lastMode = editMode; @@ -814,6 +818,14 @@ void MainTab::rejectChanges() setEnabled(false); } } + // now let's avoid memory leaks + struct dive *cd = current_dive; + FREE_IF_DIFFERENT(tag_list); + FREE_IF_DIFFERENT(location); + FREE_IF_DIFFERENT(buddy); + FREE_IF_DIFFERENT(divemaster); + FREE_IF_DIFFERENT(notes); + FREE_IF_DIFFERENT(suit); hideMessage(); MainWindow::instance()->dive_list()->setEnabled(true); -- cgit v1.2.3-70-g09d2