Client-side memory management depends on the specific LDAP functions. In general, you must release the memory associated with data returned to the client either as an out parameter or as a function return value. However, some functions, such as ldap_err2string, return a pointer to a static buffer, and in that case you should not free the memory associated with the returned data.
The LDAP API supports the following memory management functions. For more information about managing memory for a particular function, see the reference page for that function.
The ldap_memfree function frees memory that was allocated from the LDAP heap. Call this function to free strings, such as the DN returned by ldap_get_dn and the ErrorMessage and MatchedDNS parameters in ldap_parse_result.
The ldap_value_free function frees the character string returned by ldap_get_values. Call ldap_value_free_len to free the berval structures returned by ldap_get_values_len.
Call the ldap_control_free function to dispose of a previously allocated LDAPControl structure. Call ldap_controls_free to dispose of a previously allocated array of LDAPControl structures.
Call ldap_msgfree to dispose of an LDAPMessage structure, such as the one the server returns in a call to ldap_first_reference.