Skip to content

Commit

Permalink
store weakrefs to sockets in context (fixes #124) (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj authored Apr 8, 2018
1 parent d05c020 commit bd61076
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/ZMQ.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ mutable struct Socket
socket = new(p)
socket.pollfd = _FDWatcher(fd(socket), #=readable=#true, #=writable=#false)
@compat finalizer(close, socket)
push!(ctx.sockets, socket)
push!(ctx.sockets, WeakRef(socket))
return socket
end
end
Expand All @@ -99,16 +99,16 @@ end
mutable struct Context
data::Ptr{Cvoid}

# need to keep a list of sockets for this Context in order to
# need to keep a list of weakrefs to sockets for this Context in order to
# close them before finalizing (otherwise zmq_term will hang)
sockets::Vector{Socket}
sockets::Vector{WeakRef}

function Context()
p = ccall((:zmq_ctx_new, libzmq), Ptr{Cvoid}, ())
if p == C_NULL
throw(StateError(jl_zmq_error_str()))
end
zctx = new(p, Socket[])
zctx = new(p, WeakRef[])
@compat finalizer(close, zctx)
return zctx
end
Expand All @@ -120,8 +120,9 @@ function close(ctx::Context)
if ctx.data != C_NULL # don't close twice!
data = ctx.data
ctx.data = C_NULL
for s in ctx.sockets
close(s)
for w in ctx.sockets
s = w.value
s isa Socket && close(s)
end
rc = ccall((:zmq_ctx_destroy, libzmq), Cint, (Ptr{Cvoid},), data)
if rc != 0
Expand Down

0 comments on commit bd61076

Please sign in to comment.