Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maybe a bug #258

Closed
dekonunes opened this issue Jun 3, 2016 · 6 comments
Closed

Maybe a bug #258

dekonunes opened this issue Jun 3, 2016 · 6 comments

Comments

@dekonunes
Copy link

My english is not so well, but i'll try show what happening.

I'm creating a genetic algorithm and when create a string with 16 or more bits the lib have a bug, will explain better.

First i'm reading like this:

void IndividuoBinario::openJson() {        
        using json = nlohmann::json;
        ifstream texto("entrada.json");
        if (!texto)
            cout << "dont open" << endl;
        stringstream buffer;
        buffer << texto.rdbuf();
        this->entrada = json::parse(buffer.str());
        texto.close();
}

I have a class IndividuoBinario with this constructor:

IndividuoBinario::IndividuoBinario() {
    static mt19937 mt(time(NULL));
    static uniform_int_distribution<int> bit(0, 1);
    string aux;
    openJson();
    pair<int, int> auxGenes;
    for (int count = 0; count < this->entrada["qtdVariaveis"]; ++count) {
        auxGenes.first = this->entrada["variaveis"][count]["min"];
        auxGenes.second = this->entrada["variaveis"][count]["max"];
        this->genes.push_back(auxGenes);
    }
    for (int count = 0; count < this->genes.size(); ++count) {
        this->quantidadeBitsCadaGene.push_back(getNumeroBits(this->entrada["variaveis"][count]["max"],
                this->entrada["variaveis"][count]["min"],
                this->entrada["variaveis"][count]["precisao"]));
    }

    for (int var = 0; var < this->genes.size(); ++var) {
        for (int i = 0; i < this->quantidadeBitsCadaGene.at(var); i++) {
            aux = static_cast<ostringstream*>(&(ostringstream() << bit(mt)))->str();
            this->cromossomo = this->cromossomo + aux;
        }
    }
    this->probMutacao = this->entrada["chanceMutacao"];
}

Working fine, BUT when i make a pair this bug happen:

pair<IndividuoBinario, IndividuoBinario> newIndividuosCrossover; 

using printf to find the error (error is Segmentation fault) i found in this line (of method openJSON):

cout << "here 1" << endl; //show
ifstream texto("entrada.json");
cout << "here 2" << endl; // dont show (Segmentation fault)

when run:

.....
here1
here2
here1
here2
line before pair
here1
Segmentation Fault (core dumped)

but more weird its because this happen just when my string (this->cromossomo) is more then 16 chars and just when use pairs, if invoke normal class IndividuoBinario work fine, just using pair this happen and with more then 16 chars.

I used gdb and this happen

4134    malloc.c: Arquivo ou diretório não encontrado.

I dont know if this is because the lib, but maybe. I'm trying hard to solve this.

@gregmarr
Copy link
Contributor

gregmarr commented Jun 3, 2016

aux = static_cast<ostringstream*>(&(ostringstream() << bit(mt)))->str();
this->cromossomo = this->cromossomo + aux;

This definitely looks suspect. As a start, I would rewrite this to use an actual named variable rather than doing funny things with taking addresses of temporaries and then using static_cast.

ostringstream str;
str << bit(mt);
this->cromossomo += str->str();

@dekonunes
Copy link
Author

its not,
put every '1' or '0' and continue the error.

aux = '1';
this->cromossomo = this->cromossomo + aux;

same problem.

@gregmarr
Copy link
Contributor

gregmarr commented Jun 3, 2016

The segmentation fault in the ifstream constructor along with gdb saying it's malloc_consolidate points to some kind of memory corruption.

I don't see anything else obviously wrong with the portion of the program that you've posted.
You don't include getNumeroBits, so that's a possibility.

Is cromossomo a std::string?
What is the type of probMutacao?
I assume that quantidadeBitsCadaGene is std::vector<int> or something similar, and that genes is std::vector<std::pair<int,int>> or something similar.
What is the value of qtdVariaveis in your .json file?
How many instances of IndividuoBinario have you created?
I count at least 3 based on your output.
Could you simply be running out of memory?

if the array variaveis in the json doesn't have at least qtdVariaveis elements, or it's not an array, or any of those array elements aren't objects or don't have min, max, and precisao elements, then you could be using uninitialized memory, or you could be trying access an object of the wrong type. The wrong type should be handled by an assert and exception, but the uninitialized memory may be biting you. entrada["variaveis"][count]["min"] will create objects to fill in [count] and ["min"] for you if they don't exist. This could result in bad behavior.

Are you able to post the contents of entrada.json?

@dekonunes
Copy link
Author

of course:

  • Is cromossomo a std::string? yes
  • What is the type of probMutacao? int
  • I assume that quantidadeBitsCadaGene is std::vector or something similar, and that genes is std::vectorstd::pair<int,int> or something similar. - correct
  • What is the value of qtdVariaveis in your .json file? int (min 1..10 max)
  • How many instances of IndividuoBinario have you created? created with 30 before call pairs (error), tryed with 10 and same error
  • I count at least 3 based on your output. - just cut the output (.... its because had more)
  • Could you simply be running out of memory? how can i do this?

try explain entrada.json.
lines 2 to 17 you can disregard, with this parameters create a this->cromossomo with 16 bits (error), with put "qtdVariaveis":5 this->cromossomo will be 15 bits and don't crash. But if put "qtdVariaveis":1 and the first element of array be like "max": 32000, this->cromossomo will have the same 16 bits and will crash. dont worry of "precisao": 0 its correct.

reforcing the error just happen when i call the function pair, if call a IndividuoBinario normal this work fine with 16.17...30 bits.

{
  "codificacao": "binario",
  "tamPop": 10,
  "execucoes": 3,
  "geracoes": 200,
  "selecao": 1,
  "crossover": "padrao",
  "tournament": 3,
  "chanceCrossover": 100,
  "chanceMutacao": 5,
  "elitismo": true,
  "escalonado": false,
  "gap": 1,
  "sharring": false,
  "sigma": 1,
  "alpha": 1,
  "crowding": 3,
 "qtdVariaveis": 6,
  "variaveis": [
    {
      "min": 0,
      "max": 4,
      "precisao": 0
    },
    {
      "min": 0,
      "max": 4,
      "precisao": 0
    },
    {
      "min": 0,
      "max": 4,
      "precisao": 0
    },
    {
      "min": 0,
      "max": 4,
      "precisao": 0
    },
    {
      "min": 0,
      "max": 4,
      "precisao": 0
    },
    {
      "min": 0,
      "max": 4,
      "precisao": 0
    },
    {
      "min": 0,
      "max": 4,
      "precisao": 0
    },
    {
      "min": 0,
      "max": 4,
      "precisao": 0
    },
    {
      "min": 0,
      "max": 4,
      "precisao": 0
    },
    {
      "min": 0,
      "max": 4,
      "precisao": 0
    }
  ]
}

@gregmarr
Copy link
Contributor

gregmarr commented Jun 3, 2016

Next I would see if the json lib is actually involved in the error, by first replacing all the direct calls in the constructor with their respective values, and then in a separate test run removing the parse call in openJson, since it looks like everything should work fine with that input file.

@dekonunes
Copy link
Author

I have not thought about it. Did this and continue, so sorry. Will try continue find this bug. thanks a lot for help, and sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants