[MLton-user] Does GC always trigger MLtonFinalizable?

Wesley W.Terpstra wesley at terpstra.ca
Fri Feb 16 15:12:43 PST 2007


On Feb 16, 2007, at 8:41 PM, Vesa Karvonen wrote:
> Quoting "Wesley W. Terpstra" <wesley at terpstra.ca>:
> [...]
>> The database handle has actual file descriptors and other system
>> resources. I wanted people to explicitly close this like in the  
>> basis.
>>
>> However, you may be right. If I use finalizers for queries, perhaps I
>> should also use a finalizer for the database. Although, then it is
>> possible to leak file handles until a GC.
> [...]
>
> Why not just keep a list of unfinalized queries with the DB handle and
> close them explicitly when the DB handle is closed?

I have been considering this myself. As you note later, you will  
still need finalizers to clean up queries during normal operation  
prior to the close. Don't forget that it is not possible (well, a bad  
idea) to finalize a query that is still being executed (and therefore  
the database can't be closed either).

There is also a design trade-off between the 'close everything' and  
'close only if unused' approach. In the first case, you invalidate  
handles to prepared queries that are not being executed, but that  
might be executed again in the future. Certainly usage of the  
prepared query after closing the associated database could raise an  
exception, but then all code using queries must worry about this.

The other approach, 'close only if unused' is more in keeping with  
SQLite's own interface philosophy. If there is a prepared query for  
the database, the user probably has it with the expectation he can  
execute another query. Therefore, if there are any prepared queries,  
fail on close.

My original question about collect+finalizers was to see if it is  
possible to preserve the 'close only if unused' interface of SQLite.  
If I can't, then I will do the 'force close everything' approach.  
Both approaches will require finalizers associated with query objects.

So, my original question again: does a GC collect promise to run all  
finalizers for all unreferenced objects?




More information about the MLton-user mailing list