我使用SWIG在c ++中包装一些类。我的python脚本看起来像
#!/usr/bin/python
import pyinput
import pygeom
import pystate
import pyeqfactory
import pyeigenprobfactory
import pyeigensolver
inp = pyinput.Input("heter1d/1d1g_heter")
geo = pygeom.ProbGeom1D(inp)
state = pystate.State1D(inp, geo);
equation = pyeqfactory.EqFactory1D.New(state)
eigenprob = pyeigenprobfactory.EigenProbFactory1D.New(state, equation)
solver = pyeigensolver.EigenSolver1D(state, eigenprob)
solver.solve_eigenproblem()
print state.get_keff()
我从一个智能指针类得到一个错误,因为一个实例被删除,但仍由其他
拥有--------------------------------------------------------
An error occurred in line <114> of file </home/segonpin/lib/dealii/source/base/subscriptor.cc> in function
virtual dealii::Subscriptor::~Subscriptor()
The violated condition was:
counter == 0
The name and call sequence of the exception was:
ExcInUse (counter, object_info->name(), infostring)
Additional Information:
Object of class N6dealii13TriangulationILi1ELi1EEE is still used by 1 other objects.
(Additional information: <none>)
See the entry in the Frequently Asked Questions of deal.II (linked to from http://www.dealii.org/) for a lot more information on what this error means and how to fix programs in which it happens.
Stacktrace:
-----------
#0 /home/segonpin/lib/dealii-8.4.bin/lib/libdeal_II.g.so.8.4.2: dealii::Subscriptor::~Subscriptor()
#1 /home/segonpin/lib/dealii-8.4.bin/lib/libdeal_II.g.so.8.4.2: dealii::Triangulation<1, 1>::~Triangulation()
#2 /home/segonpin/codessebas/dealii-neutron-transport/python/_pygeom.so: Forest::ProbGeom<1>::~ProbGeom()
#3 /home/segonpin/codessebas/dealii-neutron-transport/python/_pygeom.so:
#4 python: PyObject_Call
#5 python: PyObject_CallFunctionObjArgs
#6 /home/segonpin/codessebas/dealii-neutron-transport/python/_pygeom.so:
#7 python: ) [0x4fd4e6]
#8 python: PyDict_SetItem
#9 python: _PyModule_Clear
#10 python: PyImport_Cleanup
#11 python: Py_Finalize
#12 python: Py_Main
#13 /lib/x86_64-linux-gnu/libc.so.6: __libc_start_main
#14 python: _start
--------------------------------------------------------
当我手动删除以正确顺序生成问题的两个类时,错误消失
#!/usr/bin/python
import pyinput
import pygeom
import pystate
import pyeqfactory
import pyeigenprobfactory
import pyeigensolver
inp = pyinput.Input("heter1d/1d1g_heter")
geo = pygeom.ProbGeom1D(inp)
state = pystate.State1D(inp, geo);
equation = pyeqfactory.EqFactory1D.New(state)
eigenprob = pyeigenprobfactory.EigenProbFactory1D.New(state, equation)
solver = pyeigensolver.EigenSolver1D(state, eigenprob)
solver.solve_eigenproblem()
print state.get_keff()
# these two lines solve the problem
del state
del geo
不应该python以相同的顺序删除这些实例(因为geo
的构造函数中使用了state
?谢谢!
编辑2017年2月15日,11:19。
我可以添加一些额外的理由来解释为什么我需要按特定顺序删除。我正在使用swig来解析一个c ++库(www.dealii.org),当我创建一个需要A实例的B实例时,A中的一个计数器会增加,说它归B所有。这用于删除对象(B需要A存在,所以我们在删除A之前删除B)。如果你以其他方式做,你会收到错误。在这种情况下,python实例包含一些这些依赖项,因此我必须删除特定其他对象中的对象以避免错误消息。
我在python析构函数中添加了一些消息
#!/usr/bin/python
import pyinput
import pygeom
import pystate
import pyeqfactory
import pyeigenprobfactory
import pyeigensolver
def deltalk(self):
print type(self).__name__ ," die"
pyinput.Input.__del__ = deltalk
pygeom.ProbGeom1D.__del__ = deltalk
pystate.State1D.__del__ = deltalk
pyeqfactory.EqFactory1D.__del__ = deltalk
pyeigenprobfactory.EigenProbFactory1D.__del__ = deltalk
pyeigensolver.EigenSolver1D.__del__ = deltalk
pypostprocessing.PostProcessing1D.__del__ = deltalk
inp = pyinput.Input("heter1d/1d1g_heter")
geo = pygeom.ProbGeom1D(inp)
state = pystate.State1D(inp, geo);
equation = pyeqfactory.EqFactory1D.New(state)
eigenprob = pyeigenprobfactory.EigenProbFactory1D.New(state, equation)
solver = pyeigensolver.EigenSolver1D(state, eigenprob)
当我运行它时,我发现它并没有以我需要的方式进行破坏(在geo
在c ++中引发错误之前销毁state
$ python test2_forest.py
EigenSolver1D die
ProbGeom1D die
--------------------------------------------------------
An error occurred in line <114> of file </home/segonpin/lib/dealii/source/base/subscriptor.cc> in function
virtual dealii::Subscriptor::~Subscriptor()
The violated condition was:
counter == 0
The name and call sequence of the exception was:
ExcInUse (counter, object_info->name(), infostring)
Additional Information:
Object of class N6dealii13TriangulationILi1ELi1EEE is still used by 1 other objects.
(Additional information: <none>)
See the entry in the Frequently Asked Questions of deal.II (linked to from http://www.dealii.org/) for a lot more information on what this error means and how to fix programs in which it happens.
Stacktrace:
-----------
#0 /home/segonpin/lib/dealii-8.4.bin/lib/libdeal_II.g.so.8.4.2: dealii::Subscriptor::~Subscriptor()
#1 /home/segonpin/lib/dealii-8.4.bin/lib/libdeal_II.g.so.8.4.2: dealii::Triangulation<1, 1>::~Triangulation()
#2 /home/segonpin/codessebas/dealii-neutron-transport/python/_pygeom.so: Forest::ProbGeom<1>::~ProbGeom()
#3 /home/segonpin/codessebas/dealii-neutron-transport/python/_pygeom.so:
#4 python: PyObject_Call
#5 python: PyObject_CallFunctionObjArgs
#6 /home/segonpin/codessebas/dealii-neutron-transport/python/_pygeom.so:
#7 python: ) [0x4fd4e6]
#8 python: PyDict_SetItem
#9 python: _PyModule_Clear
#10 python: PyImport_Cleanup
#11 python: Py_Finalize
#12 python: Py_Main
#13 /lib/x86_64-linux-gnu/libc.so.6: __libc_start_main
#14 python: _start
考虑到这种依赖性,是否有一种告诉python删除对象的一般方法?或者我应该在每个类的析构函数中明确说出它?
由于
答案 0 :(得分:0)
如果将来有人发现同样的问题,我找到了一种方法,可以使用atexit
按我想要的顺序调用删除。
它不是很干净,但我会是这样的:
#!/usr/bin/python
import pyinput
import pygeom
import pystate
import pyeqfactory
import pyeigenprobfactory
import pyeigensolver
def goodbye() :
print "Goodbye!!\n"
import atexit
atexit.register(goodbye)
def deltalk(self):
print self.__class__.__name__ ," die"
pyinput.Input.__del__ = deltalk
pygeom.ProbGeom1D.__del__ = deltalk
pystate.State1D.__del__ = deltalk
pyeqfactory.Equation1D.__del__ = deltalk
pyeigenprobfactory.EigenProb1D.__del__ = deltalk
pyeigensolver.EigenSolver1D.__del__ = deltalk
pypostprocessing.PostProcessing1D.__del__ = deltalk
inp = pyinput.Input("heter1d/1d1g_heter")
atexit.register(inp.__del__)
geo = pygeom.ProbGeom1D(inp)
atexit.register(geo.__del__)
state = pystate.State1D(inp, geo);
atexit.register(state.__del__)
equation = pyeqfactory.EqFactory1D.New(state)
atexit.register(equation.__del__)
eigenprob = pyeigenprobfactory.EigenProbFactory1D.New(state, equation)
atexit.register(eigenprob.__del__)
solver = pyeigensolver.EigenSolver1D(state, eigenprob)
atexit.register(solver.__del__)
当我运行脚本时,我得到了
$ python test2_forest.py
EigenSolver1D die
EigenProb1D die
Equation1D die
State1D die
State1D die
ProbGeom1D die
Input die
Goodbye!!
Input die
EigenProb1D die
State1D die
ProbGeom1D die
EigenSolver1D die
Equation1D die
如果我可以在构造函数中添加atexit.register
会更容易,如下例所示
#!/usr/bin/python
import atexit
class Foo(object):
def __init__(self, id):
self.id = id
atexit.register(self.__del__)
def __del__(self):
print(self.id, " says bye")
a = Foo(1)
b = Foo(2)
c = Foo(3)
但是这些类是用c ++定义的并且用SWIG解析,所以我需要修改现有类定义的构造函数来做这样的事情,所以对于一个简短的程序来说更容易手动完成。
如果您必须为许多课程执行此操作,这不是最佳选择,但如果您仍想避免手动注册内容。类foo如下
#simplefoo.py
class Foo(object):
def __init__(self, id):
self.id = id
现有类的构造函数可以修改如下
#test.py
from simplefoo import Foo
# first: I load the module atexit and register the bye function
import atexit
def bye():
print("Bye bye!")
atexit.register(bye)
# second: I add the destructor that talks loudly
def del_ini(self):
print(self.id, " says bye")
Foo.__del__ = del_ini
# third: I modify the original constructor. This is ugly
Foo.ini_old = Foo.__init__
def ini_new(self, id):
self.ini_old(id)
atexit.register(self.__del__)
Foo.__init__ = ini_new
# fourth: I create objects and finish the program them to see what happens
a = Foo(1)
b = Foo(2)
c = Foo(3)
当我们运行脚本时,我们得到了
$ python3 test.py
3 says bye
2 says bye
1 says bye
Bye bye!
3 says bye
1 says bye
2 says bye
感谢。