Memory leak during painting

I am trying to create a simple game, but I cannot find a specific memory leak. Every second or so, the program seems to use 3 MB of memory.

The problem is with this drawing method. If I do not name this method, everything will be fine. I am trying to draw a sprite on several parts of the screen:

void Map::draw(HDC hBackBufferDC)   
{  
    for(int i = 0; i < 24; i++)  
    {  
        for(int j = 0; j < 27; j++)  
        {  
            if(mapState[i][j] == 'm')
            {
                blueWall->draw(hBackBufferDC, new Position(j, i));  
            }  
        }
    }
} 

If I remove the draw method call, there is no problem, so the problem is this method:

void StaticSprite::draw(HDC hBackBufferDC, Position* pos)  
{
    int x = (int)pos->x * 22;
    int y = (int)pos->y * 22;

    HGDIOBJ oldObj = SelectObject(this->hSpriteDC, this->hMask);

    BitBlt(hBackBufferDC, x, y, 22, 22, this->hSpriteDC, 0, 0, SRCAND);

    SelectObject(this->hSpriteDC, this->hImage);
    BitBlt(hBackBufferDC, x, y, 22, 22, this->hSpriteDC, 0, 0, SRCPAINT);

    SelectObject(this->hSpriteDC, oldObj);
}

Any idea what causes a memory leak here? I think this is related to this part, but if necessary, I can place other parts of the code.

thank

+3
source share
5 answers

Are you using managed C ++ or not? You assign a new position (24 * 27) times. This causes 648 leaks every time you call Map :: draw.

.

void Map::draw(HDC hBackBufferDC)
    {
    for(int i = 0; i < 24; i++)
       {
       for(int j = 0; j < 27; j++)
         {
           if(mapState[i][j] == 'm') {
              Position tmp(j,i);
              blueWall->draw(hBackBufferDC, &tmp);
           }
         }
       }
    }
    }

Position ! , .

void Map::draw(HDC hBackBufferDC)
    {
    for(int i = 0; i < 24; i++)
       {
       for(int j = 0; j < 27; j++)
         {
           if(mapState[i][j] == 'm') {
              Position *tmp = new Position(j,i);
              blueWall->draw(hBackBufferDC, tmp);
              delete tmp;
           }
         }
       }
    }
    }
+4

new Position() .

if(mapState[i][j] == 'm') {
  Position P(j, i);
  blueWall->draw(hBackBufferDC, &P);
}
+3

As others have pointed out, you should not dynamically highlight a line item.

A more idiomatic solution: remove the β€œnew” ...

blueWall->draw(hBackBufferDC, Position(j, i));

and follow the link const ...

void StaticSprite::draw(HDC hBackBufferDC, const Position& pos) 
{
    int x = (int)pos.x * 22;
    int y = (int)pos.y * 22;
    ...
+2
source

To be more efficient, you have to declare an automatic variable before the loop, and then just update its elements:

void Map::draw(HDC hBackBufferDC)   
{  
    Position pos;
    for(int i = 0; i < 24; i++)  
    {  
        for(int j = 0; j < 27; j++)  
        {  
            if(mapState[i][j] == 'm')
            {
                pos.x = j;
                pos.y = i;
                blueWall->draw(hBackBufferDC, &pos);  
            }  
        }
    }
} 

This solution does not require changing the signature of your method.

+2
source

Instead

blueWall->draw(hBackBufferDC, new Position(j, i));

Why not give it a try:

Position pos(j,i);
blueWall->draw(hBackBufferDC, &pos);
+1
source

Source: https://habr.com/ru/post/1792485/


All Articles