We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

scandir() - any ideas ?

alan_stephens
on
Medium Priority
288 Views
Last Modified: 2010-04-15
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;
}
Comment
Watch Question

Commented:
I think you don't free the dirent for "." and ".." - move free outside the if.

Author

Commented:
no - both . and .. do get freed up in the code above.

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


there is no free db_mem
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.