Question

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?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top