   Releasing com objects and deleting pointers
  Hey guys,Wanting to get some clarification on cleaning up memory.As I understand it if you have a pointer to some object then you unallocate the memory with delete.And if you have a com object you unallocate the memory by using release function.My question is what if you have a pointer that pointers to a com object? Do you use delete or release? I have been using delete only in this situation.What if you have pointers to an array of com objects?EXAMPLE 1int *intArray = new int [5];for (int i = 0; i < 5; i++){// Deleting the individual elements firstdelete intArray ;ORdelete[] intArray ;// I have seen code snippets doing both ways... but which way is the correct way?}// Deleting the actual array nowdelete [] intArray;With Example 1, which way is the correct way?delete intArray  OR delete[] intArray ?EXAMPLE 2IDirect3DTexture9 **texture = new IDirect3DTexture9 *[ 2 ];for (int i = 0; i < 2; i++){// Deleting the individual elements firstdelete[] texture;ORdelete texture;}delete [] texture;</pre>With example 2, even though it is a pointer to an array of com objects… I am &#111;nly freeing memory through the 'delete' keyword and not using release function at all.I am just wondering… do you need to also use the release method too after delete? Or is delete sufficient to cleanly unallocate memory there?The &#111;nly time I've use release is when I have &#111;nly a single com object<pre>EXAMPLE 3IDirect3DDevice9 D3DDevice = NULL;D3DDevice-&gt;release();</pre>Hmmmm come to think about it, I have this in my code.<pre>EXAMPLE 4Which way is the right way to free memory?IDirect3DDevice9 *D3DDevice = NULL;if( D3DDevice != NULL ){D3DDevice-&gt;Release();D3DDevice = NULL;}ORdelete D3DDevice;</pre>Thanks guys :)<!–EDIT–><span class=editedby><!–/EDIT–>[Edited by - tenpoundbear on April 19, 2010 9:18:08 AM]<!–EDIT–></span><!–/EDIT–>
Quote:Original post by tenpoundbearMy question is what if you have a pointer that pointers to a com object? Do you use delete or release? I have been using delete only in this situation.If you allocated the COM pointer with new, you need to free it with delete.Quote:Original post by tenpoundbearWhat if you have pointers to an array of com objects?If the array was allocated with new[], you need to loop through the array and Release() each one, and then delete[] the array.Quote:Original post by tenpoundbearEXAMPLE 1int *intArray = new int [5];for (int i = 0; i < 5; i++){// Deleting the individual elements firstdelete intArray ;ORdelete[] intArray ;// I have seen code snippets doing both ways... but which way is the correct way?}// Deleting the actual array nowdelete [] intArray;With Example 1, which way is the correct way?delete intArray  OR delete[] intArray ?If intArray was allocated with new, then you use delete. If it was allocated with new[], you use delete[].Quote:Original post by tenpoundbearEXAMPLE 2IDirect3DTexture9 **texture = new IDirect3DTexture9 *[ 2 ];for (int i = 0; i < 2; i++){// Deleting the individual elements firstdelete[] texture;<br>OR<br>delete texture;<br>}<br><br>delete [] texture;<br></pre><br><br>With example 2, even though it is a pointer to an array of com objects… I am &#111;nly freeing memory through the 'delete' keyword and not using release function at all.<br><br>I am just wondering… do you need to also use the release method too after delete? Or is delete sufficient to cleanly unallocate memory there?<!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–>You didn't allocate the IDirect3DTexture9* with new, it was allocated by D3D, so you call Release(), not delete or delete[]. You can leave the last line there, because the array itself was allocated with new[].<br><br><!–QUOTE–><BLOCKQUOTE><span class="smallfont">Quote:</span><table border=0 cellpadding=4 cellspacing=0 width="95%"><tr><td class=quote><!–/QUOTE–><!–STARTQUOTE–><i>Original post by tenpoundbear</i><br>The &#111;nly time I've use release is when I have &#111;nly a single com object<br><pre><br>EXAMPLE 3<br>IDirect3DDevice9 D3DDevice = NULL;<br>D3DDevice-&gt;release();<br></pre><br><br>Hmmmm come to think about it, I have this in my code.<br><pre><br>EXAMPLE 4<br>Which way is the right way to free memory?<br><br>IDirect3DDevice9 *D3DDevice = NULL;<br><br>if( D3DDevice != NULL )<br>{<br>D3DDevice-&gt;Release();<br>D3DDevice = NULL;<br>}<br>OR<br>delete D3DDevice;<br></pre><br><br>Thanks guys :)<!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–>In that case you Release() it, because you didn't allocate it with new. At no point does your code do: D3DDevice = new IDirect3DDevice9.<br><br>I'd highly recommend going back to basics and learning C++ before jumping into DirectX; all of the samples and documentation assumes that you're fluent in C++, and jumping into DirectX without knowing C++ or DirectX will just cause you more confusion.<br><br>Alternatively, at least look into using std::vector instead of manually managing your arrays, and use smart pointers for the COM objects. Then you'll almost never need to worry about using Release(), delete, or delete[].