[zeromq-dev] Ruby GC

Chuck Remes cremes.devlist at mac.com
Tue Dec 20 22:25:35 CET 2011


On Dec 20, 2011, at 3:03 PM, Morten Møller Riis wrote:

> Hi Guys
> 
> I am having a problem that is driving me crazy.
> 
> It seems to me that the ruby garbage collector and zeromq might not be the best of friends.
> 
> I have an application that uses ZMQ where something like this happens:
> 
> class Client
> 	def self.listen
> 		@socket = ZMQ::Context.new.socket(ZMQ::UPSTREAM)
> 		@socket.bind("tcp://#{IP}:#{MINION_PORT}")
> 
> 		while(true)
> 			msg = @socket.recv()
> 			# do something with the message
> 		end
> 	end
> end


This isn't new for 2.1. I think you are just getting lucky on that other code.

The problem is this line:

> 		@socket = ZMQ::Context.new.socket(ZMQ::UPSTREAM)


You are not saving a reference to the context that you just created, so the GC will reap it at some point. Change it to:

> 		@context = ZMQ::Context.new
> 		@socket = @context.socket(ZMQ::UPSTREAM)

And now all will be well with the world.

cr




More information about the zeromq-dev mailing list