Solved

scandir() - any ideas ?

Posted on 1998-07-13
4
244 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;
}
0
Comment
Question by:alan_stephens
4 Comments
 
LVL 2

Expert Comment

by:mlev
ID: 1251619
I think you don't free the dirent for "." and ".." - move free outside the if.
0
 

Author Comment

by:alan_stephens
ID: 1251620
no - both . and .. do get freed up in the code above.

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


0
 
LVL 3

Expert Comment

by:danny_pav
ID: 1251621
there is no free db_mem
0
 
LVL 1

Accepted Solution

by:
Rajkumar_R earned 200 total points
ID: 1251622
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TCP/IP Socket - connection close results in data lost 14 142
change colour of repeater control in asp.net c# 7 114
Acrinis True image 2 93
Resolve Dependency Issues 4 79
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question