class CZMQ::ZFrame

Public Class Methods

new(obj=nil) click to toggle source
# File lib/czmq/zframe.rb, line 5
def initialize(obj=nil)
  if obj.is_a? FFI::Pointer
    @zframe = obj
  else
    @zframe = LibCZMQ.zframe_new(obj)
  end

  setup_finalizer
end

Private Class Methods

close_zframe(zframe) click to toggle source

Need to make this a class method, or the deallocation won't take place. See: www.mikeperham.com/2010/02/24/the-trouble-with-ruby-finalizers/

# File lib/czmq/zframe.rb, line 64
def self.close_zframe(zframe)
  Proc.new do
    LibCZMQ.zframe_destroy(zframe)
    zframe = nil # Just in case
  end
end

Public Instance Methods

__extract__() click to toggle source
# File lib/czmq/zframe.rb, line 32
def __extract__
  raise 'Trying to extract internal @zframe pointer from an unitialized ZFrame!' if @zframe.nil?

  # Since we explicitly hand over the @zframe pointer, we have to remove the finalizer.
  remove_finalizer

  # Return content of @zframe pointer and reset it to nil
  zframe = @zframe
  @zframe = nil
  zframe
end
destroy() click to toggle source
# File lib/czmq/zframe.rb, line 23
def destroy
  if @zframe
    # Since we explicitly close the zframe, we have to remove the finalizer.
    remove_finalizer
    LibCZMQ.zframe_destroy(@zframe)
    raise 'Error!' unless @zframe.nil?
  end
end
initialize_copy(orig) click to toggle source

Need to provide a copy constructor that implements deep copy

Calls superclass method
# File lib/czmq/zframe.rb, line 16
def initialize_copy(orig)
  super
  unless @zframe.nil?
    @zframe = LibCZMQ.zframe_dup(@zframe)
  end
end

Private Instance Methods

remove_finalizer() click to toggle source
# File lib/czmq/zframe.rb, line 58
def remove_finalizer
  ObjectSpace.undefine_finalizer self
end
setup_finalizer() click to toggle source

After object destruction, make sure that the corresponding zframe is destroyed as well.

NOTE: We don't care about ensuring, as ffi-rmzq does, that the resource deallocation request comes from the process that allocated it in the first place. In fact, the CZMQ documentation at zeromq.org/area:faq explicitly states “It is not safe to share a context or sockets between a parent and its child.”

# File lib/czmq/zframe.rb, line 54
def setup_finalizer
  ObjectSpace.define_finalizer(self, self.class.close_zframe(@zframe))
end