In RDKit, atoms are owned by the molecule. When you ask for:

auto atom = mol->getAtomWithIdx(0);


You are asking for a pointer to memory internally owned by the
RDKit::ROMol. However:

auto mol = RDKit::SmilesToMol("CCCC");


creates a new molecule in memory, so it's your job to delete it. This is
documented here:
https://github.com/rdkit/rdkit/blob/e86e2c1d5d375c75cbd7e00871ecc1e0a29b3548/Code/GraphMol/SmilesParse/SmilesParse.h#L47.
I think that, in general, RDKit tends to document when you *do* need to
clean up after yourself.

I'd recommend one of these idioms:

ROMOL_SPTR mol1(RDKit::SmilesToMol("CCCC")); // this is a
boost::shared_ptr<RDKit::ROMol>, requires #include <GraphMol/ROMol.h>
std::unique_ptr<RDKit::ROMol> mol2(RDKit::SmilesToMol("CCCC")); // requires
#include <memory>



*dan nealschneider* | lead developer
[image: Schrodinger Logo] <https://www.schrodinger.com/>


On Thu, Aug 27, 2020 at 1:33 PM dmaziuk via Rdkit-discuss <
rdkit-discuss@lists.sourceforge.net> wrote:

> On 8/27/2020 3:06 PM, Nils Weskamp wrote:
> > To add to this: you are looking at the wonderful concept of an
> > "undefined behavior" in C/C++. There is no guarantee that your example
> > program will always show the same behaviour.
> >
> > In more recent versions of C++, you have access to "smart pointers" like
> > std::shared_ptr, which basically implement reference counting. Not sure
> > if this would help here.
>
> It's worse: with all the boost junk they pulled in the really recent
> versions, good luck figuring out which calls pass "smart" pointers and
> which don't.
>
> There are reasons why everyone's into Rust, and the efforts of C++
> Standards Committee are behind many of them.
>
> Dima
>
>
> _______________________________________________
> Rdkit-discuss mailing list
> Rdkit-discuss@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
>
_______________________________________________
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss

Reply via email to