sales@scenomics.com +1 650 396 9674

Allocation Expressions

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.

  • new type-name
  • delete object-variable-name

Details

Details are as follows:

  • You can only allocate object data types, not primitive data types.
  • In the case of allocating memory for a new <Node>, you do not need to free memory if the <Node> is added to document. In fact, you cannot free the memory once the <Node> is added to the document unless you call Model.DeleteNode( node ).

Examples

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
a_oStr = new Str( "Hello World" ); // Don't do this!

// You just created a memory leak because auto is not reference counted.

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;