Notes on memory management
ForeignPtrs are used wherever possible to make sure we don't have
However, there is one potential problem with them. If a whole bunch
of database stuff goes out of scope all at once, then the Connection,
Statement, and everything else could become finalizable all at once.
And it is not guaranteed to be finalized in any particular order.
We don't want to have the Connection finalized before the Statements.
This could cause segfaults with some databases.
So we implement a simple reference counting system for the
Connection. Each use -- whether in a Connection or a Statement --
counts as one, and the whole thing is freed when the use count drops
to 0 (the last object using it has been finalized).
That solves almost every problem. We have one more: when somebody
calls disconnect on the parent, we really want to close all open
statements first. So a system of weak refs is used to handle that.
This makes sure that we free things in the proper order for explicit
disconnects as well.