Thread: memory leak in vector <int>

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    163

    memory leak in vector <int>

    I implement a vector
    Code:
    vector<int> v;
    which reads in integers by

    Code:
    v.push_back(item); // add item to end of vector
    When my program ends, I call
    Code:
    v.clear();
    to clear v

    But I have memory leak from it.
    Code:
    Detected memory leaks!
    
    Dumping objects ->
    
    {132} normal block at 0x0035CD10, 36 bytes long.
    
    Data: < > 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00 
    
    Object dump complete.
    Why is it so? I check my memory leak by using

    Code:
    #include <crtdbg.h>
    
    _CrtDumpMemoryLeaks();
    in Microsoft Visual Studio 2005

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    item looks suspect.

    More code please. What is item and how are you allocating it?

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Ditto to Bubba... if item is allocated dynamically, I don't believe clear does any deletion on it. Plus, 36 bytes seems off to me. Wouldn't it be 32 more likely? I don't know what the overhead would be.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    this is a part of my code, d->ReadData() is the function which reads in the number. d is of class DATA, which i declare
    Code:
    	do {
    		d->ReadData();
    		if (!d->v.empty()) {
    			if (d->v.size()+tranTol >= (unsigned int)support) {
    				arr = NewArray_db(d->v.size() + 1); // +1 to store the transaction number too 	
    				sort( d->v.begin(), d->v.end() );  
    				arr[0] = d->v.size();       
    				j=1;
    				for (unsigned int i=0; i<d->v.size(); i++) {
    					arr[j++] = d->v.at(i);	  	        
    					bucket_item[d->v.at(i)]++;
    					//cout << d->v[i] <<" ";
    				}
    				//cout <<endl;
    			}
    			else {
    				arr = NewArray_db(1);
    				arr[0] = 0;       
    			}
    			tempt[index++] = arr;   
    			for (int z=0; z<d->v.size(); z++) {
    				d->v[z] = 0;
    			}
    			d->v.clear();      
    			d->v.resize(0);
    		}
    		else { //the transaction read in is empty
    			arr = NewArray_db(1);
    			arr[0] = 0;       
    			tempt[index++] = arr;   
    			d->v.clear();      
    		}
    	} while (!feof(d->in));


    Code:
    void Data::ReadData() {
    	do {
    		usi pos=0, item=0;
    		c = getc(in);
    
    		if (c=='-') {//if read in a -1, skip it
    			c = getc(in);
    			c = getc(in);
    		}
    
    		while((c >= '0') && (c <= '9')) {
    			item *=10;
    			item += usi(c)-usi('0'); 
    			c = getc(in);
    			pos++;
    		}
    
    		if (pos) {
    			v.push_back(item); // add item to end of vector	  
    		}
    
    		if (c == '\n')
    			return;
    
    	} while (!feof(in));
    }

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Seems far more likely you're failing to properly delete an object you allocated, like d, or arr, or something.

    BTW -- your code really needs descriptive variable names, and comments.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Always use braces whenever you're doing a statement.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by maxorator
    Always use braces whenever you're doing a statement.
    I don't know why... you mean in situations like this?
    Code:
    if (exit == true)
        return 0;
    I almost never use braces on single statement conditionals and loops. Braces can get annoying to me and they're easily replaced with good spacing, which I try to use. His braces seem fine to me. His indentation is weird but that's because he's probably using tabs not spaces. The only real issue I have with his style is the way he puts comments after opening braces. In my opinion:
    Code:
    /* This is ugly */
    if (condition) { // This is a comment
       // Doing stuff
    }
    /* End ugly */
    
    /* Better to do this */
    if (condition) // This is a comment
    {
        // Doing stuff
    }
    
    /* Or this */
    if (condition) {
       // This is a comment
       //Doing stuff
    }
    
    /* In my opinion */
    Sent from my iPadŽ

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    TABS RULE
    Code:
    /* Yeah, I like this too: Or this */
    if (condition) {
       // This is a comment
       //Doing stuff
    }
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    I tried something simple

    Code:
    	vector<int> g;
    	g.push_back(10);
    	g.clear();
    	_CrtDumpMemoryLeaks();
    but there is still memory leak.

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    found the solution here! you got to free vector by youself

    http://www.gamedev.net/community/for...opic_id=391685

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    My interpretation of that is that it does not mean that your program has a memory leak: rather, at the point where the check is made, the memory simply has not been deallocated yet, even though the objects themselves have been destroyed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    yes, i thought of that too, but is there anyway to double confirm? I think I will play safe than be sorry and use the solution above.

    hey, i'm from singapore too! =)

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > {132} normal block at 0x0035CD10, 36 bytes long.
    Well if you go on to read the manual a bit more on the memory leak debugging, the {nnn} is the allocation number, which you can set a breakpoint on (the manual tells you how).

    When you know which allocation that is, you can then decide whether it's one you really need to worry about, or wether it is some "system" overhead block which has nothing to do with your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    yes, i thought of that too, but is there anyway to double confirm?
    I'm not sure what your main() function looks like, but rigging it up like this helps:
    Code:
    void Run()
    {
      // do everything in here
    }
    
    int main()
    {
      Run();
      _CrtDumpMemoryLeaks();
    }
    This way, you know all the automatic destructors have been called by the time you call the dump. This will still report any heap memory allocated by any global or static objects you have though.

    if you #define _CRTDBG_MAP_ALLOC before you #include <crtdbg.h>, it should give you the file and line number where the allocation was made, but it doesn't always work correctly. That define defines new as an inline function that takes __LINE__ and __FILE__, but I've had lots of trouble trying to get that function to actually compile inline. If it doesn't, it will tell you all allocations happen in that function instead of the one that called new, which sucks. I swear I've gotten it to work before, but I can't remember how.

  15. #15
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Perhaps try the method from this thread.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

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