Any Memory Leak Here

This is a discussion on Any Memory Leak Here within the C Programming forums, part of the General Programming Boards category; Dear All, There is a part of a C code, that gives me ,apparently, a memory leak. I cannot find ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    6

    Any Memory Leak Here

    Dear All,

    There is a part of a C code, that gives me ,apparently, a memory leak.
    I cannot find the exact place, and make sure if anything is wrong.
    I just wanted to see if anybody can help on this:


    Here is where the potential problem maybe:
    Code:
    void* my_function(void* ptr){
    	
           //this is taking a string, and parses its portions to separate argv, argc to pass to my_main()
    	MyStructure *main_input;
    	char *input_command_line = (char *)ptr;
    	main_input = (MyStructure *)malloc(sizeof(MyStructure));
    	main_input->argv = (char **)malloc(sizeof(char *));
    	parse_command_line(input_command_line, main_input);
    
    	my_main(main_input->argc,main_input->argv);
    
    	free(input_command_line);
    	free(main_input);
    
    	return;
    }
    Where MyStructure is defined like this:
    Code:
    typedef struct MyStructure{
        int argc;
        char** argv;
    
    } MyStructure;
    and the functions my_main(), and parse_command_line() are defined in the same c file like this:
    Code:
    void parse_command_line(char *command_line, MyStructure *return_value){
    	
    	return_value->argc = 0;
    	
    	return_value->argv[return_value->argc]=(char *)malloc(sizeof(char)*64);
    	
    	return_value->argv[return_value->argc] = strtok (command_line," ");
    	
    	while(return_value->argv[return_value->argc] != NULL){
    	    
    		(return_value->argc)++;
    		return_value->argv[return_value->argc]=(char *)malloc(sizeof(char)*64);
    		return_value->argv[return_value->argc] = strtok (NULL," ");
    		
    	}
    	return;
    }
    and:
    Code:
    int my_main(int argc, char **argv){
        /*Some stuff.....Nothing wrong here for sure*/
    
    return 0;
    }
    Last edited by akm3; 03-14-2009 at 04:00 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    You never free the allocation here:
    Code:
    return_value->argv[return_value->argc]=(char *)malloc(sizeof(char)*64);
    In fact, you even overwrite the allocated memory address. So you lose this address - so you can never freeit.
    Read the strtok manual...

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    Quote Originally Posted by EVOEx View Post
    You never free the allocation here:
    Code:
    return_value->argv[return_value->argc]=(char *)malloc(sizeof(char)*64);
    In fact, you even overwrite the allocated memory address. So you lose this address - so you can never freeit.
    Read the strtok manual...
    Well,

    I can free the memory at the end of "my_function()", but I don't know why the allocated memory is overwritten?

    I saw some examples of strtok(), they do not do memory allocation for the objects that strtok() returns, but in my case, if I don't do this, I still have the memory leak problem

    Is there any alternative solution to parse the string without having this allocation problems?

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    but I don't know why the allocated memory is overwritten?
    not the memory, just the address -
    Code:
    return_value->argv[return_value->argc]=(char *)malloc(sizeof(char)*64);
    		return_value->argv[return_value->argc] = strtok (NULL," ");
    first line stores in your var address returned by malloc
    second line stores address returned by strtok effectively leaking the previous address
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    Quote Originally Posted by akm3 View Post
    Well,

    I can free the memory at the end of "my_function()", but I don't know why the allocated memory is overwritten?

    I saw some examples of strtok(), they do not do memory allocation for the objects that strtok() returns, but in my case, if I don't do this, I still have the memory leak problem

    Is there any alternative solution to parse the string without having this allocation problems?
    Well,

    Thanks for the reply.
    I converted the big file, I am working with to a small example.
    I tried many changes on that, but I still have the memory leak problem

    Here is my simple code:
    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    typedef struct MyStructure{
        int argc;
        char** argv;
    
    } MyStructure;
    
    
    int my_main(int argc, char **argv)
    {
            int i;
    	printf("my_main is here\n");
    	for(i = 0; i < argc ; i++){
    		printf("In my_main: argv[%d]=%s\n",i,argv[i]);
    	}
    	return 1;
    }
    
    
    void parse_command_line(char *command_line, MyStructure *return_value){
    	
    	return_value->argc = 0;
    	
    	//return_value->argv[return_value->argc]=(char *)malloc(sizeof(char)*64);
    	
    	return_value->argv[return_value->argc] = strtok (command_line," ");
    	
    	while(return_value->argv[return_value->argc] != NULL){
    	    
    		(return_value->argc)++;
    		//return_value->argv[return_value->argc]=(char *)malloc(sizeof(char)*64);
    		return_value->argv[return_value->argc] = strtok (NULL," ");
    		
    	}
    	return;
    
    }
    
    void* av_function(void* ptr){
    	
    	MyStructure *main_input;
    	char *input_command_line = (char *)ptr;
    	main_input = (MyStructure *)malloc(sizeof(MyStructure));
    	main_input->argv = (char **)malloc(sizeof(char *));
    	parse_command_line(input_command_line, main_input);
    	printf("av_encode_decode called\n");
    	my_main(main_input->argc,main_input->argv);
    	printf("av_function called exited\n");
    	free(input_command_line);
    	free(main_input);
    
    	return;
    }
    int main()
    {
    	av_function("av -i testvideo.flv out.mog");
    	return 1;
    }
    I have commented out the allocations form the strings that are returned by strtok()
    But when I compile it in Cygwin(gcc -c myfile.c) I have the following complaiunt:

    8 [main] memtest 5976 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)
    Segmentation fault (core dumped)
    Last edited by akm3; 03-15-2009 at 03:12 PM.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    this code simply takes a string like "av -i testvideo.flv out.mog" in the av_function, and parses its words, and puts in MyStructure, then prints the values of argv[i]

    Is it a memory leak at all? And if so, how should I correct the code to get rid of the leak?
    Last edited by akm3; 03-15-2009 at 03:14 PM. Reason: Redundant

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    your argv is array of 1 pointer, you are assigning a lot more members
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    Quote Originally Posted by vart View Post
    your argv is array of 1 pointer, you are assigning a lot more members
    Even this doesn't solve the issue:

    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    typedef struct MyStructure{
        int argc;
        char** argv;
    
    } MyStructure;
    
    
    int my_main(int argc, char **argv)
    {
            int i;
    	printf("my_main is here\n");
    	for(i = 0; i < argc ; i++){
    		printf("In my_main: argv[%d]=%s\n",i,argv[i]);
    	}
    	return 1;
    }
    
    
    void parse_command_line(char *command_line, MyStructure *return_value){
    	
    	return_value->argc = 0;
    	
    	//return_value->argv[return_value->argc]=(char *)malloc(sizeof(char)*64);
    	
    	return_value->argv[return_value->argc] = strtok (command_line," ");
    	
    	while(return_value->argv[return_value->argc] != NULL){
    	    
    		(return_value->argc)++;
    		//return_value->argv[return_value->argc]=(char *)malloc(sizeof(char)*64);
    		return_value->argv[return_value->argc] = strtok (NULL," ");
    		
    	}
    	return;
    
    }
    
    void* av_function(void* ptr){
    	
    	MyStructure *main_input;
    	char *input_command_line = (char *)ptr;
    	main_input = (MyStructure *)malloc(sizeof(MyStructure));
    	main_input->argv = (char **)malloc(sizeof(char *)*256);
    	parse_command_line(input_command_line, main_input);
    	printf("av_encode_decode called\n");
    	my_main(main_input->argc,main_input->argv);
    	printf("av_function called exited\n");
    	free(input_command_line);
    	free(main_input);
    
    	return;
    }
    int main()
    {
    	av_function("av -i testvideo.flv out.mog");
    	return 1;
    }
    I see the problem is inside the function "parse_command_line()"
    Cygwin gives me a "sementation fault", and MSVC++ reports access violation.
    And I see its breaking inside that function, but I can't find out why, and cannot correct the issue there

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Code:
    "av -i testvideo.flv out.mog"
    That's a string constant. So, you can't change it. Still, you pass it as a non-const string, and you modify it, which causes the access violation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leak in this case?
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2008, 05:05 AM
  2. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 05:50 AM
  3. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  4. Any Memory Leak Checking Tool?
    By George2 in forum C Programming
    Replies: 4
    Last Post: 06-21-2006, 11:02 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21