Commit 0ae44493 authored by Julien Gamba's avatar Julien Gamba

push_back() produce a segfault when receiving data

parent 79f17d17
...@@ -54,7 +54,7 @@ void * CComUDPServer::UDP_server_thread(void *parm) { ...@@ -54,7 +54,7 @@ void * CComUDPServer::UDP_server_thread(void *parm) {
printf(" Received individual from %s:%d\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port)); printf(" Received individual from %s:%d\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
pthread_mutex_lock(&server_mutex); pthread_mutex_lock(&server_mutex);
/*process received data */ /*process received data */
RECV_DATA buffer_copy = RECV_DATA(); RECV_DATA buffer_copy;
memmove(buffer_copy.data,buffer,sizeof(char)*MAXINDSIZE); memmove(buffer_copy.data,buffer,sizeof(char)*MAXINDSIZE);
// printf("address %p\n",(p->data)); // printf("address %p\n",(p->data));
p->data.push_back(buffer_copy); p->data.push_back(buffer_copy);
...@@ -72,7 +72,6 @@ CComUDPServer::CComUDPServer(unsigned short port,int dg) { ...@@ -72,7 +72,6 @@ CComUDPServer::CComUDPServer(unsigned short port,int dg) {
struct sockaddr_in ServAddr; /* Local address */ struct sockaddr_in ServAddr; /* Local address */
debug = dg; debug = dg;
this->nb_data = 0; this->nb_data = 0;
this->data.assign(sizeof(RECV_DATA),RECV_DATA());
#ifdef WIN32 #ifdef WIN32
WSADATA wsadata; WSADATA wsadata;
if (WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) { if (WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) {
......
...@@ -535,13 +535,13 @@ void CEvolutionaryAlgorithm::sendIndividual(){ ...@@ -535,13 +535,13 @@ void CEvolutionaryAlgorithm::sendIndividual(){
void CEvolutionaryAlgorithm::receiveIndividuals(){ void CEvolutionaryAlgorithm::receiveIndividuals(){
//Checking every generation for received individuals //Checking every generation for received individuals
if(this->treatedIndividuals<(unsigned)this->server->nb_data){ if(this->server->parm->data.size() != 0){
//cout << "number of received individuals :" << this->server->nb_data << endl; //cout << "number of received individuals :" << this->server->nb_data << endl;
//cout << "number of treated individuals :" << this->treatedIndividuals << endl; //cout << "number of treated individuals :" << this->treatedIndividuals << endl;
CSelectionOperator *antiTournament = getSelectionOperator("Tournament",!this->params->minimizing, globalRandomGenerator); CSelectionOperator *antiTournament = getSelectionOperator("Tournament",!this->params->minimizing, globalRandomGenerator);
//Treating all the individuals before continuing //Treating all the individuals before continuing
while(this->treatedIndividuals < (unsigned)this->server->nb_data){ while(this->server->parm->data.size() != 0){
//selecting the individual to erase //selecting the individual to erase
antiTournament->initialize(this->population->parents, 7, this->population->actualParentPopulationSize); antiTournament->initialize(this->population->parents, 7, this->population->actualParentPopulationSize);
unsigned index = antiTournament->selectNext(this->population->actualParentPopulationSize); unsigned index = antiTournament->selectNext(this->population->actualParentPopulationSize);
...@@ -552,16 +552,14 @@ void CEvolutionaryAlgorithm::receiveIndividuals(){ ...@@ -552,16 +552,14 @@ void CEvolutionaryAlgorithm::receiveIndividuals(){
//cout << "old individual fitness :" << this->population->parents[index]->fitness << endl; //cout << "old individual fitness :" << this->population->parents[index]->fitness << endl;
//cout << "old Individual :" << this->population->parents[index]->serialize() << endl; //cout << "old Individual :" << this->population->parents[index]->serialize() << endl;
this->server->read_data_lock(); this->server->read_data_lock();
string line = this->server->parm->data[this->treatedIndividuals].data; string line = this->server->parm->data.back().data;
this->server->parm->data.pop_back();
this->population->parents[index]->deserialize(line); this->population->parents[index]->deserialize(line);
//TAG THE INDIVIDUAL AS IMMIGRANT //TAG THE INDIVIDUAL AS IMMIGRANT
this->population->parents[index]->isImmigrant = true; this->population->parents[index]->isImmigrant = true;
delete [] this->population->parents;
this->population->parents = NULL;
this->server->read_data_unlock(); this->server->read_data_unlock();
//cout << "new Individual :" << this->population->parents[index]->serialize() << endl; //cout << "new Individual :" << this->population->parents[index]->serialize() << endl;
this->treatedIndividuals++;
} }
} }
} }
......
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
#define MAXINDSIZE 50000 /*maximum size of an individual in number of characters*/ #define MAXINDSIZE 50000 /*maximum size of an individual in number of characters*/
typedef struct { typedef struct recv_data {
char data[MAXINDSIZE]; char data[MAXINDSIZE];
}RECV_DATA; }RECV_DATA;
...@@ -41,7 +41,7 @@ typedef struct { ...@@ -41,7 +41,7 @@ typedef struct {
int Socket; /* Socket descriptor for server */ int Socket; /* Socket descriptor for server */
struct sockaddr_in ServAddr; /* Local address */ struct sockaddr_in ServAddr; /* Local address */
int debug; int debug;
std::vector<RECV_DATA> data; std::vector<recv_data> data;
int *nb_data; int *nb_data;
}UDP_server_thread_parm_t; }UDP_server_thread_parm_t;
...@@ -49,7 +49,7 @@ class CComUDPServer { ...@@ -49,7 +49,7 @@ class CComUDPServer {
public: public:
int debug; int debug;
std::vector<RECV_DATA> data; std::vector<recv_data> data;
int nb_data; int nb_data;
UDP_server_thread_parm_t *parm; UDP_server_thread_parm_t *parm;
CComUDPServer(unsigned short port, int dg); CComUDPServer(unsigned short port, int dg);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment