Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's look at each piece of code.</p> <p><strong>1) Allocating memory</strong></p> <pre><code>Tile* tile = new Tile(); </code></pre> <p>This creates a new Tile object on the heap and stores the memory address in the variable tile. Keep in mind that the variable tile is only a pointer, not the object itself.</p> <p><strong>2) Copying the reference</strong></p> <pre><code>void Level::addTile(int x, int y, Tile *tile) { map[x][y] = tile;} </code></pre> <p>The above function simply takes a pointer and saves it into a multi-dimensional array for future use. In the context of all of the code listed, there will now be TWO references to the object...the tile* in the original calling function and the entry in the multi-dimensional array. Again, keep in mind that these are pointers (just 4 bytes, depending on your system architecture).</p> <p><strong>3) Setting a pointer to NULL</strong></p> <pre><code>tile = NULL; </code></pre> <p>The result of this code is that the pointer variable tile will no longer point to the created object on the heap. The object will still exist, though. At this point, in the context of all of the code, you will still have a pointer to the object due to the map[][] array.</p> <p>In reality, you do not need this line of code. tile isn't a dangling pointer, as it points a valid object (before you set it to NULL). The code does not destroy the object either. Since tile is a local variable, it will be cleaned up when it function scope exits. Only the pointer will be cleaned up, not the object it points to. Setting it to NULL in this scenario accomplishes very little except waste cycles.</p> <p>It is also not a memory leak per say. You still have a valid pointer to the object in the map[][] array, so you can always use that reference to clean up the memory.</p> <p><strong>4) What you need to do</strong></p> <p>You need to delete the object somewhere. In C++, this is the delete keyword.</p> <pre><code>delete tile; </code></pre> <p>OR</p> <pre><code>delete map[x][y]; </code></pre> <p>Now, keep in mind, once the above code runs, the memory on the heap will be freed back to the operating system and your application can no longer access the memory safely. Therefore, any call to map[x][y]->{SomeMethod} would result in a access violation exception. The pointer stored in map[x][y] is now a dangling pointer (it is pointing to a memory address that not valid for that type).</p> <p>If you need to remove the tile from the level map before you destroy the level, you would do this:</p> <pre><code>void Level::deleteTile(int x, int y) { if (map[x][y] != NULL) { delete map[x][y]; map[x][y] = NULL; } } </code></pre> <p>I would also change the addTile method to something like this:</p> <pre><code>void Level::addTile(int x, int y, Tile *tile) { deleteTile(x, y); map[x][y] = tile; } </code></pre> <p>Lastly, when you are destorying a Level object, you'll need to do something like this:</p> <pre><code>void ~Level() { for (int i; i&lt;MaxX; i++) { for (int j; j&lt;MaxY; j++) { delete map[i][j]; } } } </code></pre> <p>When you're creating a Level object, you should zero out the map[][] array so that all values will be NULL as well. Since the map array is not local to a function, it is a good idea to set all of its pointer values to NULL so that you know when it contains a valid pointer and when it doesn't.</p> <p>If you dynamically create (using the new keyword) the map[][] array, you'll need to clean up its memory using the delete keyword too.</p> <p>Hope this helps. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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