Thread: CRT reports memory leaks

  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    CRT reports memory leaks

    Just started some testing of my resource manager and the CRT is reporting leaks but they are not being dumped at program termination. My program exits with code 0 and nothing is dumped.

    So why would the CRT report memory leaks and yet none of them are dumped?

    If I create the skysphere and sun CRT will report a mem leak on exit but no blocks are reported or dumped. If I only create the skysphere or the sun but not both, CRT does not report anything and as well no blocks are dumped.

    Quite odd.

    CResource.h
    Code:
    class IResource
    {
      public:
        virtual IResource* Get()=0;
        virtual void* GetBuffer() const=0;
        virtual UINT GetSize() const=0;
        virtual DWORD GetID() const=0;
        virtual void SetID(const DWORD ID) const=0;
        
    };
    
    class CResource:public IResource
    {
      private:
        CResource(const CResource &res);
        CResource &operator=(const CResource &res);
        
      protected:
        //Size of file (and hence this resource)
        UINT m_uFilesize;
        
        //Resource unique ID - must be unique or system will fail
        mutable DWORD m_dwID;
                
        //Resource data
        BYTE *m_pBuffer;
        
      public:
        CResource(std::string Filename);
        virtual ~CResource()
        {
          //::MessageBox(0,"Deleting resource",0,0);
          delete [] m_pBuffer;
        }
        ...
        ...
    
    };
    CResMgr.h
    Code:
    ...
    ...
    class CResource;
    
    //Typedef for cache list and map iterators
    typedef std::list<CResource *>::iterator CacheList_Iter;
    typedef std::map<DWORD,CacheList_Iter>::iterator CacheMap_Iter;
    
    
    
    class CResMgr
    {
      private:
        //Hide these two to prevent accidental copying
        CResMgr(const CResMgr &); 
        CResMgr &operator=(const CResMgr &);
      
            
        //Cache stats
        DWORD m_dwDesiredSize;
        DWORD m_dwCurSize;
        DWORD m_dwNumHits;
        DWORD m_dwNumMisses;
        
        DWORD m_dwMaxSize;
                
      protected:
        
        //STL map and list
        std::list<CResource *> CacheList;
        std::map<DWORD, CacheList_Iter> CacheMap;
        ...
        ...
    };
    CResMgr.cpp
    Code:
    ...
    ...
    
    CResMgr::~CResMgr()
    {
      //Delete the list
      CacheList_Iter List_Iter=CacheList.begin();
      
      for (UINT i=0;i<static_cast<UINT>(CacheList.size());i++);
      {
        delete *List_Iter;
        List_Iter++;
      
      }
      
      CacheList.clear();
      CacheMap.clear();
      
    }
    Am I leaking in the destructor? I think I've posted all of the code needed to answer this.
    Last edited by VirtualAce; 09-14-2007 at 11:50 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > for (UINT i=0;i<static_cast<UINT>(CacheList.size());i++);
    There's a classic trailing ; here
    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.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    OMG. Let me check my original code and see if that is there. If it is I feel more than stupid.

    Hehe.

    Thanks for pointing it out.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Is it the newest VC++ or GCC that warns about this stuff? I know at least one of them can do it. (And if you really need an empty loop, use empty braces.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    My copy of MinGW doesn't seem to do it (gcc/g++ version 3.4.5). I wonder if I'll ever see an update to MinGW again in my lifetime. They sure are taking their time about it.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    Quote Originally Posted by Frobozz View Post
    My copy of MinGW doesn't seem to do it (gcc/g++ version 3.4.5). I wonder if I'll ever see an update to MinGW again in my lifetime. They sure are taking their time about it.
    they'll port gcc-4.2 some time...

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    3.4.5 is hardly the newest version
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Warnings for GCC, from http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
    -Wextra [or -W]
    - (C++ only) An empty body occurs in a `while' or `for' statement with no whitespacing before the semicolon. This warning can be independently controlled by -Wempty-body.

    -Wempty-body
    Warn if an empty body occurs in an `if', `else' or `do while' statement. Additionally, in C++, warn when an empty body occurs in a `while' or `for' statement with no whitespacing before the semicolon. This warning is also enabled by -Wextra.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Quote Originally Posted by CornedBee
    3.4.5 is hardly the newest version
    Which is sad because I'm using the "candidate" install for MinGW. I wish they'd stop spending so much time updating their stupid site layout (confusing me each time in the process) and spend more time updating their software package.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. help understanding free and memory leaks
    By mc61 in forum C Programming
    Replies: 4
    Last Post: 04-08-2008, 12:47 AM
  4. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 03:22 AM
  5. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM