python - osx 中的 rsvg python 内存泄漏(ctypes?)

标签 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__),
                ("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)

(来自 Error with Python ctypes and librsvg)

我调用 img = Handle(path) 并且这会泄漏内存。我很确定这是由于不正确地使用 ctypes 和指针造成的,但我找不到解决此问题的方法。

最佳答案

根据 rsvg_handle_new 的文档, 用 g_object_unref 释放 handle .此外,如果失败的调用分配了一个 GError,在您获得代码和消息后,您必须使用 g_error_free 释放错误。 .

from ctypes import *
from ctypes.util import find_library

_gobj = CDLL(find_library("gobject-2.0"))
_glib = CDLL(find_library("glib-2.0"))

class _GError(Structure):
    _fields_ = [("domain", c_uint32), 
                ("code", c_int), 
                ("message", c_char_p)]

_GErrorP = POINTER(_GError)

_glib.g_error_free.restype = None
_glib.g_error_free.argtypes = [_GErrorP]

_gobj.g_object_unref.restype = None
_gobj.g_object_unref.argtypes = [c_void_p]

您可以在 Handle 类的 __del__ 方法中释放句柄:

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

关于python - osx 中的 rsvg python 内存泄漏(ctypes?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23797789/

相关文章:

python - 来自带有 matplotlib 和 cx_Freeze 的 ctypes 的 NotADirectoryError

python - 在 python lxml 中设置和访问命名空间

python - Heroku/gunicorn/flask 应用程序说 "Connection in use"

python - Selenium 从字典中选择带有 for 循环的下拉选项

python - 从 SVG 路径中采样几何点的快速方法

javascript - 如何让hammer.js在不使用jquery的情况下处理svg文件?

html - 如何删除包含固定大小 svg 元素的 div 之间的水平空间?

python - 使用 ctypes 在 python 中创建背景转换器,不工作

python - 通过 Python 调用的 C 函数确定错误的数组大小

python - 使用 pyserial flush 方法