[Home]

Summary:ASTERISK-09311: Memory leak problem in 'inboxcount' function when using IMAP/Realtime for Voicemail
Reporter:rayjay (rayjay)Labels:
Date Opened:2007-04-25 05:10:59Date Closed:2007-05-01 16:42:15
Priority:MinorRegression?No
Status:Closed/CompleteComponents:Applications/app_voicemail
Versions:Frequency of
Occurrence
Related
Issues:
Environment:Attachments:
Description:I believe I have found a memory leak problem in the 'inboxcount' function when you configure Asterisk to use IMAP for Voicemail storage and also have * Realtime configured for voicemail configuration. There is a call to find_user in the 'inboxcount' function "vmu = find_user(NULL, context, mailboxnc)" which appears to allocate an 'ast_vm_user struct' amount of memory but does not free that memory after use.  In my setup with 100 or so extensions I managed to grow my Asterisk process to 1.1GB in 7 days thanks to this leak.

I believe I have stopped the leak by inserting a 'free' statement for the vmu variable before doing each return.  I am fairly new to Asterisk, so I apologise if this is wrong or there is a proper way to do this (free_user function?).  It does appear to have stopped the leak though and my Voicemail still seems to work fine.  Rather than submit an official patch I have provided a simple 'diff -C3' below to illustrate what I have done.  I do not feel comfortable submitting an official patch yet as I am still a novice finding my way around the app_voicemail.c code!

--

$ diff -C3 app_voicemail.c.orig app_voicemail.c

***************
*** 2602,2607 ****
--- 2602,2608 ----
       /* No IMAP account available */
       if (vmu->imapuser[0] == '\0') {
               ast_log (LOG_WARNING,"IMAP user not set for mailbox %s\n",vmu->mailbox);
+               free(vmu);
               return -1;
       }

***************
*** 2611,2616 ****
--- 2612,2618 ----
                       ast_log (LOG_DEBUG,"Returning before search - user is logged in\n");
               *newmsgs = vms_p->newmessages;
               *oldmsgs = vms_p->oldmessages;
+               free(vmu);
               return 0;
       }

***************
*** 2618,2625 ****
       if (!(vms_p = get_vm_state_by_imapuser(vmu->imapuser, 0)) && !(vms_p = get_vm_state_by_mailbox(mailboxnc, 0))) {
               if(option_debug > 2)
                       ast_log (LOG_DEBUG,"Adding new vmstate for %s\n",vmu->imapuser);
!               if (!(vms_p = ast_calloc(1, sizeof(*vms_p))))
                       return -1;
               ast_copy_string(vms_p->imapuser,vmu->imapuser, sizeof(vms_p->imapuser));
               ast_copy_string(vms_p->username, mailboxnc, sizeof(vms_p->username)); /* save for access from interactive entry point */
               vms_p->mailstream = NIL; /* save for access from interactive entry point */
--- 2620,2629 ----
       if (!(vms_p = get_vm_state_by_imapuser(vmu->imapuser, 0)) && !(vms_p = get_vm_state_by_mailbox(mailboxnc, 0))) {
               if(option_debug > 2)
                       ast_log (LOG_DEBUG,"Adding new vmstate for %s\n",vmu->imapuser);
!               if (!(vms_p = ast_calloc(1, sizeof(*vms_p)))) {
!                       free(vmu);
                       return -1;
+               }
               ast_copy_string(vms_p->imapuser,vmu->imapuser, sizeof(vms_p->imapuser));
               ast_copy_string(vms_p->username, mailboxnc, sizeof(vms_p->username)); /* save for access from interactive entry point */
               vms_p->mailstream = NIL; /* save for access from interactive entry point */
***************
*** 2636,2641 ****
--- 2640,2646 ----
       ret = init_mailstream(vms_p, 0);
       if (!vms_p->mailstream) {
               ast_log (LOG_ERROR,"Houston we have a problem - IMAP mailstream is NULL\n");
+               free(vmu);
               return -1;
       }

***************
*** 2680,2686 ****
               *newmsgs = vms_p->newmessages;
               *oldmsgs = vms_p->oldmessages;
       }
!
       return 0;
  }

--- 2685,2691 ----
               *newmsgs = vms_p->newmessages;
               *oldmsgs = vms_p->oldmessages;
       }
!       free(vmu);
       return 0;
  }


****** ADDITIONAL INFORMATION ******

Memory leak seems to exist in all 1.4 releases (including 1.4.3) though my latest fix is against trunk revision 61797.
Comments:By: Igor Goncharovsky (igorg) 2007-05-01 02:11:16

For review you patch you must provide patch in file attached to bug and fax disclaimer.
Please read here:
http://www.voip-info.org/wiki/view/Asterisk+Developer+Guidelines

By: Tilghman Lesher (tilghman) 2007-05-01 16:42:15

Committed a slightly different fix in 62545, merged in 62546.