Deallocate array?

Steven Schveighoffer schveiguy at yahoo.com
Fri May 10 06:47:46 PDT 2013


On Fri, 10 May 2013 04:44:14 -0400, Matic Kukovec  
<matic.kukovec at pametnidom.si> wrote:

> Thanks for the explanation guys.
> Hope there will be improvements in D's GC in the future.
>
> Also "delete temp_array" also works instead of "GC.free(temp_array.ptr)".
> And you also need to call "GC.minimize()" after a delete/GC.free() if  
> you want to shrink the memory use after the for loop:
>
>          temp_array = new string[100000000];
> 	for(int i=0;i<100000000;i++)
> 	{
> 		temp_array[i] = "aaaaaaaaaaaaaaaaaaaaa";
> 	}
> 	delete temp_array; or GC.free(temp_array.ptr);
> 	temp_array = null;
> 	GC.minimize();  //releases the memory, works without this line
>
> Is "delete" safe for this kind of use?

Delete is unsafe.  It is being deprecated actually (not sure when).   
Becuase it cannot zero out all pointers that reference that data, you can  
have dangling pointers to the data.

delete is replaced by the function destroy, which finalizes data, and  
GC.free to deallocate.  Only the GC.free part is unsafe, destroy is OK to  
use.

However, for arrays, destroy is equivalent to setting the value to null.

-Steve


More information about the Digitalmars-d-learn mailing list