?
Solved

scandir() - any ideas ?

Posted on 1998-07-13
4
Medium Priority
?
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 400 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

801 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