Everytime a mailbox is closed, the context is freed right afterwards.
Also, as mx_open_mailbox is responsible for allocating the context, it
makes sense for symmetry to let mx_(fast)close_mailbox free it. That
way, the context is only valid between calls to open and close mailbox.
---
 attach.c       |  6 ++----
 buffy.c        |  3 +--
 commands.c     |  9 +++------
 compose.c      |  8 +++-----
 curs_main.c    | 11 ++++-------
 editmsg.c      |  9 +++------
 imap/command.c |  2 +-
 imap/imap.c    |  3 +--
 mailbox.h      |  4 ++--
 mbox.c         | 12 ++++++------
 mx.c           | 55 ++++++++++++++++++++++++++++++-------------------------
 pop.c          |  6 ++----
 postpone.c     | 16 +++++-----------
 sendlib.c      | 12 ++++--------
 14 files changed, 67 insertions(+), 89 deletions(-)

diff --git a/attach.c b/attach.c
index a8290e3..c898d40 100644
--- a/attach.c
+++ b/attach.c
@@ -734,8 +734,7 @@ int mutt_save_attachment (FILE *fp, BODY *m, char *path, 
int flags, HEADER *hdr)
        return -1;
       if ((msg = mx_open_new_message (ctx, hn, is_from (buf, NULL, 0, NULL) ? 
0 : MUTT_ADD_FROM)) == NULL)
       {
-       mx_close_mailbox(ctx, NULL);
-       FREE (&ctx);
+       mx_close_mailbox(&ctx, NULL);
        return -1;
       }
       if (ctx->magic == MUTT_MBOX || ctx->magic == MUTT_MMDF)
@@ -748,8 +747,7 @@ int mutt_save_attachment (FILE *fp, BODY *m, char *path, 
int flags, HEADER *hdr)
        r = -1;
 
       mx_close_message (ctx, &msg);
-      mx_close_mailbox (ctx, NULL);
-      FREE (&ctx);
+      mx_close_mailbox (&ctx, NULL);
       return r;
     }
     else
diff --git a/buffy.c b/buffy.c
index 87573f2..e0d5deb 100644
--- a/buffy.c
+++ b/buffy.c
@@ -455,8 +455,7 @@ static int buffy_mbox_check (BUFFY* mailbox, struct stat 
*sb, int check_stats)
       mailbox->msg_unread      = ctx->unread;
       mailbox->msg_flagged     = ctx->flagged;
       mailbox->stats_last_checked = ctx->mtime;
-      mx_close_mailbox (ctx, 0);
-      FREE (&ctx);
+      mx_close_mailbox (&ctx, 0);
     }
   }
 
