Operating System - HP-UX
1823920 Members
3088 Online
109667 Solutions
New Discussion

C problem: Scandir memory leak

 
Alan Harding_1
New Member

C problem: Scandir memory leak

I have a Pro*C program which reads the files from a directory on filename order. I am using scandir for this. I copied the basic code from the scandir manpage. However, I have a memory leak which is causing the program to fail every 10 or so hours. I can see the process size getting larger over time. Here is what i have:

struct dirent **filelist, **list;
.
.
.
if ((vi_num_files = scandir(vc_data_dir, &filelist, f_filter_files, alphasort)) < 0)
{
printf(vc_log_msg, "Error performing scandir: %d ", vi_num_files);
exit(1);
}

if (vi_num_files)
{
for (vi_loop_counter = 0, list = filelist; vi_loop_counter < vi_num_files; vi_loop_counter++)
{
/* Do stuff with the file */
free(*list);
list++;
}
free (filelist)
}

The program is failing on the scandir, I assume because the malloc cannot allocate any memory dur to the leak. The directory permissions are not a problem.

Any ideas?

thanks

Alan
1 REPLY 1
Gregory Fruth
Esteemed Contributor

Re: C problem: Scandir memory leak

Perhaps examining errno will give a clue
as to what's going wrong. The man page
for scandir() doesn't say that it sets errno,
but the underlying malloc() ought to be
setting it.

See the man pages for malloc(), perror() and
errno for details.

If you have Purify, try running it on your code.
Purify claims that HP's remove() leaks memory;
maybe scandir() leaks, too. Also try installing
the latest libc patch to see if that helps.