fix debug to avoid null pointer exception
fixed a "too many open files" bug (file descriptor leak)