Notes on Allocating and Deallocating Memory in In-process Code

Unless a function's description states otherwise, if you allocate a piece of memory, then you are responsible for de-allocating that memory, and if the server allocates a piece of memory and returns it to you, the server is responsible for de-allocating the memory.

For each piece of memory that is allocated by either you or the server, the de-allocation method must correspond to the allocation method that you (or the server) used. For example, if you allocate a piece of memory using C8Malloc(), then the memory should be freed using C8Free() rather than free() or some other de-allocation method. If you use the wrong de-allocation method for a particular piece of memory, the results are undefined.

Similarly, if you allocate a piece of memory that the server is responsible for de-allocating, the memory must be allocated using the appropriate function so that the server can de-allocate it correctly.

These rules apply to all code in IPAs and UDFs (including user-defined aggregate functions).

The following table shows when to use each type of de-allocation method:

Allocated By

Deallocated By

Language

C8Malloc() or C8Realloc()

C8Free()

C or C++

malloc() or realloc()

free()

C or C++

new (C++ memory allocation)

delete (C++ memory deallocation)

C++

C8MessageCreate() or C8MessageCreateWithSize()C8MessageCreateWithSize() or C8AdapterReceiveMessage() or C8AdapterReceiveMessageWait()

C8MessageDestroy()

C or C++

Warning!  

You must use the appropriate de-allocation function for each piece of memory. For example, use C8Free() , not free() , to free a piece of memory that you allocated with C8Malloc().

In general, if a function returns a pointer to object A that is part of another object B, then you should not specifically de-allocate object A; A will be de-allocated when B is de-allocated. In the Sybase CEP C/C++ SDK function prototypes, objects that are part of other objects are usually marked as "const" ; for example, in the function prototype below, the return value is "const C8Schema *", which indicates that the returned schema is part of a C8Publisher object, and therefore should not be de-allocated separately.

const C8Schema * C8PublisherGetSchema(...);