CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Mar 2010
    Posts
    1

    Are the memory leaks detected real leaks?

    Hi!

    I'm debugging a program doing a bundle block adjustment that someone else wrote. I used the memory leak detector macros of Visual Studio (2008) i.e.

    Code:
    #	define _CRTDBG_MAP_ALLOC
    #	include <stdlib.h>
    #	include <crtdbg.h>
    
    (code omitted)
    
    int 
    main( int argc, char* argv[] )
    {
    	using namespace std;
    
    #ifdef CRTDB
    	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
    
    	unsigned allocNumber = 6015;
    	if( allocNumber > 0 )
    		_CrtSetBreakAlloc(allocNumber);
    #endif
    
    (code omitted)
    
    } // end main()
    I also tried Visual Leak Detector, as suggested in another thread. Both found 2 memory leaks at the end of the program. But if I use the function _CrtDumpMemoryLeaks() just before exiting (i.e. return EXIT_SUCCESS, I have a whole bunch of "detected" memory leaks.

    Even with the VS macros, I can't have the file name and the line number of the faulty call. With VLD, I have more info. But what about the other "detected" leaks? Are they real leaks since the memory seems to be freed at the exit of the program and they are not detected by the two tools?

    One concern I have is that for a small sets of inputs, the program runs well and fast (at least, except for the two leaks). For large inputs, the running time is unpredictable. I suppose that at each iteration of the bundle adjustment new objects are created to store the new computed values of the variables and that the old objects are not freed. The memory gets saturated and "something" (the OS?) is dumping objects during run time, thus the different running times recorded (the time for "something" to free the memory).

    I think I have found the faulty class, Data. It is nested in another class, template< typename T > class Matrix. T is double by default. Here is the code of Data (I have added some optional commentaries).

    Code:
        class
        Data : public stdx::ReferenceCountedObject
        {
        // Types
        public:
            typedef T           value_type;		
            typedef size_t      size_type;		
            typedef T*          iterator;		
            typedef const T*    const_iterator;	
    
        // Data Members
        private:
            size_type   m_rows, m_cols;			
    #ifdef MATRIX_STACK_ARRAY
            value_type  m_staticArray[MATRIX_STACK_ARRAY_SIZE];
    #endif
            value_type* m_dynamicArray;			
            bool        m_deletable;			
    
        // Function Members
        public:
            Data() 
                : m_rows(0), m_cols(0), 
                 m_dynamicArray(0), m_deletable(true) 
    	{ 
    		// _DOM_ Petit commentaire
    		//std::cerr << "\n Constructeur Data()";
    	}
            Data( size_t r, size_t c, const T& x = T() )
                : m_rows(r), m_cols(c), 
                  m_dynamicArray(0), m_deletable(true)
            {
                allocate();
                std::fill( array(), array()+size(), x );
            }        
            Data( size_t r, size_t c, const no_init& )
                : m_rows(r), m_cols(c), 
                  m_dynamicArray(0), m_deletable(true)
            {
                allocate();
            }  
            Data( T* array, size_t r, size_t c )
                : m_rows(r), m_cols(c), 
                  m_dynamicArray(array), m_deletable(false) 
    		{}
            Data( const Data& rhs )
                : m_rows(rhs.rows()), m_cols(rhs.cols()), 
                  m_dynamicArray(0), m_deletable(true)
            {
                allocate();
                std::copy( rhs.array(), rhs.array()+size(), array() );
            }
            virtual ~Data() 
            { 
                if( this->m_deletable )
    	{
                    delete [] m_dynamicArray; 
    	// _DOM_ Petit commentaire
    	//std::cerr << "\n Destructeur ~Data()";
    	}
            }
    
    
    (accessors, modifiers and opertors redefinitions omitted)
    
    
            friend class Matrix;
    
            void allocate()
            {
    #ifdef MATRIX_STACK_ARRAY
                m_dynamicArray = size() > MATRIX_STACK_ARRAY_SIZE ? new T[size()] : NULL;
    #else
                m_dynamicArray = new T[size()];
    #endif
            }
    
    };
    In each contructor, the attribute m_deletable is set correctly (there is one Matrix:ata that needs to be kept in memory from beginning to end, hence m_deletable(false)). What I am not sure about is the destructor: it does not seem to be called when needed (assuming again the "detected" leaks are real). I did not find any other class being the child of the class Data, so it looks strange that the destructor is virtual. Is there an other way to write this destructor?

    If needed, I can post the code for the ReferenceCountedObject class inherited by Data.

    I'm using Visual Studio 2008 on Windows XP pro x64. I have four builds: Win32-debug, Win32-release, x64-debug and x64-release. The same two leaks (and the other "detected" ones) appear in every build.

    Thanks for your help! Any comment is welcome!
    Last edited by Marc G; March 12th, 2010 at 03:31 AM. Reason: Added code tags

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured