开发者

Learning C++ and SDL- does the following generate a memory leak?

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 ra开发者_StackOverflow社区ther 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.


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.


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.


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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