[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

scandir() - any ideas ?

Ive run this scandir code through a memory analysis
tool and it says that 264 bytes are not getting freed up -
i.e. 1 dirent entry. Ive removed unneccessary code below
but left the core stuff - Im running on a HP 10.20 unix box
with cc compiler.

Any ideas why the memory for 1 entry is not getting freed ?

Rgds
Al.

 /* lots of headers, etc removed */

#define db_path "/tmp"

typedef struct
{
   char first_line[80];
   unsigned char db_num;
   unsigned char arr_size;
   unsigned char db_arr[db_arr_len];
} S_DB;


LOCAL S_DB *dbmem = NULL;

int load_db_files()
{
   char file_name[35];
   int i=0,j=0, k=0, ret=-1, notdb_files=0;

   struct dirent **namelist, **list;


   num_dbs=0;
   if (dbmem != NULL)
   {
      free(dbmem);
   }

   if ((num_dbs = scandir(db_path,&namelist,NULL,alphasort))< 0)
   {
      return -1;
   }
   else
   {
      if ((dbmem = (S_DB *) malloc (sizeof(S_DB)*(num_dbs-2)))!=NULL)
      {
         for (i=0,list=namelist; i<num_dbs;i++)
         {
           (void)strcpy(file_name,(*list)->d_name);
           if (file_name[0] != '.')
           {
            if (strncmp(file_name,"db",4) == 0)
            {

               /* lots of processing removed but no returns or exits,etc*/

            }
            else
            {
               notdb_files++;
            }
           }
           else
           {
            notdb_files++;
           }

           free(*list);
           *list++;
         }

         free(namelist);
         ret=0;
      }
   }
   num_dbs = num_dbs-notdb_files;
   return num_dbs;
}
0
alan_stephens
Asked:
alan_stephens
1 Solution
 
mlevCommented:
I think you don't free the dirent for "." and ".." - move free outside the if.
0
 
alan_stephensAuthor Commented:
no - both . and .. do get freed up in the code above.

at line free(*list) within the for loop.


0
 
danny_pavCommented:
there is no free db_mem
0
 
Rajkumar_RCommented:
Few observations ...one might do the job for you !
1)There is no free for "dbmem" as somebody else told ....
2), if scandir returned something less than 0 you return -1 ....just before that check whether namelist is null if not then free it before returning !
3) for (i=0,list=namelist; i<num_dbs;i++)  try making this "i<=num_dbs".
4)The free(list*) ;
         *list++;                  and/or
  for (i=0,list=namelist; i<num_dbs;i++)
togather  is causing one dirent to be left out with out freeing ...
just change as it fits your logic !
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now