简体   繁体   English

SDL2内存泄漏C ++

[英]SDL2 Memory Leaks C++

So when I run the app, at the biginning every thing runs smooth, but the more it goes, the slower it is. 因此,当我运行该应用程序时,总的来说,每件事都运行顺利,但是它运行的越多,速度就越慢。 I looked at the memory it was using and when it reaches 400 mb it completely stops for 30 secs and then drop back to 200. 我查看了它正在使用的内存,当它达到400 mb时,它完全停止了30秒,然后又下降到200。

I am pretty new to SDL2, and I assume it is because each frame I call: 我对SDL2相当陌生,我认为这是因为我调用的每个帧:

optionsTS = TTF_RenderText_Blended(font, "Options.", blanc);
optionsT = SDL_CreateTextureFromSurface(renderer, optionsTS);

for example and I have plenty of them. 例如,我有很多。

The problem is that I don't know how to delete properly the object each frame, because if I do a SDL_FreeSurface I get an error. 问题是我不知道如何正确删除每一帧的对象,因为如果执行SDL_FreeSurface会出现错误。

I won't publish my whole code because it's a mess, but if you want it, feel free to ask. 我不会发布整个代码,因为那是一团糟,但是如果您愿意,可以随时提出。

Do you know how to fix that? 你知道怎么解决吗?

Just thought I would turn my comment into an answer. 只是以为我会将我的评论变成答案。

In your code you call 在您的代码中调用

optionsTS = TTF_RenderText_Blended(font, "Options.", blanc);
optionsT = SDL_CreateTextureFromSurface(renderer, optionsTS);

every frame, I suspect that if you remove them from there, initialise them outwith of the render loop and simply pass them in as arguments, you should lose the memory leak: the reason being that you will create only one in-memory instance of each and then you can repeatedly use them as needed. 每帧,我怀疑如果从那里删除它们,将它们从render循环中初始化出来,然后简单地将它们作为参数传递进去,则应该会丢失内存泄漏:原因是您将只为每个帧创建一个内存中实例然后您可以根据需要重复使用它们。 On looking at it again, I suspect that you could destroy optionTS once you have made optionT , that way you will save even more memory. 再次查看时,我怀疑一旦制成optionT可以销毁optionTS ,这样可以节省更多的内存。 (not tested yet as my main machine just crashed this weekend, and I am still re-installing drivers and VS2010) (尚未测试,因为我的主机本周末刚刚崩溃,并且我仍在重新安装驱动程序和VS2010)

As a general rule, try and not create/destroy any objects in the render loop, tends to get big and messy fast. 作为一般规则,请尝试不要在渲染循环中创建/销毁任何对象,否则它们会很快变得大而混乱。

Consider taking advantage of RAII in C++ if possible. 如果可能,请考虑利用C ++中的RAII。 For example, create a class that wraps an SDL_Surface and calls SDL_FreeSurface in the destructor. 例如,创建一个包装SDL_Surface的类,并在析构函数中调用SDL_FreeSurface。

class MySurface
{
public:
    MySurface(SDL_Surface & surface) : m_surface(surface) {}
    ~MySurface() {SDL_FreeSurface(m_surface);}

    SDL_Surface & GetSDLSurface() {return m_surface;}

private:
    SDL_Surface & m_surface;
};

You would then create an instance of MySurface every time you grabbed an SDL_Surface from the SDL API, and you won't have to worry about when or whether to free that surface. 然后,每次您从SDL API中获取SDL_Surface时,您都将创建MySurface的实例,而不必担心何时或是否要释放该表面。 The surface will be freed as soon as your instance of MySurface goes out of scope. 一旦您的MySurface实例超出范围,该表面将被释放。

I'm certain better implementations can be written and tailored to your needs, but at a minimum something similar to this may prevent you from having leaks in the future. 我敢肯定,可以编写出更好的实现,并根据您的需求进行量身定制,但至少类似的事情可能会阻止您将来的泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM