【问题标题】:memory usage keep increasing as the C++ program running [closed]随着 C++ 程序的运行,内存使用量不断增加[关闭]
【发布时间】:2015-04-05 18:13:59
【问题描述】:

我制作了一个程序来在屏幕上移动一个点。我基本上只是在重新创建lazyfoo sdl 教程。 问题是我的程序吃掉了内存。它的内存使用量以 200kb/s 的速度缓慢增加。随着时间的推移,它会变得更大。

我使用视觉泄漏检测器对其进行了测试,它说没有内存泄漏。 所以我做错了什么? 请帮我。快把我逼疯了。

这里是代码:感谢lazyfoo。 如果代码太长或不清楚,我会根据需要进行编辑。 谢谢。

//Main Stage
int main(int argc, char* args[])
{
    if (!init())
    {
        printf("failed to init! \n");
    }
    else
    {
        cTile *tileSet[TOTAL_TILES];

        if (!loadMedia(tileSet))
        {
            printf("failed to load media! \n");
        }
        else
        {
            bool running = true;
            SDL_Rect wall;
            wall.x = 200;
            wall.y = 200;
            wall.h = 150;
            wall.w = 70;
            cDot Dot1(1270, 720);
            cDot Dot2(SCREEN_HEIGHT / 4, SCREEN_WIDTH / 4);
            cTimer timer;
            SDL_Event e;
            SDL_Color textColor = { 0, 0, 120, 120 };

            std::vector<SDL_Rect> camera;
            camera.resize(1);
            camera[0] = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };

            Uint32 startTime = 0;
            std::stringstream timeText;
            int countedFrames = 0;
            timer.start();

            while (running)
            {
                while (SDL_PollEvent(&e) != 0)
                {
                    if (e.type == SDL_QUIT)
                    {
                        running = false;
                    }

                    Dot1.handleEvent(e);

                    for (int i = 0; i < TOTAL_BUTTONS; i++)
                    {
                        buttons[i].buttonEvent(&e);
                    }
                }

                float avgFPS = countedFrames / (timer.getTicks() / 1000.f);
                if (avgFPS > 2000)
                {
                    avgFPS = 0;
                }

                if (!textTexture.loadTextFromFile(timeText.str().c_str(), textColor))
                {
                    printf("unable to render text ypoooooo");
                }

                timeText.str("");
                timeText << "fps " << avgFPS;

                Dot1.move(tileSet);
                Dot1.setCamera(camera[0]);

                /*camera.x = (Dot1.getPosX() + cDot::DOT_WIDTH / 2) - SCREEN_WIDTH / 2;
                camera.y = (Dot1.getPosY() + cDot::DOT_HEIGHT / 2) - SCREEN_HEIGHT / 2;*/

                /*if (camera.x < 0){ camera.x = 0; }
                if (camera.y < 0){ camera.y = 0; }
                if (camera.x > LEVEL_WIDTH - camera.w){ camera.x = LEVEL_WIDTH - camera.w; }
                if (camera.y > LEVEL_HEIGHT - camera.h){ camera.y = LEVEL_HEIGHT - camera.h; }*/

                SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
                SDL_RenderClear(renderer);

                //SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
                //SDL_RenderDrawRect(renderer, &wall);
                for (int i = 0; i < TOTAL_TILES; ++i)
                {
                    tileSet[i]->render(camera);
                }
                //BGTexture.render(0, 0, &camera);
                Dot1.render(camera[0]);

                textTexture.render(250, 250);
                timeTextTexture.render(250, 250 + textTexture.getWidth());
                for (int i = 0; i < 2; i++)
                {
                    //buttons[i].render();
                }


                //Dot2.render(camera.x, camera.y);

                SDL_RenderPresent(renderer);
                if (timer.started())
                {
                    countedFrames++;
                }
                else if (!timer.started())
                {
                    countedFrames = 0;
                }
            }
        }
        close(tileSet);
    }
    return 0;
}

编辑:

感谢 PAUL 和风向标,解决方案如此简单!!!!显然我把这些代码行放在一个循环中。它的主要功能基本上是创建纹理。它一遍又一遍地加载纹理,因为它在循环中。我只需要删除它。 这些行:

if (!textTexture.loadTextFromFile(timeText.str().c_str(), textColor))
                    {
                        printf("unable to render text ypoooooo");
                    }

谢谢你们!!! :) 抱歉,我不能投票,显然它需要更多的声誉来投票。 :|

【问题讨论】:

  • 今天不在我的待办事项清单上的事情:挖掘 1000 行代码以查找某人的内存泄漏。
  • 显示分配发生的位置
  • 我认为你需要确定除了运行内存分析器之外你还做了什么。换句话说,找出你认为可能有问题的地方。它可能与分配内存有关,但不释放它,指向空的指针等。识别代码中的那些点,除非你根本不知道如何弄清楚。
  • 是的,让别人挖掘我的代码似乎太过分了。我的错。对不起,伙计们。我会删除我的问题。谢谢。
  • @user3189174 类似 valgrind 的东西可能会派上用场。

标签: c++ memory-leaks sdl


【解决方案1】:

我怀疑这只会在 NULL 时删除。

for (int i = 0; i < TOTAL_TILES; ++i)
{
    if (tiles[i] == NULL)
    {
        delete tiles[i];
        tiles[i] = NULL;
    }
}

编辑:

这是另一个镜头:您在主循环重复中调用loadTextFromFile(),但只在最后的close() 中调用SDL_DestroyTexture()

这里有一个类似的问题,Memory leak SDL while using SDL_CreateTextureFromSurface

【讨论】:

  • 那是英雄事迹!
  • 我也更改了它,检查它是否不为 NULL,没有帮助。除此之外,它仅在我关闭程序时才有效。顺便谢谢你
  • @user3189174 - 无论如何,您都不需要检查 NULL / not NULL。删除空指针是完全合法和安全的。
  • @PaulMcKenzie 虽然,由于 OP 应用了这个条件,它显然应该是 if (tiles[i] != NULL),不是吗?
  • 我现在已经删除了 NULL 检查器。
猜你喜欢
  • 1970-01-01
  • 2013-08-27
  • 1970-01-01
  • 1970-01-01
  • 2016-02-04
  • 1970-01-01
  • 1970-01-01
  • 2015-07-09
  • 2023-03-07
相关资源
最近更新 更多