We found out that this call :

ldap_msgfree(res);

does not get executed because the LDAPMessage is NULL (res is null) in case the timeout is on the client side.

There is any other way to clean it?