Shouldn't it (ldap_msgfree) be done at the end of the function in case of search['return'] == 0 ?? Just asking, I'm not familiar with this api