Thread: memory leak in vector <int>

  1. #16
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    ok, thanks! i go try it out

  2. #17
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    usi pos=0, item=0;
    What is usi? If that is an object you really need a better way to distinguish it.

  3. #18
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    I apologise, usi is actually int

  4. #19
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I've had a lot of troubles relying on the information spit out from the debugger and from the MFC overloaded new and delete.

    At times I've used _CrtSetBreakAlloc(allocation_number) as Salem has suggested and found that the 'leak' is coming from an array that was allocated on the stack. It seems that at times the dump is performed prior to your objects actually being destroyed which means they will definitely be reported as leaks. However, your code may clean these up but b/c the dump was performed before this cleanup, you get incorrect information.

    I've also experience no dump at all when in fact _crtDumpMemoryLeaks() returned true indicating a memory leak somewhere. Very odd.


    The best way to perform debugging is to ignore that dump crapola and set crt debug hooks and let the crt debug library call your functions when something goes wrong. Then you can spit out information that is important to your code. Look in the manual on setting hooks and the _CrtMemState structure.

    You can also use exception handling within try catch blocks to help you debug your code.

    Code:
    try
    {
      int *pArray=new int[1000];
      if (!pArray) throw std::bad_alloc;
    }
    catch (std::bad_alloc ba)
    {
      cout << ba.what() << endl;
    
      //Decide here if you can recover gracefully or just exit
    }
    You can also create your own exception class and throw it.

    Code:
    class CMyException
    {
      char *m_pText;
      char *m_pFile;
      int m_iLine;
    
      public:
        CMyException(char *pText,char *pFile,int iLine):m_pText(pText),m_pFile(pFile),m_iLine(iLine) { }
    
        void ShowError(void)
        {
           //Show the error here
        }
    };
    Code:
    ...
    try 
    {
      CObject *pObject=new CObject();
      if (!pObject) throw CMyException("Allocation failure for CObject",__FILE__,__LINE__);
    }
    catch (CMyException &me)
    {
      me.ShowError();
    }
    Last edited by VirtualAce; 10-10-2006 at 04:52 AM.

  5. #20
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    How to explain this:
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class Test
    {
        int x;
        public:
            Test()
            {
                cout << "Constructor!" << endl;
            }
            ~Test()
            {
                cout << "Destructor!" << endl;
            }
    };
     
    int main()
    {
        vector <Test> v;
        Test t1, t2, t3;
        v.push_back(t1);
        v.push_back(t2);
        v.push_back(t3);
        
        v.clear();
    }
    My output is:
    Constructor!
    Constructor!
    Constructor!
    Destructor!
    Destructor!
    Destructor!
    Destructor!
    Destructor!
    Destructor!
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  6. #21
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Micko
    How to explain this:
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class Test
    {
        int x;
        public:
            Test()
            {
                cout << "Constructor!" << endl;
            }
            ~Test()
            {
                cout << "Destructor!" << endl;
            }
    };
     
    int main()
    {
        vector <Test> v;
        Test t1, t2, t3;
        v.push_back(t1);
        v.push_back(t2);
        v.push_back(t3);
        
        v.clear();
    }
    My output is:
    Constructor!
    Constructor!
    Constructor!
    Destructor!
    Destructor!
    Destructor!
    Destructor!
    Destructor!
    Destructor!
    You're pointing out the discrepancy between the number of times the constructor and destructor have been called? You need to add a copy constructor to your code and then see what happens. Each push_back call is calling the copy constructor to create a copy of the objects you push onto the vector. This is where the 3 missing constructor calls are hiding.

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class Test
    {
        int x;
        public:
            Test()
            {
                cout << "Constructor!" << endl;
            }
            Test(const Test& test)
            {
                x = test.x;
                cout << "Copy constructor!" << endl;
            }
            ~Test()
            {
                cout << "Destructor!" << endl;
            }
    };
    
    int main()
    {
        vector <Test> v;
        Test t1, t2, t3;
        v.push_back(t1);
        v.push_back(t2);
        v.push_back(t3);
        
        v.clear();
    }
    Should generate output similar to:
    Constructor!
    Constructor!
    Constructor!
    Copy constructor!
    Copy constructor!
    Copy constructor!
    Destructor!
    Destructor!
    Destructor!
    Destructor!
    Destructor!
    Destructor!
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #22
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by hk_mp5kpdw
    Each push_back call is calling the copy constructor to create a copy of the objects you push onto the vector.
    That explains things. Thank you!
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

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