Allocation expressions are used to allocate and deallocate objects. The rules are the same as in C++: if you use new to allocate memory, then you must free the memory using delete. Otherwise you will leak memory. Finally, as noted below, if you use the auto qualifier, then RAII rules apply and the memory is freed as soon as the object goes out of scope.
Details are as follows:
Several examples are provided below.
The following example illustrates <Node> allocation:
// Allocate a node and add it to the model: Camera camera = new Camera; camera.Name = "Camera"; Model.AddNode( camera, Model, -1 ); // No need to free memory because the Model takes ownership. // Create a list using the 'new' keyword. List myList = new List; myList.AddRef( camera ); // Do something cool with the camera and then delete the list. The camera is not destroyed. delete myList; // Create a list using the 'auto' keyword. auto List myAutoList; myAutoList.AddRef( camera ); // Do something cool with the list. No need to delete it since we used 'auto'. // Allocate a Str object auto Str a_oStr = new Str( "Your text goes here!" ); // Do something with the Str. // Allocate another Str object using a_oStr a_oStr = new Str( "Hello World" ); // Don't do this! You just created a memory leak because auto is not reference counted. a_oStr.Value "Hello World"; // Do this instead.
The following example illustrates <Vec> allocation.
// Allocate two Vec objects, perform some processing on them, then delete them: Vec min = new Vec; Vec max = new Vec; min.X = 10; max.X = 30; // Do something useful with the Vec objects... // Delete the objects when finished. delete min; delete max;