Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows API
  4. C/C++ - Win32 : how can i find memory leaks?

C/C++ - Win32 : how can i find memory leaks?

Scheduled Pinned Locked Moved Windows API
graphicsquestionc++testingbeta-testing
9 Posts 4 Posters 75 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11511469
    wrote on last edited by
    #1

    i did a big program. now i have several memory leaks and they are hard to find. i did find several GDI memory leaks and i did MemoryDC and BitmapDC class's for avoid several memory leaks ;)

    class MemoryDC
    {
    private:
    HDC memoryDC;

    public:
    MemoryDC ()
    {
    memoryDC=CreateCompatibleDC(NULL);
    }

    operator HDC() const
    {
        return memoryDC;
    }
    
    ~MemoryDC ()
    {
       DeleteDC(memoryDC);
    }
    

    };

    class BitmapDC
    {
    private:
    MemoryDC hdcbitmap;
    HGDIOBJ bitmapold;
    HBITMAP bitmapcurrent;
    int intwidth;
    int intheight;

    void init(int width, int height)
    {
        bitmapcurrent = CreateBitmap(width, height, 1, 32, NULL);
        bitmapold = SelectObject(hdcbitmap, bitmapcurrent);
        intwidth=width;
        intheight=height;
    }
    
    void destroy()
    {
        SelectObject(hdcbitmap, bitmapold);
        DeleteObject(bitmapcurrent);
    }
    

    public:

    BitmapDC(int width, int height)
    {
        init(width, height);
    }
    
    BitmapDC()
    {
        init(1, 1);
    }
    
    void size(int width, int height)
    {
        destroy();
        init(width, height);
    }
    
    int Width()
    {
        return intwidth;
    }
    
    int Height()
    {
        return intheight;
    }
    
    BitmapDC& operator= (const BitmapDC &bitmapsource)
    {
        if (this == &bitmapsource)      // Same object?
            return \*this;
        destroy();
        init(bitmapsource.intwidth, bitmapsource.intheight);
        BitBlt(bitmapsource, 0, 0, intwidth, intheight, bitmapsource, 0, 0, SRCCOPY);
        return \*this;
    }
    
    BitmapDC& operator= (const HBITMAP &bitmapsource)
    {
        destroy();
        BITMAP bm;
        GetObject(bitmapsource,sizeof(bm),&bm);
        init(bm.bmWidth, bm.bmHeight);
        DrawHBITMAPtoHDC(bitmapsource,hdcbitmap);
        return \*this;
    }
    
    //testing the bitmao value if is nullptr
    bool operator != ( nullptr\_t ) const
    {
        return bitmapcurrent != nullptr;
    }
    
    bool operator==(const BitmapDC &other) const
    {
        return (other.bitmapcurrent == this->bitmapcurrent);
    }
    
    bool operator!=(const BitmapDC &other) const
    {
        return !(\*this == other);
    }
    
    operator HBITMAP() const
    {
        return bitmapcurrent;
    }
    
    operator HDC() const
    {
        return hdcbitmap;
    }
    
    ~BitmapDC()
    {
       destroy();
    }
    

    };

    like you

    L J 2 Replies Last reply
    0
    • U User 11511469

      i did a big program. now i have several memory leaks and they are hard to find. i did find several GDI memory leaks and i did MemoryDC and BitmapDC class's for avoid several memory leaks ;)

      class MemoryDC
      {
      private:
      HDC memoryDC;

      public:
      MemoryDC ()
      {
      memoryDC=CreateCompatibleDC(NULL);
      }

      operator HDC() const
      {
          return memoryDC;
      }
      
      ~MemoryDC ()
      {
         DeleteDC(memoryDC);
      }
      

      };

      class BitmapDC
      {
      private:
      MemoryDC hdcbitmap;
      HGDIOBJ bitmapold;
      HBITMAP bitmapcurrent;
      int intwidth;
      int intheight;

      void init(int width, int height)
      {
          bitmapcurrent = CreateBitmap(width, height, 1, 32, NULL);
          bitmapold = SelectObject(hdcbitmap, bitmapcurrent);
          intwidth=width;
          intheight=height;
      }
      
      void destroy()
      {
          SelectObject(hdcbitmap, bitmapold);
          DeleteObject(bitmapcurrent);
      }
      

      public:

      BitmapDC(int width, int height)
      {
          init(width, height);
      }
      
      BitmapDC()
      {
          init(1, 1);
      }
      
      void size(int width, int height)
      {
          destroy();
          init(width, height);
      }
      
      int Width()
      {
          return intwidth;
      }
      
      int Height()
      {
          return intheight;
      }
      
      BitmapDC& operator= (const BitmapDC &bitmapsource)
      {
          if (this == &bitmapsource)      // Same object?
              return \*this;
          destroy();
          init(bitmapsource.intwidth, bitmapsource.intheight);
          BitBlt(bitmapsource, 0, 0, intwidth, intheight, bitmapsource, 0, 0, SRCCOPY);
          return \*this;
      }
      
      BitmapDC& operator= (const HBITMAP &bitmapsource)
      {
          destroy();
          BITMAP bm;
          GetObject(bitmapsource,sizeof(bm),&bm);
          init(bm.bmWidth, bm.bmHeight);
          DrawHBITMAPtoHDC(bitmapsource,hdcbitmap);
          return \*this;
      }
      
      //testing the bitmao value if is nullptr
      bool operator != ( nullptr\_t ) const
      {
          return bitmapcurrent != nullptr;
      }
      
      bool operator==(const BitmapDC &other) const
      {
          return (other.bitmapcurrent == this->bitmapcurrent);
      }
      
      bool operator!=(const BitmapDC &other) const
      {
          return !(\*this == other);
      }
      
      operator HBITMAP() const
      {
          return bitmapcurrent;
      }
      
      operator HDC() const
      {
          return hdcbitmap;
      }
      
      ~BitmapDC()
      {
         destroy();
      }
      

      };

      like you

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Member 11545824 wrote:

      can anyone advice me?

      Why are you duplicating what is already provided by the Windows GDI+ classes[^]?

      U 1 Reply Last reply
      0
      • U User 11511469

        i did a big program. now i have several memory leaks and they are hard to find. i did find several GDI memory leaks and i did MemoryDC and BitmapDC class's for avoid several memory leaks ;)

        class MemoryDC
        {
        private:
        HDC memoryDC;

        public:
        MemoryDC ()
        {
        memoryDC=CreateCompatibleDC(NULL);
        }

        operator HDC() const
        {
            return memoryDC;
        }
        
        ~MemoryDC ()
        {
           DeleteDC(memoryDC);
        }
        

        };

        class BitmapDC
        {
        private:
        MemoryDC hdcbitmap;
        HGDIOBJ bitmapold;
        HBITMAP bitmapcurrent;
        int intwidth;
        int intheight;

        void init(int width, int height)
        {
            bitmapcurrent = CreateBitmap(width, height, 1, 32, NULL);
            bitmapold = SelectObject(hdcbitmap, bitmapcurrent);
            intwidth=width;
            intheight=height;
        }
        
        void destroy()
        {
            SelectObject(hdcbitmap, bitmapold);
            DeleteObject(bitmapcurrent);
        }
        

        public:

        BitmapDC(int width, int height)
        {
            init(width, height);
        }
        
        BitmapDC()
        {
            init(1, 1);
        }
        
        void size(int width, int height)
        {
            destroy();
            init(width, height);
        }
        
        int Width()
        {
            return intwidth;
        }
        
        int Height()
        {
            return intheight;
        }
        
        BitmapDC& operator= (const BitmapDC &bitmapsource)
        {
            if (this == &bitmapsource)      // Same object?
                return \*this;
            destroy();
            init(bitmapsource.intwidth, bitmapsource.intheight);
            BitBlt(bitmapsource, 0, 0, intwidth, intheight, bitmapsource, 0, 0, SRCCOPY);
            return \*this;
        }
        
        BitmapDC& operator= (const HBITMAP &bitmapsource)
        {
            destroy();
            BITMAP bm;
            GetObject(bitmapsource,sizeof(bm),&bm);
            init(bm.bmWidth, bm.bmHeight);
            DrawHBITMAPtoHDC(bitmapsource,hdcbitmap);
            return \*this;
        }
        
        //testing the bitmao value if is nullptr
        bool operator != ( nullptr\_t ) const
        {
            return bitmapcurrent != nullptr;
        }
        
        bool operator==(const BitmapDC &other) const
        {
            return (other.bitmapcurrent == this->bitmapcurrent);
        }
        
        bool operator!=(const BitmapDC &other) const
        {
            return !(\*this == other);
        }
        
        operator HBITMAP() const
        {
            return bitmapcurrent;
        }
        
        operator HDC() const
        {
            return hdcbitmap;
        }
        
        ~BitmapDC()
        {
           destroy();
        }
        

        };

        like you

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        For GDI memory leaks see the MSDN blog Debugging a GDI Resource Leak[^]. You should also check the rest of your program for non-GDI memory leaks. Don't forget to check for library and Windows API functions that may allocate memory.

        U 1 Reply Last reply
        0
        • J Jochen Arndt

          For GDI memory leaks see the MSDN blog Debugging a GDI Resource Leak[^]. You should also check the rest of your program for non-GDI memory leaks. Don't forget to check for library and Windows API functions that may allocate memory.

          U Offline
          U Offline
          User 11511469
          wrote on last edited by
          #4

          how can check the rest of your program for non-GDI memory leaks, if i don't know how? :( what i can do, like i did with joystick, is comment some code, until i see where is it ;)

          J D 2 Replies Last reply
          0
          • L Lost User

            Member 11545824 wrote:

            can anyone advice me?

            Why are you duplicating what is already provided by the Windows GDI+ classes[^]?

            U Offline
            U Offline
            User 11511469
            wrote on last edited by
            #5

            why???? i never used GDI+ ;)

            L 1 Reply Last reply
            0
            • U User 11511469

              why???? i never used GDI+ ;)

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Exactly so, but why not? It is a fully tested set of classes provided by Microsoft, so it is much less likely to be causing the problems you are having with your own classes.

              1 Reply Last reply
              0
              • U User 11511469

                how can check the rest of your program for non-GDI memory leaks, if i don't know how? :( what i can do, like i did with joystick, is comment some code, until i see where is it ;)

                J Offline
                J Offline
                Jochen Arndt
                wrote on last edited by
                #7

                Finding memory leaks is a difficult task. First you need to detect memory leaks. This can be done by adding some checks to your application (usually in debug builds). See the MSDN articles Finding Memory Leaks[^] and Using UMDH to Find a User-Mode Memory Leak[^]. There are also some tools to help you. An example is this CP article: Memory(-Leak) and Exception Trace (CRT and COM Leaks)[^]. Then execute your application and start specific parts to see where the leaks occur. This should give you the information which source files may contain a leak. Finally check the source files.

                U 1 Reply Last reply
                0
                • J Jochen Arndt

                  Finding memory leaks is a difficult task. First you need to detect memory leaks. This can be done by adding some checks to your application (usually in debug builds). See the MSDN articles Finding Memory Leaks[^] and Using UMDH to Find a User-Mode Memory Leak[^]. There are also some tools to help you. An example is this CP article: Memory(-Leak) and Exception Trace (CRT and COM Leaks)[^]. Then execute your application and start specific parts to see where the leaks occur. This should give you the information which source files may contain a leak. Finally check the source files.

                  U Offline
                  U Offline
                  User 11511469
                  wrote on last edited by
                  #8

                  sorry i use GNU compiler

                  1 Reply Last reply
                  0
                  • U User 11511469

                    how can check the rest of your program for non-GDI memory leaks, if i don't know how? :( what i can do, like i did with joystick, is comment some code, until i see where is it ;)

                    D Offline
                    D Offline
                    Daniel Pfeffer
                    wrote on last edited by
                    #9

                    Member 11545824 wrote:

                    how can check the rest of your program for non-GDI memory leaks, if i don't know how? :(

                    One way is to replace operators new(), delete(), etc. with operators that log memory allocations. For example, an pseudo-code implementation of new() and delete() could look like this:

                    std::map< void*, std::size_t > allocations;

                    void* operator new( std::size_t size )
                    {
                    void* p = std::malloc( size );

                    if ( p != nullptr )
                    {
                    allocations.insert( make_pair( p, size ) );
                    return p;
                    }

                    throw std::bad_alloc();
                    }

                    void operator delete( void* p )
                    {
                    if ( allocations.find( p ) == allocations.end() )
                    {
                    // handle case of deallocation of unallocated memory
                    }

                    std::free( p );
                    allocations.erase( allocations.find( p ) );
                    

                    }

                    Note that the code will probably not compile; I just wrote it quickly as an example. An additional function, called when your program exits, can ensure that the allocations map is empty. If it is not, the size(s) of the blocks may give you an idea as to what blocks were not freed.

                    If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups