Help regarding memory leak in this C program


 
Thread Tools Search this Thread
Top Forums Programming Help regarding memory leak in this C program
# 8  
Old 03-21-2012
I can post the output of valgrind but for that I have to rerun the code that I pasted above. Another problem is that the valgrind output would be very long. but still i'll post a truncated version of it for others to see and learn.

---------- Post updated at 04:57 PM ---------- Previous update was at 04:08 PM ----------

First of all, this is the output of valgrind that I get for the original source code that I pasted here which had memory leak. I did not run the program until the end as the kernel will kill it. So, I ran the program for some time and killed it myself.
Code:
==20843== Memcheck, a memory error detector
==20843== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==20843== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==20843== Command: ./a.out
==20843== Parent PID: 20744
==20843== 
==20843== Invalid read of size 8
==20843==    at 0x3A05862397: getdelim (in /lib64/libc-2.5.so)
==20843==    by 0x40145C: read_text_file (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==    by 0x400E4B: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==  Address 0x5ef9b70 is 0 bytes after a block of size 6,556,464 alloc'd
==20843==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20843==    by 0x400BAD: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== 
==20843== Invalid write of size 8
==20843==    at 0x3A058624D6: getdelim (in /lib64/libc-2.5.so)
==20843==    by 0x40145C: read_text_file (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==    by 0x400E4B: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==  Address 0x5ef9b70 is 0 bytes after a block of size 6,556,464 alloc'd
==20843==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20843==    by 0x400BAD: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== 
==20843== Conditional jump or move depends on uninitialised value(s)
==20843==    at 0x4A0759A: strcat (mc_replace_strmem.c:174)
==20843==    by 0x4015B9: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==    by 0x400F32: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== 
==20843== Invalid write of size 1
==20843==    at 0x4A075B7: strcat (mc_replace_strmem.c:174)
==20843==    by 0x4015C7: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==    by 0x400F32: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==  Address 0x54d83c4 is 0 bytes after a block of size 4 alloc'd
==20843==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20843==    by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== 
==20843== Invalid write of size 1
==20843==    at 0x4A075C9: strcat (mc_replace_strmem.c:174)
==20843==    by 0x4015C7: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==    by 0x400F32: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==  Address 0x54d83c5 is 1 bytes after a block of size 4 alloc'd
==20843==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20843==    by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== 
==20843== Invalid read of size 1
==20843==    at 0x4A075BD: strcat (mc_replace_strmem.c:174)
==20843==    by 0x4015D4: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==    by 0x400F32: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==  Address 0x54d83c4 is 0 bytes after a block of size 4 alloc'd
==20843==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20843==    by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== 
==20843== Invalid write of size 1
==20843==    at 0x4A075B7: strcat (mc_replace_strmem.c:174)
==20843==    by 0x4015C7: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==    by 0x401143: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==  Address 0x54d83c4 is 0 bytes after a block of size 4 alloc'd
==20843==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20843==    by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== 
==20843== Invalid write of size 1
==20843==    at 0x4A075C9: strcat (mc_replace_strmem.c:174)
==20843==    by 0x4015C7: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==    by 0x401143: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==  Address 0x54d83c5 is 1 bytes after a block of size 4 alloc'd
==20843==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20843==    by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== 
==20843== Invalid read of size 1
==20843==    at 0x4A075BD: strcat (mc_replace_strmem.c:174)
==20843==    by 0x4015D4: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==    by 0x401143: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==  Address 0x54d83c4 is 0 bytes after a block of size 4 alloc'd
==20843==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20843==    by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== 
==20843== 
==20843== HEAP SUMMARY:
==20843==     in use at exit: 181,687,226 bytes in 3,493,754 blocks
==20843==   total heap usage: 3,494,530 allocs, 776 frees, 201,796,962 bytes allocated
==20843== 
==20843== LEAK SUMMARY:
==20843==    definitely lost: 61,943,610 bytes in 725,020 blocks
==20843==    indirectly lost: 0 bytes in 0 blocks
==20843==      possibly lost: 0 bytes in 0 blocks
==20843==    still reachable: 119,743,616 bytes in 2,768,734 blocks
==20843==         suppressed: 0 bytes in 0 blocks
==20843== Rerun with --leak-check=full to see details of leaked memory
==20843== 
==20843== For counts of detected and suppressed errors, rerun with: -v
==20843== Use --track-origins=yes to see where uninitialised values come from
==20843== ERROR SUMMARY: 5751 errors from 9 contexts (suppressed: 4 from 4)

But now a twist in this entire story. What I did now is this:

Code:
        for ( j = 0 ; j < MAXIMUM_WORDS ; j ++ )
        {
            memset ( words_from_web_page [ j ] , 0 , strlen ( words_from_web_page [ j ] ) );
        }

But previoulsy it was, as I first posted it:
Code:
memset ( words_from_web_page , 0 , MAXIMUM_WORDS );

Now when I introduce this change, I see that no memory leak occurs or better memory usage does not increase with time as I see it using the top command using the same function which I posted here first - that is:
Code:
void read_text_file ( FILE *file_pointer , char ** words_to_be_read , unsigned long int *number_of_words )
{
    unsigned long int len = 0;
    char *line = NULL;

    while ( ! feof ( file_pointer ) )
    {
        getline ( &words_to_be_read [ ( *number_of_words) ++ ] , &len , file_pointer );
    }

    ( *number_of_words )--;
    fclose ( file_pointer );
}

This means that problem was not with this function but the probelm was with the way I was using memset(). So, fundamentally I used getline() correctly. I really will loose a lot of hair thinking about this now. Smilie

Here is the valgrind output for the modified code that is modified using for loop in memser()

Code:
==20955== Memcheck, a memory error detector
==20955== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==20955== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==20955== Command: ./a.out
==20955== Parent PID: 20744
==20955== 
==20955== Invalid read of size 8
==20955==    at 0x3A05862397: getdelim (in /lib64/libc-2.5.so)
==20955==    by 0x4014B1: read_text_file (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==    by 0x400E4B: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==  Address 0x5ef9b70 is 0 bytes after a block of size 6,556,464 alloc'd
==20955==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20955==    by 0x400BAD: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== 
==20955== Invalid write of size 8
==20955==    at 0x3A058624D6: getdelim (in /lib64/libc-2.5.so)
==20955==    by 0x4014B1: read_text_file (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==    by 0x400E4B: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==  Address 0x5ef9b70 is 0 bytes after a block of size 6,556,464 alloc'd
==20955==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20955==    by 0x400BAD: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== 
==20955== Conditional jump or move depends on uninitialised value(s)
==20955==    at 0x4A0759A: strcat (mc_replace_strmem.c:174)
==20955==    by 0x40160E: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==    by 0x400F32: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== 
==20955== Invalid write of size 1
==20955==    at 0x4A075B7: strcat (mc_replace_strmem.c:174)
==20955==    by 0x40161C: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==    by 0x400F32: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==  Address 0x54d83c4 is 0 bytes after a block of size 4 alloc'd
==20955==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20955==    by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== 
==20955== Invalid write of size 1
==20955==    at 0x4A075C9: strcat (mc_replace_strmem.c:174)
==20955==    by 0x40161C: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==    by 0x400F32: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==  Address 0x54d83c5 is 1 bytes after a block of size 4 alloc'd
==20955==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20955==    by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== 
==20955== Invalid read of size 1
==20955==    at 0x4A075BD: strcat (mc_replace_strmem.c:174)
==20955==    by 0x401629: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==    by 0x400F32: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==  Address 0x54d83c4 is 0 bytes after a block of size 4 alloc'd
==20955==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20955==    by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== 
==20955== Conditional jump or move depends on uninitialised value(s)
==20955==    at 0x40114C: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== 
==20955== Invalid write of size 1
==20955==    at 0x4A075B7: strcat (mc_replace_strmem.c:174)
==20955==    by 0x40161C: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==    by 0x401198: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==  Address 0x54d83c4 is 0 bytes after a block of size 4 alloc'd
==20955==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20955==    by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== 
==20955== Invalid write of size 1
==20955==    at 0x4A075C9: strcat (mc_replace_strmem.c:174)
==20955==    by 0x40161C: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==    by 0x401198: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==  Address 0x54d83c5 is 1 bytes after a block of size 4 alloc'd
==20955==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20955==    by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== 
==20955== Invalid read of size 1
==20955==    at 0x4A075BD: strcat (mc_replace_strmem.c:174)
==20955==    by 0x401629: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==    by 0x401198: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==  Address 0x54d83c4 is 0 bytes after a block of size 4 alloc'd
==20955==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20955==    by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== 
==20955== 
==20955== More than 10000000 total errors detected.  I'm not reporting any more.
==20955== Final error counts will be inaccurate.  Go fix your program!
==20955== Rerun with --error-limit=no to disable this cutoff.  Note
==20955== that errors may occur in your program without prior warning from
==20955== Valgrind, because errors are no longer being displayed.
==20955== 
==20955== 
==20955== HEAP SUMMARY:
==20955==     in use at exit: 128,109,778 bytes in 3,047,279 blocks
==20955==   total heap usage: 3,047,508 allocs, 229 frees, 147,908,818 bytes allocated
==20955== 
==20955== LEAK SUMMARY:
==20955==    definitely lost: 13,800 bytes in 115 blocks
==20955==    indirectly lost: 0 bytes in 0 blocks
==20955==      possibly lost: 0 bytes in 0 blocks
==20955==    still reachable: 128,095,978 bytes in 3,047,164 blocks
==20955==         suppressed: 0 bytes in 0 blocks
==20955== Rerun with --leak-check=full to see details of leaked memory
==20955== 
==20955== For counts of detected and suppressed errors, rerun with: -v
==20955== Use --track-origins=yes to see where uninitialised values come from
==20955== ERROR SUMMARY: 10000000 errors from 10 contexts (suppressed: 4 from 4)

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Web Development

Finding Cause of Memory Leak

Hi We have just got a dedicated server with Fasthosts, O/S is Linux CentOS 6 64 bit. It was a fresh install and I have just moved one WordPress site onto there. The problem is we seem to be getting a memory leak (that's what Fasthosts said) and the database (I think) keeps crashing, so we... (3 Replies)
Discussion started by: Pokeyzx
3 Replies

2. Red Hat

Memory leak

Hi all I am using RED HAT 5.4, and i am getting memory uses problem. when i use "sync;echo 3 > /proc/sys/vm/drop_cache" command the memory will release after 2,3 hour memory show 95%. pls suggest right way. thanks (37 Replies)
Discussion started by: reply.ravi
37 Replies

3. Programming

Memory Leak

Hi, I am trying a database server which keeps a B+ plus tree structure and works on it. I am trying to find the memory used/leak while executing this process. I check the memory leak by using ps uax command. When i execute a delete query i am sure that my code frees up the existing... (9 Replies)
Discussion started by: kumaran_5555
9 Replies

4. IP Networking

memory leak?

Hi All, my client server application can work in two modes: 1) one direction - only client sends msgs to server 2) two directions - server gives 'answers' to client. when program run in the first mode it looks OK, but when server answers to client than client's application exit its... (2 Replies)
Discussion started by: lenna
2 Replies

5. UNIX for Advanced & Expert Users

Need to create a memory leak

Hi. This might be a strange request, but does anyone have any idea on a simple shell script that would use more and more memory as it ran? Like a purposeful leak. I want to test the behaviour of an already running program when the machine runs out of memory. Thanks! (4 Replies)
Discussion started by: rebelbuttmunch
4 Replies

6. Programming

Memory LEAK with pthreads

I have this code... #include <stdio.h> #include <iostream> #include <pthread.h> static void* cliente(void *datos); int main() { pthread_attr_t tattr; int ret; size_t size = PTHREAD_STACK_MIN + 0x0100; ret = pthread_attr_init(&tattr); ret =... (8 Replies)
Discussion started by: JEscola
8 Replies

7. Programming

how to check memory leak in C program under Unix

Hi, How to detect memory leak in C program under unix ? Thanks (6 Replies)
Discussion started by: useless79
6 Replies

8. Programming

Memory leak of fork()

Today, I wrote a test code for fork/execvp/waitpid. In the parent process, it fork 100 child processes which only execute "date" to print the current datetime. When any child process die, the parent process will receive a SIGCHLD signal. Then, the parent process will re-fork-execvp the child... (7 Replies)
Discussion started by: whererush
7 Replies

9. Programming

about virtual memory and memory leak

Hi, First of all I appreciate this group very much for its informative discussions and posts. Here is my question. I have one process whose virtual memory size increases linearly from 6MB to 12MB in 20 minutes. Does that mean my process has memory leaks? In what cases does the... (4 Replies)
Discussion started by: shriashishpatil
4 Replies
Login or Register to Ask a Question