A typealias doesn't get you enough here. (It's really just a nickname for
another type). What you need is to create a new type that wraps a Ptr{Void},

type PGconn
  ptr::Ptr{Void}
end


On Tue, Jan 28, 2014 at 3:32 AM, Maurizio De Santis <
[email protected]> wrote:

> Hello,
>
> I'm studying Julia and I'm trying to write a libpq (PostgreSQL) 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 what type. Any idea?
>
> Thank you :-)
>

Reply via email to