lib/libvirt/domain.rb in libvirt_ffi-0.3.0 vs lib/libvirt/domain.rb in libvirt_ffi-0.4.0

- old
+ new

@@ -1,14 +1,23 @@ # frozen_string_literal: true module Libvirt class Domain + + def self.load_ref(dom_ptr) + ref_result = FFI::Domain.virDomainRef(dom_ptr) + raise Error, "Couldn't retrieve domain reference" if ref_result < 0 + new(dom_ptr) + end + def initialize(dom_ptr) @dom_ptr = dom_ptr free = ->(obj_id) do - STDOUT.puts("finalized Libvirt::Domain obj_id=0x#{obj_id.to_s(16)}, @dom_ptr=#{@dom_ptr},") + return unless @dom_ptr + fr_result = FFI::Domain.virDomainFree(@dom_ptr) + STDERR.puts "Couldn't free Libvirt::Domain (0x#{obj_id.to_s(16)}) pointer #{@dom_ptr.address}" if fr_result < 0 end ObjectSpace.define_finalizer(self, free) end def get_state @@ -61,8 +70,14 @@ def screenshot(stream, display = 0) mime_type, pointer = FFI::Domain.virDomainScreenshot(@dom_ptr, stream.to_ptr, display, 0) raise Error, "Couldn't attach domain screenshot" if pointer.null? # free pointer required mime_type + end + + def free_domain + result = FFI::Domain.virDomainFree(@dom_ptr) + raise Error, "Couldn't free domain" if result < 0 + @dom_ptr = nil end end end