4.3.  Deallocating memory

4.3.1. IGRAPH_FINALLY — Register an object for deallocation.
4.3.2. IGRAPH_FINALLY_CLEAN — Signal clean deallocation of objects.
4.3.3. IGRAPH_FINALLY_FREE — Deallocate all registered objects.

If a function runs into an error (and the program is not aborted) the error handler should deallocate all temporary memory. This is done by storing the address and the destroy function of all temporary objects in a stack. The IGRAPH_FINALLY function declares an object as temporary by placing its address in the stack. If an igraph function returns with success it calls IGRAPH_FINALLY_CLEAN() with the number of objects to remove from the stack. If an error happens however, the error handler should call IGRAPH_FINALLY_FREE() to deallocate each object added to the stack. This means that the temporary objects allocated in the calling function (and etc.) will be freed as well.

4.3.1. IGRAPH_FINALLY — Register an object for deallocation.

#define IGRAPH_FINALLY(func,ptr)

Arguments: 

func:

The address of the function which is normally called to destroy the object.

ptr:

Pointer to the object itself.

This macro places the address of an object, together with the address of its destructor in a stack. This stack is used if an error happens to deallocate temporarily allocated objects to prevent memory leaks.

4.3.2. IGRAPH_FINALLY_CLEAN — Signal clean deallocation of objects.

void IGRAPH_FINALLY_CLEAN(int num);

Removes the specified number of objects from the stack of temporarily allocated objects. Most often this is called just before returning from a function.

Arguments: 

num:

The number of objects to remove from the bookkeeping stack.

4.3.3. IGRAPH_FINALLY_FREE — Deallocate all registered objects.

void IGRAPH_FINALLY_FREE(void);

Calls the destroy function for all objects in the stack of temporarily allocated objects. This is usually called only from an error handler. It is not appropriate to use it instead of destroying each unneeded object of a function, as it destroys the temporary objects of the caller function (and so on) as well.