Hi Noel,

thanks for your reply.

A complete code is pasted below.

The code is compiled with no problems and when executed, I get the
following (and expected) output:


$ McConf 169517407.mol2.gz
****************************************************************************************************
****************************************************************************************************
*                            McConf - Conformer Generation for McLiBELa
*

* University of Sao Paulo

* More Info:

*      http://www.biotechmol.ifsc.usp.br/

*

****************************************************************************************************
****************************************************************************************************
Original energy for molecule 169517407.mol2.gz:    43.9004
..tot conformations = 5184
..tot confs tested = 1000
..below energy threshold = 95
Energy/RMSD for conformer [  0]:    44.3411 kcal/mol /     0.0000 Ang
Energy/RMSD for conformer [  1]:    35.4359 kcal/mol /     1.7079 Ang
Energy/RMSD for conformer [  2]:    35.5114 kcal/mol /     1.7735 Ang
Energy/RMSD for conformer [  3]:    35.9091 kcal/mol /     1.5028 Ang
Energy/RMSD for conformer [  4]:    36.0429 kcal/mol /     1.5053 Ang
Energy/RMSD for conformer [  5]:    36.0517 kcal/mol /     1.8496 Ang
Energy/RMSD for conformer [  6]:    36.0932 kcal/mol /     1.3544 Ang
Energy/RMSD for conformer [  7]:    36.2786 kcal/mol /     1.1911 Ang
Energy/RMSD for conformer [  8]:    36.8942 kcal/mol /     1.9949 Ang
Energy/RMSD for conformer [  9]:    36.9710 kcal/mol /     1.5969 Ang
Number of conformers:   39

However, when I add a "delete mol" in the line just before the "return
(file_read);", the code remains compiled with no errors, but the output of
the execution gives me:

$McConf 169517407.mol2.gz
****************************************************************************************************
****************************************************************************************************
*                            McConf - Conformer Generation for McLiBELa

*

* University of Sao Paulo

* More Info:

*      http://www.biotechmol.ifsc.usp.br/

*

****************************************************************************************************
****************************************************************************************************
Original energy for molecule 169517407.mol2.gz:    43.9004
..tot conformations = 5184
..tot confs tested = 1000
..below energy threshold = 95
Energy/RMSD for conformer [  0]:    43.9004 kcal/mol /     0.0000 Ang
Energy/RMSD for conformer [  1]:    34.9952 kcal/mol /     1.7079 Ang
Energy/RMSD for conformer [  2]:    35.0707 kcal/mol /     1.7735 Ang
Energy/RMSD for conformer [  3]:    35.4684 kcal/mol /     1.5028 Ang
Energy/RMSD for conformer [  4]:    35.6022 kcal/mol /     1.5053 Ang
Energy/RMSD for conformer [  5]:    35.6110 kcal/mol /     1.8496 Ang
Energy/RMSD for conformer [  6]:    35.6525 kcal/mol /     1.3544 Ang
Energy/RMSD for conformer [  7]:    35.8379 kcal/mol /     1.1911 Ang
Energy/RMSD for conformer [  8]:    36.4535 kcal/mol /     1.9949 Ang
Energy/RMSD for conformer [  9]:    36.5303 kcal/mol /     1.5969 Ang
Number of conformers:   39
McConf(52198,0x7fffabacf3c0) malloc: *** error for object 0x7fd53071fd90:
pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

That's what I still can't understand.... I believe it should be possible to
deallocate the memory from the OBMol object before ending the code. I could
not figure out where am I messing up here.....

***

#include <iostream>

#include <cstdlib>

#include <vector>

#include "../LiBELa/Mol2.cpp"

#include "../LiBELa/Conformer.cpp"

#include "../LiBELa/PARSER.cpp"

#include "../LiBELa/WRITER.cpp"

#include "../LiBELa/COORD_MC.cpp"

#include <openbabel/mol.h>

#include <openbabel/obconversion.h>

#include <openbabel/forcefield.h>

#include <openbabel/math/vector3.h>



using namespace std;


int main(int argc, char* argv[]) {


    
printf("****************************************************************************************************\n");

    
printf("****************************************************************************************************\n");

    printf("*                            McConf - Conformer Generation
for McLiBELa                            *\n");

    printf("*
                                        *\n");

    printf("* University of Sao Paulo
                                        *\n");

    printf("* More Info:
                                        *\n");

    printf("*      http://www.biotechmol.ifsc.usp.br/
                                        *\n");

    printf("*
                                        *\n");

    
printf("****************************************************************************************************\n");

    
printf("****************************************************************************************************\n");



    if (argc < 2){

        printf("Usage: %s file.mol2 [input_file]\n", argv[0]);

        exit(1);

    }


    PARSER* Input = new PARSER;

    Mol2* Mol = new Mol2(Input, string(argv[1]));

    WRITER* Writer = new WRITER("McConf", Input);


    if (argc > 2){

        Input->set_parameters(argv[2]);

    }


    Input->write_mol2 = true;

    Input->dock_mode = true;



    bool file_read;

    OBMol* mol = new OBMol ;

    OBConversion* conv = new OBConversion;

    OBFormat *format = conv->FormatFromExt(argv[1]);

    if (!format || !conv->SetInFormat(format)) {

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

        exit(1);

    }


    ifstream ifs(argv[1]);

    if (!ifs) {

        printf("Could not open %s for reading\n", argv[1]);

        exit(1);

    }


    if (!conv->Read(mol, &ifs)) {

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

        exit(1);

    }


    OBMol* ref_mol = new OBMol;

    ifstream ifs2(argv[1]);

    conv->Read(ref_mol, &ifs2);


    delete conv;

    format->~OBFormat();


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


    if (!OBff){

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

        exit(1);

    }


    // Original conformation energy

    OBff->Setup(*mol);

    mol->SetTotalCharge(mol->GetTotalCharge());

    double energy = OBff->Energy();

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

        energy = energy/4.18;

    }


    printf("Original energy for molecule %s: %10.4f\n", argv[1], energy);


    // Conformer Search

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

    OBff->GetConformers(*mol);


    double rmsd;

    int conformers = Input->lig_conformers;

    if (mol->NumConformers() > 0){

        int i=0;

        if (mol->NumConformers() < conformers) {

            conformers = mol->NumConformers();

        }

        while (i < conformers){

            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;

            }


            OBAlign* align = new OBAlign;

            align->SetRefMol(*ref_mol);

            align->SetTargetMol(*mol);

            align->Align();

            rmsd = align->GetRMSD();

            align->UpdateCoords(mol);


            printf("Energy/RMSD for conformer [%3d]: %10.4f kcal/mol /
%10.4f Ang\n", i, energy, rmsd);


            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();

            }


            Writer->writeMol2(Mol,xyz_tmp, energy, rmsd, "McConf");

            xyz_tmp.clear();

            delete[] xyz;

            i++;

        }

        file_read = true;

    }

    else {

        file_read = false;

    }


    printf("Number of conformers: %4d\n", mol->NumConformers());


    delete ref_mol;


    delete Writer;

    delete Mol;

    delete Input;


    return (file_read);

}




Em dom., 1 de mar. de 2020 às 06:37, Noel O'Boyle <baoille...@gmail.com>
escreveu:

> It would help us if you could provide the shortest possible (but complete)
> example code that exhibits the problem. As it is, you say the problem is
> when you delete the molecule, but there is no line that deletes the
> molecule.
>
> On Thu, 27 Feb 2020 at 16:19, Alessandro S. Nascimento <
> asnascime...@ifsc.usp.br> wrote:
>
>> 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
>>
>

-- 
********************************************************
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