py_object不会可靠地释放引用的对象

时间:2015-09-06 19:30:31

标签: python ctypes

重新分配ctypes py_object时,将释放先前分配的对象(如预期的那样)。

但是当获取指向py_object的指针然后解析此指针时,生成的py_object不能按预期工作(= ref计数器不会更改):

from sys import getrefcount
from ctypes import *

pyobj = 'any python object'
ct_ref = py_object(pyobj)
ct_ref2 = pointer(ct_ref).contents

refCntBefore = getrefcount(pyobj)
ct_ref2.value = 'another python object'   # when replace ct_ref2 with ct_ref everything will work!
assert getrefcount(pyobj) < refCntBefore, 'object was not released'

在我的系统上,我使用的是Python 2.7.10 32位(Windows)。

我认为这是ctypes中的一个错误,老实说我不相信解决方案。但也许有人有解决方法吗?非常感谢...

0 个答案:

没有答案
相关问题