ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Could you show us the beginning of the function with its declarations and possible use of the dp pointer before the line where valgrind reports possible memory leak?
Could you show us the beginning of the function with its declarations and possible use of the dp pointer before the line where valgrind reports possible memory leak?
Code:
char *inputDir; // This value is set at program startup from an
// environment variable (getenv). It is free'd when
// the used pressed Ctrl-C to terminate the
// application (if ever).
// pollDirectory( ) is called within a for( ;; ) statement, if the
// return value is < 0, the application terminates
int pollDirectory( ){
pid_t pid;
struct direct *dirp;
struct stat statbuf;
struct stat filestat;
char inPath[256];
char tempPath[256];
char msg[1024];
int inSz = 0;
int currNumScreened = 0;
DIR *dp;
// Ensure directory exists
if( lstat( inputDir, &statbuf ) < 0 ) {
// Report error
return -1;
}
// path is a directory
if( S_ISDIR( statbuf.st_mode ) == 0 ) {
// Report error
return -1;
}
// Code to open directory and parse through filenames
// fork child processes to handle the files, child process renames
// and moves the listed files.
// After all the directory listing has been processed, closedir( )
// and return the number of files processed
}
This just occured to me while typing this post: if I am removing the files from the directoy, would that cause a memory leak within the dirent/DIR structures? It doesent seem like that should cause any leaks, but I am not totally sure.
Are you looking at the memory numbers or the swap ones ?
By the way, vmstat does not report used memory or swap, just free memory and swap.
In top I am looking at the available/free memory, total memory, and swap available/free and total. vmstat I am looking at the available/free memory. I am typically closing the application prior to the swap space being used.
Also, I ran the program through Sun Studio 10 and found a few more memory leaks, but not on the opendir call...this is VERY difficult!
Do you use the "dp" pointer in the forked process for any other purpose? Normally, a memory leak happens when you use a pointer to get a new chunk of memory without freeing a previous one. On your case, it would happen if you use "opendir" without a "closedir" for a previous opened directory. Is your function recursive? Do you fork for opening another subdirectory?
Do you use the "dp" pointer in the forked process for any other purpose? Normally, a memory leak happens when you use a pointer to get a new chunk of memory without freeing a previous one. On your case, it would happen if you use "opendir" without a "closedir" for a previous opened directory. Is your function recursive? Do you fork for opening another subdirectory?
No, dp is not used inside of the child process. The process flow is read the directory contents (no subdirectories), create a temporary filename by appending a prefix to the filename, rename the file to the temporary name (so I do re-evaluate the file), fork the process, child process evaluates file contents, parent moves to next filename. After the entire directory is evaluated, dp is closed, the pollDirectory function returns the number of files evaluated, and sleeps for x number of seconds and then repeats the entire process.
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783
Rep:
Quote:
Originally Posted by Rayven
In top I am looking at the available/free memory, total memory, and swap available/free and total. vmstat I am looking at the available/free memory.
vmstat doesn't reports available memory.
Quote:
I am typically closing the application prior to the swap space being used.
Gnu/Linux and Solaris manage swap space quite differently.
With Solaris, the total swap space reported (for example by swap -s) is the sum of the RAM size plus the sum of swap areas on disk. A malloc will fail if no page is available in this whole swap space.
With Gnu/Linux, the total swap space reported is the size of the swap areas on disk. By default, a malloc won't fail if no more virtual memory is available. This is the controversial lazy memory allocation method. Controversial as it breaks malloc semantics and because of the OOM killer infamous behaviour.
Where are these other memory leaks, in libraries or in your code ?
I fixed a few in my source code. I still have a couple more ti figure out if they are in libraries or in my source.
Quote:
What is very difficult ?
Debugging memory leaks is VERY difficult. In school I was never taught about good memory management for large scale programs. It is something I have had to learn on my own, with the help of forums such as these!
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783
Rep:
Quote:
Debugging memory leaks is VERY difficult. In school I was never taught about good memory management for large scale programs. It is something I have had to learn on my own, with the help of forums such as these!
Yes, memory bugs nightmares with C and C++ code are one of the reasons Java is so successful these days.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.