diff --git a/commands.c b/commands.c
index d67d4d9..dca1363 100644
--- a/commands.c
+++ b/commands.c
@@ -842,8 +842,7 @@ int mutt_save_message (HEADER *h, int delete,
     {
       if (_mutt_save_message(h, ctx, delete, decode, decrypt) != 0)
       {
-        mx_close_mailbox (ctx, NULL);
-        FREE (&ctx);
+        mx_close_mailbox (&ctx, NULL);
         return -1;
       }
     }
@@ -857,8 +856,7 @@ int mutt_save_message (HEADER *h, int delete,
          if (_mutt_save_message(Context->hdrs[Context->v2r[i]],
                             ctx, delete, decode, decrypt) != 0)
           {
-            mx_close_mailbox (ctx, NULL);
-            FREE (&ctx);
+            mx_close_mailbox (&ctx, NULL);
             return -1;
           }
        }
@@ -867,8 +865,7 @@ int mutt_save_message (HEADER *h, int delete,
 
     need_buffy_cleanup = (ctx->magic == MUTT_MBOX || ctx->magic == MUTT_MMDF);
 
-    mx_close_mailbox (ctx, NULL);
-    FREE (&ctx);
+    mx_close_mailbox (&ctx, NULL);
 
     if (need_buffy_cleanup)
       mutt_buffy_cleanup (buf, &st);
diff --git a/compose.c b/compose.c
index 27bc624..c816537 100644
--- a/compose.c
+++ b/compose.c
@@ -766,8 +766,7 @@ int mutt_compose_menu (HEADER *msg,   /* structure for new 
message */
 
          if (!ctx->msgcount)
          {
-           mx_close_mailbox (ctx, NULL);
-           FREE (&ctx);
+           mx_close_mailbox (&ctx, NULL);
            mutt_error _("No messages in that folder.");
            break;
          }
@@ -817,10 +816,9 @@ int mutt_compose_menu (HEADER *msg,   /* structure for new 
message */
          menu->redraw |= REDRAW_FULL;
 
          if (close == OP_QUIT) 
-           mx_close_mailbox (Context, NULL);
+           mx_close_mailbox (&Context, NULL);
          else
-           mx_fastclose_mailbox (Context);
-         FREE (&Context);
+           mx_fastclose_mailbox (&Context);
 
          /* go back to the folder we started from */
          Context = this;
diff --git a/curs_main.c b/curs_main.c
index 5310f6b..01442d8 100644
--- a/curs_main.c
+++ b/curs_main.c
@@ -964,7 +964,7 @@ int mutt_index_menu (void)
 
          oldcount = Context ? Context->msgcount : 0;
 
-         if (!Context || (check = mx_close_mailbox (Context, &index_hint)) == 
0)
+         if (!Context || (check = mx_close_mailbox (&Context, &index_hint)) == 
0)
            done = 1;
          else
          {
@@ -1085,13 +1085,12 @@ int mutt_index_menu (void)
       case OP_MAIN_IMAP_LOGOUT_ALL:
        if (Context && Context->magic == MUTT_IMAP)
        {
-         if (mx_close_mailbox (Context, &index_hint) != 0)
+         if (mx_close_mailbox (&Context, &index_hint) != 0)
          {
            set_option (OPTSEARCHINVALID);
            menu->redraw = REDRAW_FULL;
            break;
          }
-         FREE (&Context);
        }
        imap_logout_all();
        mutt_message _("Logged out of IMAP servers.");
@@ -1244,7 +1243,7 @@ int mutt_index_menu (void)
          mutt_str_replace (&LastFolder, Context->path);
          oldcount = Context ? Context->msgcount : 0;
 
-         if ((check = mx_close_mailbox (Context, &index_hint)) != 0)
+         if ((check = mx_close_mailbox (&Context, &index_hint)) != 0)
          {
            if (check == MUTT_NEW_MAIL || check == MUTT_REOPENED)
              update_index (menu, Context, check, oldcount, index_hint);
@@ -1253,7 +1252,6 @@ int mutt_index_menu (void)
            menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
            break;
          }
-         FREE (&Context);
        }
 
         mutt_sleep (0);
@@ -1336,8 +1334,7 @@ int mutt_index_menu (void)
        {
          if (Context)
          {
-           mx_fastclose_mailbox (Context);
-           FREE (&Context);
+           mx_fastclose_mailbox (&Context);
          }
          done = 1;
        }
diff --git a/editmsg.c b/editmsg.c
index 35cda42..00dd798 100644
--- a/editmsg.c
+++ b/editmsg.c
@@ -83,8 +83,7 @@ static int edit_one_message (CONTEXT *ctx, HEADER *cur)
        ((ctx->magic == MUTT_MBOX || ctx->magic == MUTT_MMDF) ? 0 : 
CH_NOSTATUS));
   oerrno = errno;
 
-  mx_close_mailbox (tmpctx, NULL);
-  FREE (&tmpctx);
+  mx_close_mailbox (&tmpctx, NULL);
 
   if (rc == -1)
   {
@@ -178,8 +177,7 @@ static int edit_one_message (CONTEXT *ctx, HEADER *cur)
   if (msg == NULL)
   {
     mutt_error (_("Can't append to folder: %s"), strerror (errno));
-    mx_close_mailbox (tmpctx, NULL);
-    FREE (&tmpctx);
+    mx_close_mailbox (&tmpctx, NULL);
     goto bail;
   }
 
@@ -192,8 +190,7 @@ static int edit_one_message (CONTEXT *ctx, HEADER *cur)
   rc = mx_commit_message (msg, tmpctx);
   mx_close_message (tmpctx, &msg);
   
-  mx_close_mailbox (tmpctx, NULL);
-  FREE (&tmpctx);
+  mx_close_mailbox (&tmpctx, NULL);
   
   bail:
   if (fp) safe_fclose (&fp);
diff --git a/imap/command.c b/imap/command.c
index 4d34d24..d502ae4 100644
--- a/imap/command.c
+++ b/imap/command.c
@@ -442,7 +442,7 @@ static void cmd_handle_fatal (IMAP_DATA* idata)
   if ((idata->state >= IMAP_SELECTED) &&
       (idata->reopen & IMAP_REOPEN_ALLOW))
   {
-    mx_fastclose_mailbox (idata->ctx);
+    mx_fastclose_mailbox (&idata->ctx);
     mutt_error (_("Mailbox closed"));
     mutt_sleep (1);
     idata->state = IMAP_DISCONNECTED;
diff --git a/imap/imap.c b/imap/imap.c
index 085813b..cdc04fe 100644
--- a/imap/imap.c
+++ b/imap/imap.c
@@ -1353,8 +1353,7 @@ int imap_sync_mailbox (CONTEXT* ctx, int expunge, int* 
index_hint)
  out:
   if (appendctx)
   {
-    mx_fastclose_mailbox (appendctx);
-    FREE (&appendctx);
+    mx_fastclose_mailbox (&appendctx);
   }
   return rc;
 }
diff --git a/mailbox.h b/mailbox.h
index cab24e6..9ba4176 100644
--- a/mailbox.h
+++ b/mailbox.h
@@ -63,9 +63,9 @@ CONTEXT *mx_open_mailbox (const char *, int);
 MESSAGE *mx_open_message (CONTEXT *, int);
 MESSAGE *mx_open_new_message (CONTEXT *, HEADER *, int);
 
-void mx_fastclose_mailbox (CONTEXT *);
+void mx_fastclose_mailbox (CONTEXT **);
 
-int mx_close_mailbox (CONTEXT *, int *);
+int mx_close_mailbox (CONTEXT **, int *);
 int mx_sync_mailbox (CONTEXT *, int *);
 int mx_commit_message (MESSAGE *, CONTEXT *);
 int mx_close_message (CONTEXT *, MESSAGE **);
diff --git a/mbox.c b/mbox.c
index d3c2ec4..538c982 100644
--- a/mbox.c
+++ b/mbox.c
@@ -743,7 +743,7 @@ static int mbox_check_mailbox (CONTEXT *ctx, int 
*index_hint)
   /* fatal error */
 
   mbox_unlock_mailbox (ctx);
-  mx_fastclose_mailbox (ctx);
+  mx_fastclose_mailbox (&ctx);
   mutt_unblock_signals ();
   mutt_error _("Mailbox was corrupted!");
   return (-1);
@@ -825,7 +825,7 @@ int mbox_sync_mailbox (CONTEXT *ctx, int *index_hint)
    */
   if ((ctx->fp = freopen (ctx->path, "r+", ctx->fp)) == NULL)
   {
-    mx_fastclose_mailbox (ctx);
+    mx_fastclose_mailbox (&ctx);
     mutt_error _("Fatal error!  Could not reopen mailbox!");
     return (-1);
   }
@@ -1007,7 +1007,7 @@ int mbox_sync_mailbox (CONTEXT *ctx, int *index_hint)
   if ((fp = fopen (tempfile, "r")) == NULL)
   {
     mutt_unblock_signals ();
-    mx_fastclose_mailbox (ctx);
+    mx_fastclose_mailbox (&ctx);
     dprint (1, (debugfile, "mbox_sync_mailbox: unable to reopen temp copy of 
mailbox!\n"));
     mutt_perror (tempfile);
     mutt_sleep (5);
@@ -1070,7 +1070,7 @@ int mbox_sync_mailbox (CONTEXT *ctx, int *index_hint)
              NONULL (Tempdir), NONULL(Username), NONULL(Hostname), (unsigned 
int)getpid ());
     rename (tempfile, savefile);
     mutt_unblock_signals ();
-    mx_fastclose_mailbox (ctx);
+    mx_fastclose_mailbox (&ctx);
     mutt_pretty_mailbox (savefile, sizeof (savefile));
     mutt_error (_("Write failed!  Saved partial mailbox to %s"), savefile);
     mutt_sleep (5);
@@ -1085,7 +1085,7 @@ int mbox_sync_mailbox (CONTEXT *ctx, int *index_hint)
   {
     unlink (tempfile);
     mutt_unblock_signals ();
-    mx_fastclose_mailbox (ctx);
+    mx_fastclose_mailbox (&ctx);
     mutt_error _("Fatal error!  Could not reopen mailbox!");
     return (-1);
   }
@@ -1135,7 +1135,7 @@ bail:  /* Come here in case of disaster */
   if ((ctx->fp = freopen (ctx->path, "r", ctx->fp)) == NULL)
   {
     mutt_error _("Could not reopen mailbox!");
-    mx_fastclose_mailbox (ctx);
+    mx_fastclose_mailbox (&ctx);
     return (-1);
   }
 
diff --git a/mx.c b/mx.c
index 75d414c..8d6e1dc 100644
--- a/mx.c
+++ b/mx.c
@@ -515,11 +515,13 @@ static int mx_open_mailbox_append (CONTEXT *ctx, int 
flags)
 }
 
 /* close a mailbox opened in write-mode */
-static int mx_close_mailbox_append (CONTEXT *ctx)
+static int mx_close_mailbox_append (CONTEXT **pctx)
 {
+  CONTEXT *ctx = *pctx;
+
   mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
   mutt_unblock_signals ();
-  mx_fastclose_mailbox (ctx);
+  mx_fastclose_mailbox (pctx);
   return 0;
 }
 
@@ -560,8 +562,7 @@ CONTEXT *mx_open_mailbox (const char *path, int flags)
   {
     if (mx_open_mailbox_append (ctx, flags) != 0)
     {
-      mx_fastclose_mailbox (ctx);
-      FREE (&ctx);
+      mx_fastclose_mailbox (&ctx);
       return NULL;
     }
     return ctx;
@@ -577,8 +578,7 @@ CONTEXT *mx_open_mailbox (const char *path, int flags)
     else if (ctx->magic == -1)
       mutt_perror(path);
 
-    mx_fastclose_mailbox (ctx);
-    FREE (&ctx);
+    mx_fastclose_mailbox (&ctx);
     return (NULL);
   }
   
@@ -609,8 +609,7 @@ CONTEXT *mx_open_mailbox (const char *path, int flags)
   }
   else
   {
-    mx_fastclose_mailbox (ctx);
-    FREE (&ctx);
+    mx_fastclose_mailbox (&ctx);
   }
 
   unset_option (OPTFORCEREFRESH);
@@ -618,11 +617,16 @@ CONTEXT *mx_open_mailbox (const char *path, int flags)
 }
 
 /* free up memory associated with the mailbox context */
-void mx_fastclose_mailbox (CONTEXT *ctx)
+void mx_fastclose_mailbox (CONTEXT **pctx)
 {
   int i;
   struct utimbuf ut;
+  CONTEXT *ctx;
+
+  if (!pctx)
+    return;
 
+  ctx = *pctx;
   if(!ctx) 
     return;
 
@@ -656,7 +660,7 @@ void mx_fastclose_mailbox (CONTEXT *ctx)
   if (ctx->limit_pattern) 
     mutt_pattern_free (&ctx->limit_pattern);
   safe_fclose (&ctx->fp);
-  memset (ctx, 0, sizeof (CONTEXT));
+  FREE (pctx);        /* __FREE_CHECKED__ */
 }
 
 /* save changes to disk */
@@ -758,14 +762,12 @@ static int trash_append (CONTEXT *ctx)
       {
         if (mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1)
         {
-          mx_close_mailbox (ctx_trash, NULL);
-          FREE (&ctx_trash);
+          mx_close_mailbox (&ctx_trash, NULL);
           return -1;
         }
       }
 
-    mx_close_mailbox (ctx_trash, NULL);
-    FREE (&ctx_trash);
+    mx_close_mailbox (&ctx_trash, NULL);
   }
   else
   {
@@ -777,14 +779,19 @@ static int trash_append (CONTEXT *ctx)
 }
 
 /* save changes and close mailbox */
-int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
+int mx_close_mailbox (CONTEXT **pctx, int *index_hint)
 {
   int i, move_messages = 0, purge = 1, read_msgs = 0;
+  CONTEXT *ctx;
   int check;
   int isSpool = 0;
   char mbox[_POSIX_PATH_MAX];
   char buf[SHORT_STRING];
 
+  if (!pctx)
+    return 0;
+
+  ctx = *pctx;
   if (!ctx) return 0;
 
   ctx->closing = 1;
@@ -792,7 +799,7 @@ int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
   if (ctx->readonly || ctx->dontwrite)
   {
     /* mailbox is readonly or we don't want to write */
-    mx_fastclose_mailbox (ctx);
+    mx_fastclose_mailbox (pctx);
     return 0;
   }
 
@@ -800,9 +807,9 @@ int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
   {
     /* mailbox was opened in write-mode */
     if (ctx->magic == MUTT_MBOX || ctx->magic == MUTT_MMDF)
-      mx_close_mailbox_append (ctx);
+      mx_close_mailbox_append (pctx);
     else
-      mx_fastclose_mailbox (ctx);
+      mx_fastclose_mailbox (pctx);
     return 0;
   }
 
@@ -923,16 +930,14 @@ int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
          }
          else
          {
-           mx_close_mailbox (f, NULL);
-           FREE (&f);
+           mx_close_mailbox (&f, NULL);
            ctx->closing = 0;
            return -1;
          }
        }
       }
     
