The features described in this topic are obsolete
and should not be used in new IDL code.

Notes on Variable Creation and Memory Management

This section contains miscellaneous notes about variable creation.

Freeing Resources

The variable creation functions (i.e., v_make_xxx) do not free resources associated with a variable before placing new information there. Your programs should free resources (if there are any) associated with the varinfo_t structure being passed.

To prevent memory leakage, memory associated with a variable is freed before new memory is allocated. You should make sure that the varinfo_t structure passed to the get_idl_variable function contains valid information or has been cleared (to zeroes) first. If an array of the same size, dimensions, and type is being read into the existing array variable, no allocation is performed and the same space is re-used. For example:

/* Assume that:
   X = FLTARR(1000, 1000)
   Y = FLTARR(1000, 1000)
   Z = LONARR(1000, 1000)	same size, different type
*/ 
bzero(&vinfo, sizeof(vinfo));
get_idl_variable(client, "X", &vinfo, 0); /* array allocated */
...
get_idl_variable(client, "Y", &vinfo, 0); /* memory re-used */
...
get_idl_variable(client, "Z", &vinfo, 0); /* array allocated */
free_idl_var(&vinfo); 

The get_idl_variable function calls free_idl_var before doing any allocation. So, in the example above, we only needed to free Z. X and Y were freed when we re-used vinfo.

Creating a Statically-Allocated Array

It is possible to create a statically-allocated array for receiving information from the server without having the overhead of memory reallocation every time information is received.

If the Length field of the varinfo_t structure is not zero, it is assumed to be the size of the array data. The free_idl_var function will not do anything to a variable where length is non-zero. It is up to the programmer to do their own memory management if this is the case. Storing a scalar in a static variable (i.e., a variable that has a non-zero Length field) fails as does attempting to store an array that does not fit the statically-allocated array. For example:

/* X = FLTARR(10)     40 bytes of data (10*4)
   Y = LONARR(2,2,2)  32 bytes of data(2*2*2*4)
   Z = BYTARR(50)     50 bytes of data
   W = 12             scalar
*/ 
char       buf[40]
varinfo_t  v;
VARIABLE   var;
ARRAY      arr; 
/* Build a static array. Fill in the minimum amount of */
/* information required.                              */ 
v.Variable    = &var;
v.Length      = 40;
var.type      = IDL_TYP_BYTE;
var.flags     = V_ARR;
var.value.arr = &arr;
arr.data      = buf; 
get_idl_variable(client, "X", &v, 0);  /* ok */
get_idl_variable(client, "Y", &v, 0);  /* ok */
get_idl_variable(client, "Z", &v, 0);  /* fails — too big */
get_idl_variable(client, "W", &v, 0);  /* fails — scalar */ 

Allocating Space for Strings

All space for strings is assumed to be obtained via malloc(3). This fact is important only when receiving variables (using the get_idl_variable function). For example, the following code fragment is valid:

v_make_string(&foo, "UGH", "blug");
set_idl_variable(client, &foo); 

Here is an example of code that will crash your program:

v_make_string(&foo, "UGH", "blug");
set_idl_variable(me, &foo);
send_idl_command(me, "UGH='hello world'");
get_idl_variable(me, "UGH", &foo, 0); 

In this case, the get_idl_variable function attempts to free the old resources before allocating new storage. Freeing the constant blug results in an error. You could achieve the desired result without an error by changing the first line to:

v_make_string(&foo, "UGH", strdup("blug"));