|
Rule 1 : Never allocate memory in static-initialization.
|
|
|
This fundamental rule must not be violated.
ACIS provides the ability for the customer to modify the run-time behavior of the
ACIS memory management system. That is, the customer can decide, at run-time, to enable freelisting or enable leak-tracking. Once initialize_base is called with a particular memory management configuration, that memory management configuration is set for the duration of the life of the application - even through the end of static-termination.
|
|
|
For example, suppose an
ACIS_NEW takes place in static-initialization and the memory is allocated from the
ACIS freelist. Then suppose the customer application calls initialize_base and disables freelisting. When the matching
ACIS_DELETE is called, the pointer will be freed by the system, not by the
ACIS freelisting mechanism. The system will most likely crash because it is trying to delete a pointer of which it has no knowledge.
|
|
|
Allocating memory in static initialization will most likely result in a crash.
|
|
|
Rule 2 : Use STD_CAST appropriately.
|
|
|
If
ACIS_NEW is used to allocate a simple data-type, enum, or struct, or an array of simple data-types, enums, or structs then
STD_CAST must be used while calling
ACIS_DELETE.
STD_CAST must not be used while using
ACIS_DELETE for deleting a C++ object or array of objects.
|
|
|
Examples:
|
|
|
SPAposition *p = ACIS_NEW SPAposition;
|
|
ACIS_DELETE p;
|
|
|
SPAposition *p = ACIS_NEW SPAposition[24];
|
|
ACIS_DELETE [] p;
|
|
|
SPAposition **p = ACIS_NEW SPAposition *;
|
|
ACIS_DELETE STD_CAST p;
|
|
|
SPAposition **p = ACIS_NEW SPAposition *[24];
|
|
ACIS_DELETE [] STD_CAST p;
|
|
|
Failing to use STD_CAST properly will most likely result in a false leak, but on some systems (e.g., Mac) they will result in a crash.
|
|
|
Rule 3 : Use the array operator [] appropriately when deleting.
|
|
|
This is a rule of
C++ programming. While allocating an array of elements using
new/ACIS_NEW, the array operator must be used while calling
delete/ACIS_DELETE.
|
|
|
Examples:
|
|
|
int *n = new int[6];
|
|
delete [] n;
|
|
|
my_object *mo = new my_object[10];
|
|
delete [] mo;
|
|
|
Failing to use the array operator when deleting may result in memory leaks and/or resource leaks, and in some cases, crashes.
|