LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-24-2006, 07:42 AM   #1
Rayven
LQ Newbie
 
Registered: Oct 2006
Distribution: Red Hat 4, Ubuntu
Posts: 21

Rep: Reputation: 15
Question Valgrind reports memory leak at opendir


When I run Valgrind memcheck on my C application, it reports that I have a memory leak on an opendir command:

Code:
if( (dp = opendir( inputDir )) == NULL )
{
   logError( "Could not open directory path." );
    return -1;
}
The inputDir variable is a char *, that is free'd when the program terminates. However, this application is set to run continuously for very long times without leaving. When running Valgrind this is the only memory leak that it reports, and I do not understand why this is being reported or how to fix it. I am running this under Red Hat WS 4 with the Portland Group compilers.

Thanks
 
Old 10-24-2006, 08:00 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Is this in main or its own function? if main and it fails you return -1 (exit)
If it succeeds do you close the stream?

Last edited by dmail; 10-24-2006 at 08:08 AM.
 
Old 10-24-2006, 08:38 AM   #3
Rayven
LQ Newbie
 
Registered: Oct 2006
Distribution: Red Hat 4, Ubuntu
Posts: 21

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by dmail
Is this in main or its own function? if main and it fails you return -1 (exit)
If it succeeds do you close the stream?
No, this is not in main, it is in its own function. I guess I should have put this code in just after the opendir call:

Code:
if( (dp = opendir( inputDir )) == NULL )  <========Valgrind reports error on this line
{
   logError( "Could not open directory path." );
    return -1;
}