-      mx_close_mailbox (f, NULL);
-      FREE (&f);
+      mx_close_mailbox (&f, NULL);
     }
     
   }
@@ -942,7 +947,7 @@ int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
       mutt_message _("Mailbox is unchanged.");
     if (ctx->magic == MUTT_MBOX || ctx->magic == MUTT_MMDF)
       mbox_reset_atime (ctx, NULL);
-    mx_fastclose_mailbox (ctx);
+    mx_fastclose_mailbox (pctx);
     return 0;
   }
 
@@ -1009,7 +1014,7 @@ int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
   mutt_sb_set_buffystats (ctx);
 #endif
 
-  mx_fastclose_mailbox (ctx);
+  mx_fastclose_mailbox (pctx);
 
   return 0;
 }
@@ -1206,7 +1211,7 @@ int mx_sync_mailbox (CONTEXT *ctx, int *index_hint)
        !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
     {
       unlink (ctx->path);
-      mx_fastclose_mailbox (ctx);
+      mx_fastclose_mailbox (&ctx);
       return 0;
     }
 
diff --git a/pop.c b/pop.c
index 531b848..b9b2975 100644
--- a/pop.c
+++ b/pop.c
@@ -894,8 +894,7 @@ void pop_fetch_mail (void)
 
     if (ret == -1)
     {
-      mx_close_mailbox (ctx, NULL);
-      FREE (&ctx);
+      mx_close_mailbox (&ctx, NULL);
       goto fail;
     }
     if (ret == -2)
@@ -912,8 +911,7 @@ void pop_fetch_mail (void)
     mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last, msgs - 
last);
   }
 
-  mx_close_mailbox (ctx, NULL);
-  FREE (&ctx);
+  mx_close_mailbox (&ctx, NULL);
 
   if (rset)
   {
diff --git a/postpone.c b/postpone.c
index fc0fe83..2255874 100644
--- a/postpone.c
+++ b/postpone.c
@@ -135,8 +135,7 @@ int mutt_num_postponed (int force)
       PostCount = 0;
     else
       PostCount = ctx->msgcount;
-    mx_fastclose_mailbox (ctx);
-    FREE (&ctx);
+    mx_fastclose_mailbox (&ctx);
   }
 
   return (PostCount);
@@ -253,8 +252,7 @@ int mutt_get_postponed (CONTEXT *ctx, HEADER *hdr, HEADER 
**cur, char *fcc, size
   if (! PostContext->msgcount)
   {
     PostCount = 0;
-    mx_close_mailbox (PostContext, NULL);
-    FREE (&PostContext);
+    mx_close_mailbox (&PostContext, NULL);
     mutt_error _("No postponed messages.");
     return (-1);
   }
@@ -266,15 +264,13 @@ int mutt_get_postponed (CONTEXT *ctx, HEADER *hdr, HEADER 
**cur, char *fcc, size
   }
   else if ((h = select_msg ()) == NULL)
   {
-    mx_close_mailbox (PostContext, NULL);
-    FREE (&PostContext);
+    mx_close_mailbox (&PostContext, NULL);
     return (-1);
   }
 
   if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0)
   {
-    mx_fastclose_mailbox (PostContext);
-    FREE (&PostContext);
+    mx_fastclose_mailbox (&PostContext);
     return (-1);
   }
 
