On 27/08/2020 20:15, Jason Biggs wrote:
Everything I know about C++ I learned just so that I can write a link between an interpreted language and the rdkit, so there are definitely some gaps in my knowledge.

What I'm trying to understand right now is the expected lifetime of an Atom pointer returned by a molecule, for instance by the getAtomWithIdx method.  Based on the documentation, since this method doesn't say the user is responsible for deleting the returned pointer I know I'm not supposed to delete it. But when exactly does it get deleted? If I dereference it after deleting the molecule, what is it?

auto mol = RDKit::SmilesToMol("CCCC");
auto atom = mol->getAtomWithIdx(0);
auto m2 = atom->getOwningMol();
std::cout << "Z=" << atom->getAtomicNum() << std::endl;  // prints Z=6
delete mol;
std::cout << "Z=" << atom->getIdx() << std::endl; // prints Z=0
std::cout << "N=" << m2.getNumAtoms() << std::endl;// prints N=4
delete atom; // seg fault

I would have thought the first time dereferencing the atom pointer after deleting mol would have crashed, but it does not.  I would also have expected bad things when calling the getNumAtoms method on m2 after calling delete on mol, but this also works just fine.  What am I missing?


Isn't this the soft of undefined behaviour that one would expect when accessing deleted memory? Try adding some code between the deletion of mol and the access of atom that allocates and deallocs some memory for a second or so.

Anyway, I wouldn't try to "out-clever" the RDKit by deleting molecules "by hand."


Paul.




_______________________________________________
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss

Reply via email to