Hi,

I've come across a subtle bug (or misunderstanding on my part..?) when
merging molecules from python... I've attached a minimal example that
segfaults (tested on Red Hat Enterprise Linux, python 2.7, openbabel
2.3.1). Essentially, it seems that if I do something like

mol1 += mol2

inside a function, mol1 (or more likely the C++ object underneath) is
no longer valid and doing things like converting it to an output
string fails with a segfault. I'm guessing swig or python has decided
there aren't any more references to it and has triggered deletion of
the C++ object?
e.g something like:

def addNewFragment(obmol):
    frag = newFrag() # Create some OBMol instance
    obmol += frag

mymol = getMol() # Some function to return OBMol instance
addNewFragment(mymol)
# At this point the mymol object is broken, and calls to many of its
methods fail with a segfault


Storing another reference to it (e.g. by returning the molecule from
the function and assigning it to something ) fixes the problem, e.g:

def addNewFragmentAndReturn(obmol):
   frag = newFrag()
   obmol += frag
   return obmol

mymol  =getMol()
foo = addNewFragmentAndReturn(mymol)
# mymol is still valid... (creating the "foo" reference seems to have
kept it alive...)


I think this is a bug - Can anyone else reproduce this (demo script
attached) or is it me not understanding how to use the library :)

Fred

Attachment: bug_report.py
Description: Binary data

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
OpenBabel-discuss mailing list
OpenBabel-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbabel-discuss

Reply via email to