I'm studying Julia and I'm trying to write a libpq (the PostgreSQL C library) wrapper.
The C PQconnectdb
function starts a PostgreSQL connection, and returns a pointer to the connection struct. The connection struct is not exposed by libpq, since it is not useful.
When the connection finishes, it must be closed using the C function PQfinish
which, among other things, takes care to free the struct memory.
So I guess I should call the PQfinish
function when the connection get finalized by the GC, which this is the purpose of the following code (Julia version: 0.3.0-744~ubuntu13.10.1):
module LibPQ
typealias PGconn Ptr{Void}
function pgconn_finalizer(x::PGconn)
ccall( (:PQfinish, "libpq"), Void, (PGconn,), x )
println("finalized")
end
c = ccall( (:PQconnectdb, "libpq"), PGconn, (Ptr{Uint8},), "connection params" )
finalizer(c, pgconn_finalizer)
println(c)
gc()
end
But I get this error:
$ julia libpq.jl
ERROR: objects of type Ptr{None} cannot be finalized
in finalizer at base.jl:103
in include at boot.jl:240
while loading ./libpq.jl, in expression starting on line 12
I guess I should use another type for the PGconn
typealias, but I can't figure out which one. Any idea?
extern struct PGConn; typedef struct PGConn PGConn;
. This gives you a named handle for the type instead of a void pointer, but you can't do anything with it except pass it around. – Craig Ringertype PGconn ...
– mdesantis