next up previous contents index
Next: Strings and Opaque Data Up: Problems Previous: Problems

Memory Management

The XDR library functions don't know anything about GRAPE and its own memory management, they use malloc and free to allocate and free memory. You should be careful not to allocate memory for a GRAPE instance with malloc because it would be freed with mem_free when the instance is deleted. This would lead to an error message from the GRAPE memory manager, accidentally freeing memory allocated with mem_alloc with free however would crash the program. The safest way to avoid these problems is to not use XDR functions which allocate or free memory, instead of xdr_string and xdr_array you should use the corresponding GRAPE XDR functions g_xdr_string and g_xdr_array.

Another problem is reading strings or arrays into static or local arrays which are not dynamically allocated. If an XDR function tries to free such an array which is located in the program's data segment or on the stack the program will probably crash. Since static or local strings (char arrays) are very common, g_xdr_string never frees the array if the string to be read doesn't fit into it but just allocates new memory. If the string was allocated dynamically this data block will be lost. As described in section 5.7.2 g_xdr_array behaves differently: because very large memory blocks could be lost, the old data is freed if necessary before new memory is allocated, therefore the parameter array should have been allocated dynamically.



SFB 256 Universität Bonn and IAM Universität Freiburg

Copyright © by the Sonderforschungsbereich 256 at the Institut für Angewandte Mathematik, Universität Bonn.