Hello! There is a Bitmap *bm , and the task of my program is to constantly copy arbitrary (but within the limits of the bitmap) areas of this bitmap, and draw them

 Bitmap *bm=0; void Create() { if(bm) delete bm; bm=new Bitmap(...); //рисуем в битмап, здесь всё нормально Draw(); } void Draw(HDC hdc) { Graphics g(hdc); g.DrawImage(bm->Clone(...),...); //клонируем, рисуем //потом снова вызываем эту функцию, и снова } 

The problem is that during the call to this function, the memory used by my program is constantly increasing, this is a memory leak, right?

In all forums, sites they write that GDI + objects are released themselves, but something is not noticeable. Maybe the error is not in this? What is there to do?

    1 answer 1

    Well, because you have such an obvious leak that you can miss it only if you close your eyes.

     auto clone = bm->Clone(...); g.DrawImage(clone,...); delete clone; 

    See the example on MSDN: Bitmap.Clone ()

    • So you need to call delete only if allocated memory with help. new - Arthur Klochko
    • one
      @ArturKlochko, obviously new is called inside Clone() - ixSci Sept
    • Oh, now I will try - Arthur Klochko
    • Yes, the problem is this - Arthur Klochko