On Thu, Aug 27, 2020 at 4:33 PM David Cosgrove <davidacosgrov...@gmail.com> wrote:
> Hi Jason, > The answer is that when you delete the molecule, the memory it uses is > flagged as available for re-use, but nothing else happens to it. If you > then de-reference pointers to it, such as the atoms that are buried in the > block of memory allocated to the molecule, you may get away with it and you > may not. It will depend on whether something else has written over the > memory or not. In your example, the memory was still in its original state, > so the de-referencing of the atom pointers succeeded. This is not > guaranteed, however, and this sort of bug is generally very nasty to find- > sometimes the code will run, sometimes it will crash. Worse still is if you > accidentally write to de-allocated memory that something else is now using- > you can then get failures 5 minutes later in a completely different part of > the program. > Thank you David, this really helps. Thank you also to Dan, Nils, and Dima. I knew that accessing that atom after deleting the molecule was bad juju, and was confused why it worked. I will steer clear of undefined behavior. In my application, I have a wrapper class that I expose to top level users, which holds a unique pointer to an ROMol. I know then that when my wrapper class member goes away so does the ROMol. What I don't have is a similar wrapper class for an Atom, precisely because of these ownership questions - any atom properties or modifications go through the ROMol wrapper class. I'm not very familiar with how the python interface works, is there a similar issue with the python wrappers? Does the wrapper class for the Atom clean up after itself differently if the atom is marked as having an owner? Hope that question isn't too vague Jason > > Deleting the atoms is also an error, because they will be deleted by the > molecule’s destructor, so you’ll be de-allocating the memory twice, another > exciting source of undefined behaviour. Valgrind is excellent for tracking > down these sorts of error, and many more besides. If you’re developing on > Linux, it’s good practice to use it on any code before you use that program > in earnest. > > Cheers, > Dave > > > On Thu, 27 Aug 2020 at 20:17, Jason Biggs <jasondbi...@gmail.com> 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? >> >> Thanks >> Jason >> >> >> _______________________________________________ >> >> Rdkit-discuss mailing list >> >> Rdkit-discuss@lists.sourceforge.net >> >> https://lists.sourceforge.net/lists/listinfo/rdkit-discuss >> >> -- > David Cosgrove > Freelance computational chemistry and chemoinformatics developer > http://cozchemix.co.uk > >
_______________________________________________ Rdkit-discuss mailing list Rdkit-discuss@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rdkit-discuss