Solved

scandir() - any ideas ?

Posted on 1998-07-13
4
216 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
Comment Utility
I think you don't free the dirent for "." and ".." - move free outside the if.
0
 

Author Comment

by:alan_stephens
Comment Utility
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
Comment Utility
there is no free db_mem
0
 
LVL 1

Accepted Solution

by:
Rajkumar_R earned 200 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now