while( (dirp = readdir( dp )) != NULL )
{
   if( (strcmp( dirp->d_name, "." ) != 0) && 
       (strcmp( dirp->d_name, ".." ) != 0)
   {
      // fork child process to handle the file contents
   }
}

if( closedir( dp ) < 0 )
{
   logError( "Error closing file" );
}

return numProcessed;

Last edited by Rayven; 10-24-2006 at 08:41 AM.
 
Old 11-01-2006, 02:33 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
I read elsewhere you were developing on both Red-Hat and Solaris.
If you are compiling on SPARC hardware, I would suggest you to use an alternative method to find out memory leaks on the Solaris side by using Sun Studio 11 compiler and dbx RTC.
 
Old 11-01-2006, 03:42 AM   #5
lorebett
Member
 
Registered: May 2004
Location: Italy
Distribution: Ubuntu, Gentoo
Posts: 57

Rep: Reputation: 16
sometimes valgrind reports memory leaks on library functions because some library functions do not actually free memory but keep them in a pool. This might be the case?
 
Old 11-01-2006, 03:03 PM   #6
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Quote:
opendir()
opens the directory dirname and associates a directory
stream with it. opendir() returns a pointer used to
identify the directory stream in subsequent operations.
opendir() uses malloc(3C) to allocate memory.
This is from the man page for dirent which you should read.

Add a free(dp) to relaese the DIR struct when you are done with it.
 
Old 11-01-2006, 03:38 PM   #7
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
opendir returns a pointer to a DIR stream so free(dp) wouldent make any sense. He does close the stream properly. So if there is a problem with malloc inside the opendir function then its a problem with the standard library call opendir. If this is the case try and locate the source for the library and see if it properly free's the memory. Im guessing this is going to be encapulated with the rest of the similar calls.

Last edited by exvor; 11-01-2006 at 03:52 PM.
 
Old 11-01-2006, 06:56 PM   #8
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by jim mcnamara
This is from the man page for dirent which you should read.

Add a free(dp) to relaese the DIR struct when you are done with it.
and what is wrong with using closedir()?
 
Old 11-01-2006, 07:43 PM   #9
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
closedir() is better, just doing free() may leak a file handle. Also, never, ever free FILE* and DIR* directly because those are managed by the system.

For the OP issue, what happens in valgrind if you have a trivial program that simply opens and closes a directory?

Last edited by tuxdev; 11-01-2006 at 07:44 PM.
 
Old 11-02-2006, 10:14 AM   #10
Rayven
LQ Newbie
 
Registered: Oct 2006
Distribution: Red Hat 4, Ubuntu
Posts: 21

Original Poster
Rep: Reputation: 15
Question

I am attempting to respond to multiple questions at once, so this is a rather long post!

Quote:
Originally Posted by jlliagre
I read elsewhere you were developing on both Red-Hat and Solaris.
If you are compiling on SPARC hardware, I would suggest you to use an alternative method to find out memory leaks on the Solaris side by using Sun Studio 11 compiler and dbx RTC.
I am using Sun Studio 10 on the Solaris side, however, I have not ran the code through studio10 to determine memory leaks on Solaris, since this application will be primarily ran on RHEL4.

Quote:
Originally Posted by lorebett
sometimes valgrind reports memory leaks on library functions because some library functions do not actually free memory but keep them in a pool. This might be the case?
This may be the case why my application is increasing very quickly. Is there a 'C' function call to force RHEL to free the memory from the pool? The reason I believe this may be a problem, is that when I stop the application and continue to watch the memory, it very slowly drops about 50MB or so, and then holds at that value.

Quote:
Originally Posted by tuxdev
closedir() is better, just doing free() may leak a file handle. Also, never, ever free FILE* and DIR* directly because those are managed by the system.

For the OP issue, what happens in valgrind if you have a trivial program that simply opens and closes a directory?
I ran a quick test using opendir/closedir (please excuse any syntax/sematic errors in the code below, I typed this in from memory, and I have a bad memory!)

Code:
int main( int argc, char*argv[])
{
   DIR *dp;
   struct dirent *dirp;

   dp = opendir( argv[1] );
   
   while( (dirp = readdir( dp )) != NULL )
   {
      fprintf( stderr, "File %s\n", dirp->d_name );
   }

   closedir( dp );
  
   exit( 0 );
}
Valgrind did not report any memory issues with the test code, even when I wrapped the opendir/closedir code in a for statement. I am wondering if the memory leak is occuring when the child process does not "finish" before the closedir( ) is called. The child processes are not using anything dealing with DIR or dirent pointers. The filenames are copied prior to fork being called.

Code:
if( (dp = opendir( inputDir )) == NULL )  <========Valgrind reports error on this line
{
   logError( "Could not open directory path." );
    return -1;
}

while( (dirp = readdir( dp )) != NULL )
{
   if( (strcmp( dirp->d_name, "." ) != 0) && 
       (strcmp( dirp->d_name, ".." ) != 0)
   {
      // check if valid file using dirp->d_name
     
      // Copy dirp using snprintf adding the path and new prefix

      // fork child process to handle the file contents

      // Child process does not interact with the parent process at all

      // Parent process moves to the next file in the list
   }
}

if( closedir( dp ) < 0 )
{
   logError( "Error closing file" );
}

return numProcessed;
I do not have direct access to the code right now. It is not on this machine, and the printer is down. If needed, I will try and get more code.
 
Old 11-02-2006, 10:23 AM   #11
lorebett
Member
 
Registered: May 2004
Location: Italy
Distribution: Ubuntu, Gentoo
Posts: 57

Rep: Reputation: 16
Quote:
Originally Posted by lorebett
sometimes valgrind reports memory leaks on library functions because some library functions do not actually free memory but keep them in a pool. This might be the case?
by the way, if this is the case (and for library functions it usually is), you can simply suppress this leak warning, by using the suppression options by valgrind.
 
Old 11-02-2006, 02:03 PM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by Rayven
I am using Sun Studio 10 on the Solaris side, however, I have not ran the code through studio10 to determine memory leaks on Solaris, since this application will be primarily ran on RHEL4.
Testing an application under different platforms is a good thing, at it often allows to identify bugs faster.
Quote:
This may be the case why my application is increasing very quickly. Is there a 'C' function call to force RHEL to free the memory from the pool? The reason I believe this may be a problem, is that when I stop the application and continue to watch the memory, it very slowly drops about 50MB or so, and then holds at that value.
What memory value are you referring to ?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Valgrind and Shared Memory nodger Programming 0 04-27-2005 07:29 AM
valgrind reports un-free'd memory in crypt()? Xagafinelle Programming 3 08-21-2004 11:02 AM
possible memory leak?? matt80 Linux - General 2 07-01-2004 11:07 PM
Memory Leak when using memory debugging C program on SuSE SLES8 babalina Linux - Distributions 0 10-06-2003 09:39 AM
Memory Leak? eLinux Linux - General 2 09-20-2003 09:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration