内存使用随着 C++ 程序不断增加 运行
memory usage keep increasing as the C++ program running
我编写了一个程序来在屏幕上移动一个点。我基本上只是在重新创建 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 和 WEATHER VANE,解决方案非常简单!!!!显然我把这些代码行放在一个循环中。它的主要功能基本上是创建纹理。并且它一遍又一遍地加载纹理,因为它在一个循环中。我只需要删除它。
这些行:
if (!textTexture.loadTextFromFile(timeText.str().c_str(), textColor))
{
printf("unable to render text ypoooooo");
}
谢谢你们!!! :)
对不起,我不能投票,显然它需要更多的声誉来投票回答。 :|
我怀疑这只在 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
我编写了一个程序来在屏幕上移动一个点。我基本上只是在重新创建 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 和 WEATHER VANE,解决方案非常简单!!!!显然我把这些代码行放在一个循环中。它的主要功能基本上是创建纹理。并且它一遍又一遍地加载纹理,因为它在一个循环中。我只需要删除它。 这些行:
if (!textTexture.loadTextFromFile(timeText.str().c_str(), textColor))
{
printf("unable to render text ypoooooo");
}
谢谢你们!!! :) 对不起,我不能投票,显然它需要更多的声誉来投票回答。 :|
我怀疑这只在 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