fix file descriptor leak

Hi,

I have to apologize for my rather heavy butchering of your code.

I think I've hunted down the file descriptor leakage, there's also a new test revealing it.  (dbi-close always failed to close the database file.)

I may also have fixed the occasional segfault; at least for me it didn't do that any longer after the change.

Please let me know what you think.

Bert