程序起初不卡,但是久一点就越来越卡了
sdlgui吧
全部回复
仅看楼主
level 11
最后卡死,网上查了一下发现时内存释放的问题,但是我不知道怎么做?求指导。
没有new之类的,好像书里都没有说如何释放内存...
@twtfcu3
2014年08月10日 10点08分 1
level 10
要查源码才行的
2014年08月10日 10点08分 2
楼下
2014年08月10日 11点08分
level 11
for(int i=0;i!=val;++i){//这是一小部分代码
int ts=SDL_GetTicks();
for(auto it=rect.begin();it!=rect.end();++it)
if((*it)[2]!=0){
(*it)[1]-=(*it)[2];
moved=true;}
SDL_RenderClear(ren);
draw(rect);//绘图
SDL_RenderPresent(ren);
int te=SDL_GetTicks();
cout<<te-ts<<endl;//这里看一下每次需要的时间,起初是个位数到后面成了3,4位数[狂汗]
if((te-ts)<(1000/24))
SDL_Delay(1000/24-(te-ts));
}
2014年08月10日 11点08分 3
level 10
你说的卡与内存无关吧,可能是算法问题,这段代码没有申请内存
看看这个函数draw(rect);//绘图
2014年08月10日 11点08分 4
level 11
void draw(vector<vector<int>> rect)
{
for(auto it=rect.begin();it!=rect.end();++it)
{
pos.x=(*it)[0],pos.y=(*it)[1];
SDL_RenderDrawRect(ren, &pos);
SDL_SetRenderDrawColor(ren,0,0,0,0);
SDL_RenderCopy(ren,RenderText((cnintostr((*it)[3])).c_str(),"a.ttf",numcolor[choosecolor((*it)[3])],64),NULL,&pos); //“numcolor[choosecolor((*it)[3])],64)”这个是选择颜色
}
SDL_RenderCopy(ren,RenderText(("SCORE:"+cnintostr(tscore)).c_str(),"a.ttf",numcolor[0],64),NULL,&pos1);//cnintostr(tscore))这个函数是数字转字符串
}
但是为什么前面不卡,后面越来越卡了呢?如何是算法问题的话,前面也应该会卡呀...
2014年08月10日 11点08分 5
贪方便把函数都写一行了。。。
2014年08月10日 11点08分
回复 闪光点22的xh :@twtfcu3
2014年08月10日 11点08分
RenderText这是一个自定义类,你测试过没,是否会出现内存没有回收的情况
2014年08月10日 11点08分
回复 twtfcu3 :好像是一个自定义函数
2014年08月10日 11点08分
level 11
运行程序时开了任务管理器看cpu发现50%居高不下,pf从1.95g慢慢升到2.55g(运行了有一会儿)...
2014年08月10日 11点08分 6
发现卡死的时候cpu直降到20%-30%不过不知道前后顺序,我把窗口关了发现仍然占了50%,此时没关控制台。
2014年08月10日 11点08分
level 11
SDL_Texture* RenderText(std::string message, std::string fontFile,SDL_Color color, int fontSize)
{
TTF_Font *font = nullptr;
font = TTF_OpenFont(fontFile.c_str(), fontSize);
SDL_Surface *surf = TTF_RenderUTF8_Blended(font, message.c_str(), color);
SDL_Texture *texture = SDL_CreateTextureFromSurface(ren, surf);
SDL_FreeSurface(surf);
TTF_CloseFont(font);
return texture;
}
2014年08月10日 11点08分 7
有freesurface
2014年08月10日 11点08分
回复 闪光点22的xh :texture对象是谁释放了
2014年08月10日 11点08分
回复 twtfcu3 :return了一个texture...难道返回的也要释放...,话说返回之后怎么释放,并没有一个变量给destroytexture呀...
2014年08月10日 12点08分
回复 闪光点22的xh :我只能告诉你,这个要释放,但具体的要你处理了
2014年08月10日 12点08分
level 1
直接传一个vec的容器是找死么。。。
2014年08月11日 23点08分 8
那玩意不到死亡期是不会释放内存的。
2014年08月11日 23点08分
回复 吞薯条 :他传的是临时变量,生存期只在这次函数调用的
2014年08月12日 00点08分
回复 twtfcu3 :[汗]他传了一个VEC的容器,每次进入循环都必须将容器里面的数据重新拷贝一次,效率太低了。
2014年08月12日 00点08分
回复 吞薯条 :那到时真的,为什么他不用引用呢。。。。。。或指针也行啊
2014年08月12日 01点08分
1