Chapter 2 Quick Start 21
Introductory Example
function calls after the do-while construct. That way, even if there is an error
somewhere and the program breaks out of the
do-while before executing all the calls
within the
do-while, the Destroy functions will execute. In case the error occurs
before an object has been created, it is a good idea to initialize objects to
NULL_PTR. If
an object is
NULL_PTR, the Destroy function does nothing.
Chapter 4 of the Reference Manual gives the description and prototype of the Destroy
functions:
For our example, we use the following:
Note: Following these calls, rc4Key and rc4Encrypter will be set to NULL if the
objects were disposed of properly.
In addition to destroying any objects that you created, any memory you allocated
must be freed when you are done with it. This means that each
T_malloc must have a
corresponding
T_free. Placing the T_free after the do-while guarantees that it will be
called even if there is an error somewhere. However, there is a concern that if there is
an error before the
T_malloc and the program breaks out of the do-while before
memory is allocated, then
T_free will be called without a corresponding T_malloc.
That is why it is important to initialize the pointer to
NULL_PTR. If the argument to
T_free is NULL_PTR, the extra call to T_free does nothing.
See Chapter 4 of the Reference Manual for the
T_free prototype:
void B_DestroyKeyObject (
B_KEY_OBJ *keyObject /* pointer to key object */
);
void B_DestroyAlgorithmObject (
B_ALGORITHM_OBJ *algorithmObject /* pointer to algorithm object */
);
B_DestroyKeyObject (&rc4Key);
B_DestroyAlgorithmObject (&rc4Encrypter);
void T_free (
POINTER block /* block address */
);