简体   繁体   中英

libSBML segmentation fault in python outside __init__

I'm trying to export a model using libSBML in python (2.x) into some custom representation, and I have came across a bug that I don't understand. I can access a list of species extracted from the model (self.sbSpecies) just fine in the init method, but trying to do the same in any other method results in a segmentation fault. Here is the minimal example (requires a 'yeast_7.11.xml' file from this archive: yest7.11 ):

#! /usr/bin/env python

from libsbml import *

class Exporter(object):
    def __init__(self, path_to_file):
        reader = SBMLReader()
        sbmldoc = reader.readSBMLFromFile(path_to_file)
        self.sbModel = sbmldoc.getModel()
        self.sbSpecies = [x for x in self.sbModel.getListOfSpecies()]
        print 'init:', self.sbSpecies[0]

    def debug(self):
        print 'debug:', self.sbSpecies[0]

exporter = Exporter('yeast_7.11.xml')
species = exporter.debug()

trying to run this gives me:

$ ./minimal_example.py 
init: <Species s_0001 "(1->3)-beta-D-glucan [cell envelope]">
Segmentation fault

from gdb (I don't know C or C++ so I can't really decipher what's going on)

(gdb) run ./minimal_example.py
Starting program: /usr/bin/python ./minimal_example.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
init: <Species s_0001 "(1->3)-beta-D-glucan [cell envelope]">

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff5ebffe0 in ?? () from /usr/lib/python2.7/dist-packages/libsbml/_libsbml.so
(gdb) backtrace
#0  0x00007ffff5ebffe0 in ?? () from /usr/lib/python2.7/dist-packages/libsbml/_libsbml.so
#1  0x0000000000502be0 in PyObject_CallFunction ()
#2  0x00000000004f8aa9 in _PyObject_GenericGetAttrWithDict ()
#3  0x00000000004ce145 in ?? ()
#4  0x00000000004cd95f in ?? ()
#5  0x000000000052c6d5 in PyEval_EvalFrameEx ()
#6  0x000000000056d0aa in ?? ()
#7  0x00000000004d9854 in ?? ()
#8  0x00000000004da29f in PyEval_CallObjectWithKeywords ()
#9  0x0000000000599269 in ?? ()
#10 0x00000000005165dc in PyObject_Str ()
#11 0x00000000005b52e3 in ?? ()
#12 0x000000000051694b in PyFile_WriteObject ()
#13 0x000000000052ffdd in PyEval_EvalFrameEx ()
#14 0x000000000052cf32 in PyEval_EvalFrameEx ()
#15 0x000000000055c594 in PyEval_EvalCodeEx ()
#16 0x00000000005b7392 in PyEval_EvalCode ()
#17 0x0000000000469663 in ?? ()
#18 0x00000000004699e3 in PyRun_FileExFlags ()
#19 0x0000000000469f1c in PyRun_SimpleFileExFlags ()
#20 0x000000000046ab81 in Py_Main ()
#21 0x00007ffff7817ec5 in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6
#22 0x000000000057497e in _start ()

Any ideas why accessing the same object (the self.sbSpecies list) from different methods can result in so different behaviour? How can I fix it (apart from throwing objects out)?

I managed to solve this problem by adding self. to sbmldoc.

It seems that libSBML objects from the self.sbSpecies still point to something in sbmldoc, but python for some reason doesn't keep track of that and destroys sbmldoc unless told not to do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM