Note that there are some explanatory texts on larger screens.

plurals
  1. POLearning C++ and SDL- does the following generate a memory leak?
    text
    copied!<p>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):</p> <pre><code>#include &lt;stack&gt; //stack of SDL_Surfaces stack&lt;SDL_Surface*&gt; 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 &lt;&lt; "cleanup worked correctly" &lt;&lt; endl; } //if } //loading an image (this is in the main function) background = load_image( "background.bmp" ); surfaces.push(background); //cleaning time clean(); </code></pre> <p>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.</p> <p>Any feedback/commentary would be much appreciated! I'm new to C++ so feel free to mock my futile attempt at memory management.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload