As a julia-dev mailing list post suggested, the solution is to declare a new type with a pointer handle, which will be freed by PQfinish
, as in the code shown below:
module LibPQ
type PGconn
handle::Ptr{Void}
end
function pgconn_finalizer(c::PGconn)
if c.handle != C_NULL
ccall( (:PQfinish, "libpq"), Void, (Ptr{Void},), c.handle )
c.handle = C_NULL
end
println("finalized")
end
c = ccall( (:PQconnectdb, "libpq"), PGconn, (Ptr{Uint8},), "connection params" )
finalizer(c, pgconn_finalizer)
println(c)
gc()
end