I ran the thing again to get a trace back, and went to attach the code and found a free() which was at the wrong location. I think I caused the problem.
I still don't know what's happening with ldap on the mail server, but my application bug was not related after all.