osx中的rsvg python内存泄漏(ctypes?)
我使用以下代码来阅读svg:osx中的rsvg python内存泄漏(ctypes?),python,svg,ctypes,cairo,pycairo,Python,Svg,Ctypes,Cairo,Pycairo,我使用以下代码来阅读svg: from ctypes import CDLL, POINTER, Structure, byref, util from ctypes import c_bool, c_byte, c_void_p, c_int, c_double, c_uint32, c_char_p class _PycairoContext(Structure): _fields_ = [("PyObject_HEAD", c_byte * object.__basicsize_
from ctypes import CDLL, POINTER, Structure, byref, util
from ctypes import c_bool, c_byte, c_void_p, c_int, c_double, c_uint32, c_char_p
class _PycairoContext(Structure):
_fields_ = [("PyObject_HEAD", c_byte * object.__basicsize__),
("ctx", c_void_p),
("base", c_void_p)]
class _RsvgProps(Structure):
_fields_ = [("width", c_int), ("height", c_int),
("em", c_double), ("ex", c_double)]
class _GError(Structure):
_fields_ = [("domain", c_uint32), ("code", c_int), ("message", c_char_p)]
def _load_rsvg(rsvg_lib_path=None, gobject_lib_path=None):
if rsvg_lib_path is None:
rsvg_lib_path = util.find_library('rsvg-2')
if gobject_lib_path is None:
gobject_lib_path = util.find_library('gobject-2.0')
l = CDLL(rsvg_lib_path)
g = CDLL(gobject_lib_path)
g.g_type_init()
l.rsvg_handle_new_from_file.argtypes = [c_char_p, POINTER(POINTER(_GError))]
l.rsvg_handle_new_from_file.restype = c_void_p
l.rsvg_handle_render_cairo.argtypes = [c_void_p, c_void_p]
l.rsvg_handle_render_cairo.restype = c_bool
l.rsvg_handle_get_dimensions.argtypes = [c_void_p, POINTER(_RsvgProps)]
return l
_librsvg = _load_rsvg()
class Handle(object):
def __init__(self, path):
lib = _librsvg
err = POINTER(_GError)()
self.handle = lib.rsvg_handle_new_from_file(path, byref(err))
if self.handle is None:
gerr = err.contents
raise Exception(gerr.message)
self.props = _RsvgProps()
lib.rsvg_handle_get_dimensions(self.handle, byref(self.props))
def render_cairo(self, ctx):
"""Returns True is drawing succeeded."""
z = _PycairoContext.from_address(id(ctx))
return _librsvg.rsvg_handle_render_cairo(self.handle, z.ctx)
(来自)
我调用img=Handle(path),这会泄漏内存。我很确定这是由于不正确地使用ctypes和指针造成的,但我找不到解决方法。根据的文档,用释放句柄。另外,如果一个失败的调用分配了一个
GError
,在您得到代码和消息后,您必须用它来释放错误
您可以在句柄
类的方法中释放句柄:
class Handle(object):
_gobj = _gobj # keep a valid ref for module teardown
# ...
def __del__(self):
if self.handle:
self._gobj.g_object_unref(self.handle)
self.handle = None
这听起来好像是在正确的轨道上。我尝试了g_object_unref代码,但出现了一个SEGFULT。我还试着把gobject传过去,从那里打电话,但也遇到了一个问题。
class Handle(object):
_gobj = _gobj # keep a valid ref for module teardown
# ...
def __del__(self):
if self.handle:
self._gobj.g_object_unref(self.handle)
self.handle = None