lib/0mq/socket.rb in 0mq-0.5.1 vs lib/0mq/socket.rb in 0mq-0.5.2

- old
+ new

@@ -21,12 +21,14 @@ @pointer = LibZMQ.zmq_socket @context.pointer, @type ZMQ.error_check true if @pointer.null? @msgptr = FFI::MemoryPointer.new LibZMQ::Message.size, 1, false + @context.send :register_socket_pointer, @pointer + ObjectSpace.define_finalizer self, - self.class.finalizer(@socket, Process.pid) + self.class.finalizer(@pointer, @context, Process.pid) end # Show a useful inspect output def inspect "#<#{self.class}:#{type_sym}:#{object_id.to_s(16)}>" @@ -35,27 +37,35 @@ # Close the socket def close @closed = true if @pointer - ObjectSpace.undefine_finalizer self @temp_buffers.clear if @temp_buffers + ObjectSpace.undefine_finalizer self + @context.send :unregister_socket_pointer, @pointer + rc = LibZMQ.zmq_close @pointer ZMQ.error_check true if rc==-1 @pointer = nil + @context = nil end end # Returns true if the socket is closed. def closed? @closed end - # Create a safe finalizer for the socket pointer to close on GC of the object - def self.finalizer(pointer, pid) - Proc.new { LibZMQ.zmq_close pointer if Process.pid == pid } + # Create a safe finalizer for the socket pointer to close on GC + def self.finalizer(pointer, context, pid) + Proc.new do + if Process.pid == pid + context.send :unregister_socket_pointer, pointer + LibZMQ.zmq_close pointer + end + end end # Get the socket type name as a symbol def type_sym ZMQ::SocketTypeNameMap[type].to_sym