Dear OpenBabel users,

I am trying to write a piece of C++ code to generate conformers using the
OpenBabel API. My current code is mostly based on OB tutorials... not
really a big deal.

Almost everything seems to be working. The problem is with memory
allocation. When my function gets out of scope, it gives me a mem leak:
malloc: *** error for object 0x103300350: pointer being freed was not
allocated
*** set a breakpoint in malloc_error_break to debug.
which seems to be related to the object mol (OBMol mol).

I tried the dynamic allocation with new/delete and got exactly the same
thing when trying to delete the object mol in the second function below. No
problems when deleting the ref_mol object.

My initial guess was that I had to clear the conformers generated before
deleting the object. I then tried to use DeleteConformer function with no
success.

Any clues/ideas? They are all very welcome!

My OB version is 3.0.0 and my current OS is MacOS Sierra 10.12.6:
$g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr
--with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Thanks in advance,

-Alessandro

OBMol Conformer::GetMol(const std::string &molfile){

    OBMol mol;

    OBConversion conv;

    OBFormat *format = conv.FormatFromExt(molfile.c_str());

    if (!format || !conv.SetInFormat(format)) {

    printf("Could not find input format for file\n");

    return mol;

  }

    ifstream ifs(molfile.c_str());

    if (!ifs) {

        printf("Could not open %s for reading\n", molfile.c_str());

        return mol;

    }

    if (!conv.Read(&mol, &ifs)) {

        printf("Could not read molecule from file\n");

        return mol;

    }

    return mol;

}


******************************************************************

bool file_read;

    OBMol mol = this->GetMol(molfile);

    OBMol ref_mol = this->GetMol(molfile);


    OBForceField* OBff;

    if (Input->ligand_energy_model == "GAFF" or
Input->ligand_energy_model == "gaff"){

        OBff = OBForceField::FindForceField("GAFF");

    }

    else {

        OBff = OBForceField::FindForceField("MMFF94");

    }


    if (!OBff){

        printf("Could not find OpenBabel FF parameters!\n");

        exit(1);

    }


    OBff->Setup(mol);

    mol.SetTotalCharge(mol.GetTotalCharge()); // necessary?

    double energy = OBff->Energy();

    if (OBff->GetUnit() == "kJ/mol"){       // Converting to kcal/mol,
if needed.

        energy = energy/4.18;

    }

    Lig->conformer_energies.push_back(energy);

    Lig->mcoords.push_back(Lig->xyz);


// Conformer

    OBff->DiverseConfGen(0.5, 1000, 50.0, false);

    OBff->GetConformers(mol);


    if (mol.NumConformers() > 0){

        for (int i=0; i<mol.NumConformers(); i++){

            double* xyz = new double[mol.NumAtoms()*3];

            vector<double> v3;

            vector<vector<double> > xyz_tmp;

            mol.SetConformer(i);

            OBff->Setup(mol);

            OBff->GetCoordinates(mol);

            energy = OBff->Energy();

            if (OBff->GetUnit() == "kJ/mol"){       // Converting to
kcal/mol, if needed.

                energy = energy/4.18;

            }

            Lig->conformer_energies.push_back(energy);


            OBAlign* align = new OBAlign;

            align->SetRefMol(ref_mol);

            align->SetTargetMol(mol);

            align->Align();

            align->UpdateCoords(&mol);

            delete align;


            xyz = mol.GetCoordinates();

            for (unsigned j=0; j<mol.NumAtoms(); j++){

                v3.push_back(xyz[3*j]);

                v3.push_back(xyz[(3*j)+1]);

                v3.push_back(xyz[(3*j)+2]);

                xyz_tmp.push_back(v3);

                v3.clear();

            }

            Lig->mcoords.push_back(xyz_tmp);

            xyz_tmp.clear();


            delete[] xyz;

        }

        file_read = true;

    }

    else {

        file_read = false;

    }


    ref_mol.Clear();

    mol.Clear();


    return file_read;


-- 
********************************************************
Alessandro S. Nascimento
Professor
São Carlos Institute of Physics (IFSC)
University of São Paulo (USP)
E-mail: asnascime...@ifsc.usp.br
Phone: +55-16-3373-8709
********************************************************
_______________________________________________
OpenBabel-discuss mailing list
OpenBabel-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbabel-discuss

Reply via email to