-
Notifications
You must be signed in to change notification settings - Fork 30
/
Memory.txt
24 lines (19 loc) · 1.04 KB
/
Memory.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Notes on memory management
--------------------------
ForeignPtrs are used wherever possible to make sure we don't have
memory leaks.
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.