Ptr{Void} instance finalizer

230 views
Skip to first unread message

Maurizio De Santis

unread,
Jan 27, 2014, 12:13:44 PM1/27/14
to juli...@googlegroups.com
Hi!

congratulations for the Julia language, it's a great idea :-)

I'm learning Julia and I'm trying to write a libpq (PostgreSQL) wrapper. Executing this (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


I get an 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 have to change the PGconn type alias from Ptr{Void} to something, but I am undecided about what to use.

Jake Bolewski

unread,
Jan 27, 2014, 1:04:53 PM1/27/14
to juli...@googlegroups.com
Ptr{Void} is an immutable type that cannot be finalized.

what you want to do is wrap the underlying pointer handle in a (mutable) Connection type.


type
PGconn
    handle
::Ptr{Void}
end


function pgconn_finalizer(c::PGConn)
   
if c.handle != C_NULL
        ccall
((:PQfinish, "libpq"), Void, (Ptr{Void},), x.handle)
        x
.handle = C_NULL
   
end

end

c
= ccall( (:PQconnectdb, "libpq"), PGconn, (Ptr{Uint8},), "connection params" )
finalizer
(c, pgconn_finalizer)
println
(c)
gc
()

Jake Bolewski

unread,
Jan 27, 2014, 1:08:43 PM1/27/14
to juli...@googlegroups.com
Oops! a little too hasty.

type PGconn
    handle
::Ptr{Void}
end


function pgconn_finalizer(c::PGConn)
   
if c.handle != C_NULL
        ccall
((:PQfinish, "libpq"), Void, (Ptr{Void},), x.handle)
        x
.handle = C_NULL
   
end
end



ptr
= ccall( (:PQconnectdb, "libpq"), Ptr{Void}, (Ptr{Uint8},), "connection params" )  
c
= PGconn(ptr)


finalizer
(c, pgconn_finalizer)
println
(c)
gc
()

Best,
Jake 

Eric Davies

unread,
Jan 27, 2014, 4:38:40 PM1/27/14
to juli...@googlegroups.com
Hey Maurizio,

You may be interested in my package PostgreSQL.jl. It's a libpq wrapper under active development in conjunction with John Myles White's forthcoming DBI.jl standardized database interface. Contributions are most welcome, though I understand if you want to take your own package from start to finish in order to learn Julia. It's limited but functional at the moment, and you can expect major updates in the coming week.

- Eric

Maurizio De Santis

unread,
Jan 28, 2014, 11:10:45 AM1/28/14
to juli...@googlegroups.com
Jake Bolewski: thank you, it solved the issue; I was near the solution but couldn't figure out the handle part.

Eric Davies: I wasn't aware of it; I'll be happy to contribute to it when/if I'll become fluent in Julia.

Sorry for the double post on julia-users (which is the right place where this discussion should have been happened, I guess), I have some issues with the google groups search and with the email notifications!
Reply all
Reply to author
Forward
0 new messages