学习 C++和 SDL- 以下是否会产生内存泄漏?

发布于 2024-10-10 08:33:02 字数 1021 浏览 5 评论 0原文

我正在自学一些 C++,但我不完全确定我对内存管理有很好的掌握。我只懂 Java 以及一点 PHP 和 Python,所以这对我来说有点新鲜。我也在与 SDL 合作——这似乎是一种加速学习过程的有趣方式。不管怎样,我正在尝试编写一个清理函数来释放所有已传递到堆栈的表面(我只是使用堆栈STL)。所以,我有以下代码(缩写):

#include <stack>

//stack of SDL_Surfaces  
stack<SDL_Surface*> surfaces;

void clean() {      
    SDL_Surface *temp = NULL;

    //loops through the stack depending on its size
    while (surfaces.size() != 0) {
        temp = surfaces.top();
        SDL_FreeSurface(temp);
        surfaces.pop();

    } //while
    if (surfaces.size() == 0) {
        cout << "cleanup worked correctly" << endl;     
    } //if
}

//loading an image (this is in the main function)
background = load_image( "background.bmp" );
surfaces.push(background);

//cleaning time
clean();

我不确定清理方法。我认为这将是实现 SDL_FreeSurface 函数的更好方法,而不是手动指定每个表面。因此,如果我在屏幕上绘制十个图像(例如十艘星舰)然后将它们炸毁,我需要正确删除它们。我会为这 10 艘星舰创建一个堆栈,然后在它们被摧毁时我可以将它们全部消灭,如果这有意义的话。我担心我把事情变得过于复杂并引入了一种全新的方法来导致内存泄漏。

任何反馈/评论将不胜感激!我是 C++ 新手,所以请随意嘲笑我在内存管理方面徒劳的尝试。

I'm learning a bit of C++ on my own, and I'm not entirely sure I have a good grasp on memory management. I only know Java and a bit of PHP and Python, so this is a bit new for me. I'm working with SDL as well- it seemed like a interesting way to accelerate the learning process. Anyways, I'm trying to write a cleanup function that frees all the surfaces that have been passed to a stack (I'm just using the stack STL). So, I have the following code (abbreviated):

#include <stack>

//stack of SDL_Surfaces  
stack<SDL_Surface*> surfaces;

void clean() {      
    SDL_Surface *temp = NULL;

    //loops through the stack depending on its size
    while (surfaces.size() != 0) {
        temp = surfaces.top();
        SDL_FreeSurface(temp);
        surfaces.pop();

    } //while
    if (surfaces.size() == 0) {
        cout << "cleanup worked correctly" << endl;     
    } //if
}

//loading an image (this is in the main function)
background = load_image( "background.bmp" );
surfaces.push(background);

//cleaning time
clean();

I'm unsure about the cleanup method. I thought this would be a better way to implement the SDL_FreeSurface function rather than manually specifying each surface. So if I drew ten images to the screen (say ten starships) and then blew them up, I would need to properly delete them. I would create a stack for these 10 starships, and then upon their destruction I could wipe them all out, if that makes sense. I'm worried that I overly complicated things and introduced a whole new way to cause memory leaks.

Any feedback/commentary would be much appreciated! I'm new to C++ so feel free to mock my futile attempt at memory management.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

偏爱自由 2024-10-17 08:33:02

您自己的代码没有任何内存泄漏。假设 load_image() 也是安全的,则整个代码不会出现内存泄漏。

请记住,如果不使用 new 运算符,则不会发生内存泄漏。

Your own code does not have any memory leakage. Assuming load_image() is safe too, the entire code is clean of memory leakages.

Remember that, if you don't use the new operator, no memory leakage occur.

岁月流歌 2024-10-17 08:33:02

这似乎没有任何内存泄漏。但是,您需要确保不要将表面保留太久,否则可能会出现泄漏(在 Java 中也可能发生同样的情况)。假设 clean 被调用得足够频繁,那么应该没问题。

这将清理各个表面(通过在适当的时间调用 SDL_FreeSurface)的问题转移到清理表面堆栈(通过在适当的时间调用 clean)。如果您已经有用于管理堆栈的代码(或想法),我会继续运行它。如果没有,我会尝试找到一种单独管理表面寿命的方法,因为这种方法更灵活并且不会引入新概念。

This doesn't appear to have any memory leaks. However, you want to make sure you don't keep surfaces around for too long, or it may appear like you have leaks (the same thing can happen in Java). Assuming that clean is called often enough, you should be fine.

This moves the problem of cleaning up the individual surfaces (by calling SDL_FreeSurface at the appropriate time) to cleaning up the stack of surfaces (by calling clean at the appropriate time). If you already have code (or an idea) in place for managing the stack, I'd go ahead and run with it. If not, I'd try to find a way to manage the surface lifetimes individually, since that approach is more flexible and doesn't introduce a new concept.

娇女薄笑 2024-10-17 08:33:02

您正在做的事情有点令人困惑,但是,如果 SDL_FreeSurface() 释放对象(???),那么我没有看到内存泄漏。

你想做什么? SDL_FreeSurface() 的作用是什么? load_image() 是做什么的?如果有的话,它是如何分配内存的?

It's a little confusing what you are doing but, if SDL_FreeSurface() frees the object (???) then I don't see a memory leak.

What are you trying to do? What does SDL_FreeSurface() do? What does load_image() do and how does it allocate the memory if it does at all?

千纸鹤 2024-10-17 08:33:02

阅读RAII - C++ 非常擅长于此。一旦您了解发生了什么(基本上是构造函数中的资源分配和析构函数中的释放 - 意味着您的对象会自行清理),您就可以为 SDL 函数创建漂亮的包装器。以下是创建表面的方法:

CBitmapSurface::CBitmapSurface(const std::string &filename) 
{
    m_pSurface = SDL_LoadBMP(filename.c_str());
}

CBitmapSurface::~CBitmapSurface()
{
    SDL_FreeSurface(m_pSurface); 
}

其中 m_pSurface 是 SDL_Surface* 类型的成员变量。

在完美的世界中,您希望使此类不可复制,如果使用 c++0x 则处理移动构造函数并检查错误代码并引发异常。但这应该让你开始。

Have a read about RAII - C++ is very good at it. Once you understand what is happening (basically it is resource allocation in the constructor and deallocation in the destructor - meaning your objects clean themselves up) - you can create nice wrappers for the SDL functions. Here's how you'd do a surface:

CBitmapSurface::CBitmapSurface(const std::string &filename) 
{
    m_pSurface = SDL_LoadBMP(filename.c_str());
}

CBitmapSurface::~CBitmapSurface()
{
    SDL_FreeSurface(m_pSurface); 
}

where m_pSurface is a member variable of type SDL_Surface*.

In the perfect world you'd want to make this class non-copyable, handle the move constructor if using c++0x and check error codes and throw exceptions. But this should get you started.

半世晨晓 2024-10-17 08:33:02

到目前为止,查找内存泄漏的最佳方法是使用 valgrind。据我所知,它只能在 Linux 上运行,但非常值得花时间研究和使用。

the best way by far to look for memory leaks is use valgrind. As far as I know it only works on linux, but it is well worth the time to investigate and use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文