--- a/tdlookup.c
+++ b/tdlookup.c
@@ -298,9 +298,17 @@ int respond(char *q,char qtype[2],char i
   if (!r) r = cdb_find(&c,key,4);
   if (!r) r = cdb_find(&c,key,3);
   if (!r) r = cdb_find(&c,key,2);
-  if (r == -1) return 0;
+  if (r == -1) {
+    cdb_free(&c);
+    close(fd);
+    return 0;
+  }
   if (r && (cdb_datalen(&c) == 2))
-    if (cdb_read(&c,clientloc,2,cdb_datapos(&c)) == -1) return 0;
+    if (cdb_read(&c,clientloc,2,cdb_datapos(&c)) == -1) {
+      cdb_free(&c);
+      close(fd);
+      return 0;
+    }
 
   r = doit(q,qtype);
 