@@ -288,11 +284,9 @@ int mutt_get_postponed (CONTEXT *ctx, HEADER *hdr, HEADER 
**cur, char *fcc, size
   /* avoid the "purge deleted messages" prompt */
   opt_delete = quadoption (OPT_DELETE);
   set_quadoption (OPT_DELETE, MUTT_YES);
-  mx_close_mailbox (PostContext, NULL);
+  mx_close_mailbox (&PostContext, NULL);
   set_quadoption (OPT_DELETE, opt_delete);
 
-  FREE (&PostContext);
-
   for (tmp = hdr->env->userhdrs; tmp; )
   {
     if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0)
diff --git a/sendlib.c b/sendlib.c
index 7e02d43..bedc8e7 100644
--- a/sendlib.c
+++ b/sendlib.c
@@ -2721,8 +2721,7 @@ int mutt_write_fcc (const char *path, HEADER *hdr, const 
char *msgid, int post,
     if ((tempfp = safe_fopen (tempfile, "w+")) == NULL)
     {
       mutt_perror (tempfile);
-      mx_close_mailbox (f, NULL);
-      FREE (&f);
+      mx_close_mailbox (&f, NULL);
       return (-1);
     }
     /* remember new mail status before appending message */
@@ -2736,8 +2735,7 @@ int mutt_write_fcc (const char *path, HEADER *hdr, const 
char *msgid, int post,
     onm_flags |= MUTT_SET_DRAFT;
   if ((msg = mx_open_new_message (f, hdr, onm_flags)) == NULL)
   {
-    mx_close_mailbox (f, NULL);
-    FREE (&f);
+    mx_close_mailbox (&f, NULL);
     return (-1);
   }
 
@@ -2854,8 +2852,7 @@ int mutt_write_fcc (const char *path, HEADER *hdr, const 
char *msgid, int post,
       unlink (tempfile);
       mx_commit_message (msg, f);      /* XXX - really? */
       mx_close_message (f, &msg);
-      mx_close_mailbox (f, NULL);
-      FREE (&f);
+      mx_close_mailbox (&f, NULL);
       return -1;
     }
 
@@ -2884,8 +2881,7 @@ int mutt_write_fcc (const char *path, HEADER *hdr, const 
char *msgid, int post,
   if (mx_commit_message (msg, f) != 0)
     r = -1;
   mx_close_message (f, &msg);
-  mx_close_mailbox (f, NULL);
-  FREE (&f);
+  mx_close_mailbox (&f, NULL);
 
   if (!post && need_buffy_cleanup)
     mutt_buffy_cleanup (path, &st);
-- 
2.9.3

Reply via email to