Thread: A memory leak (I think...)

  1. #1
    Stevek
    Guest

    Unhappy A memory leak (I think...)

    Hi,

    I'm writing a movie database program. One of the features I want to include is the ability to import a list of movie titles from a text file. The Import function reads in all the titles from a filename of the user's choosing, puts those titles into a list, and then copies that list into another sorted list of movie titles. Here's my code:

    Code:
    void Import(MovieList& ToList, String FromFile)
    {
    	ifstream inFile;		//input file stream inFile
    	inFile.open(FromFile);//open inFile with user-specified filename
    
    	MovieList FromList;	//list to hold titles from file
    	String ImportedTitle;	//a title from the file
    
    	while (inFile)	//while not at the end of the file
    	{
    		//read in one line of the file as a string
    		inFile.getline(ImportedTitle, 40);	
    
    		//add that title from the file into a list
    		Add(FromList, ImportedTitle);
    	}
    
    	inFile.close();	//close the file
    
    	//the head of the file's title strings
    	Movie* FromLocation = FromList.Head;
    
    	//while the title list from the file has not ended
    	while (FromLocation != NULL)
    	{
    		//add title from file list into the database
    		Add(ToList, FromLocation->Title);
    
    		//go to next title from file
    		FromLocation = FromLocation->Next;
    	}
    }
    When I run this function in my program, I get a runtime error like so:
    AFW caused an invalid page fault in
    module AFW.EXE at 014f:00413750.
    Registers:
    EAX=0042fdff CS=014f EIP=00413750 EFLGS=00010286
    EBX=00560000 SS=0157 ESP=0066fb68 EBP=0066fb74
    ECX=cccccccc DS=0157 ESI=cccccccc FS=3faf
    EDX=0066fc48 ES=0157 EDI=0066fc48 GS=0000
    Bytes at CS:EIP:
    8a 06 46 8a 27 47 38 c4 74 f2 2c 41 3c 1a 1a c9
    Stack dump:
    00560000 81668408 0066fbe4 0066fbe4 004011e3 0066fc48 cccccccc 0066fcb8 81668408 00560000 cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc


    I think this means I have a memory leak that's being caused by one of the two loops. I have no idea why this is happening... the functions and data types used here work flawlessly outside of the Import function, so I don't think that they are to blame.

    If anyone can make heads or tails of this, I'd really appreciate it.

    Steve

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You will have to post more code...the only thing I see wrong with the code that you posted is the "while (infile)" loop. This is been a popular topic of late - check out this post.

    gg

  3. #3
    Stevek
    Guest
    What extra code should I post?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Well, if you want someone to find the problem for you - all of it (zip it up and attach).
    Or you can execute the program in a debugger and track down the problem (which is what I will do if you post the code). If you're not familiar will using a debugger to track down such issues, this is a good problem to learn on.

    gg

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    8

    the code

    Heres the zip, with all the cpp and header files (and the rest of the stuff that MSVC++6 creates when it compiles).

    BTW, any good tutorials anywhere on how to use the debugger? It confuses me, to put it lightly.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I'll post something on how to use VC++ debugger after I look at your code.

    gg

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Ok, I found it. The problem is that your are assuming that the compiler will initialize your data for you. For example:
    Code:
    int n; //can't assume the value will be 0, or any other value for that matter
    The same thing goes for struct's and class's:
    Code:
    struct foo
    {
       int n;
       int *p;
    };
    ...
    foo f; //can't assume any value for f.n or f.p
    So, you are getting a runtime error because FromList->head does not equal NULL when Add(FromList, ImportedTitle) is run. This easily fixed by adding constructors to your structs:
    Code:
    struct Movie
    {
        String Title;
        String Renter;
    
        Movie* Next;
    
        Movie() : Next(NULL) {}
    };
    
    struct MovieList
    {
        Movie* Head;
        int Size;
        
        MovieList() : Head(NULL),Size(0) {}
    };
    As for using the debugger, its easier than you think. The debugger will basically allow you to inspect the values of your variables and stop/start execution. Put your cursor on "Add(FromList, ImportedTitle);" then select Build->Start Debug->Run To Cursor.
    Type in 7 and movies.txt and you'll see that execution has stopped right where you told it. You can now inspect the values of your variables by selecting View->Debug Windows->Variables.
    Click on the '+' next to 'FromList'. You will see that FromList->Head has a value of 0xcccccccc and not NULL.
    The best way to learn the debugger is to play around with it.

    gg

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    8

    Thanks!

    You're a lifesaver! Many, many thanks.

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, 06: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