Segmentation Fault in C++ With for Loop [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am making an Ant simulation with SDL2 and when I run my code it soon crashed giving me a segmentation fault. I believe this is from trying to access negative values in an array. I tried to make it so the Ant can't do this by giving an if statement checking its value on the "grid" I made out of squares. I added an if statement on line 420 to try and stop my theory but it didn't do anything. Does anyone see the issue here?
My Grid Initialiation:
struct Wall
{
bool IsAlive;
int Health = 10;
SDL_Rect Rect;
bool isAnt;
bool IsHome;
bool IsFood;
};
This is where I set up the rects:
int count = 0;
for (int i = 0; i < 1440; i += 4)
{
for (int j = 0; j < 795; j += 4)
{
SDL_Rect Rect = {i, j, 4, 4};
wall[count].Rect = Rect;
wall[count].IsAlive = true;
if (i < 10)
{
if (j < 10)
{
wall[count].IsAlive = false;
}
}
count++;
}
}
And this is where I do the Ant Digging:
void Break(int AntID, int SurroundingSqr /*Clockwise*/)
{
if (SurroundingSqr == -199)
{
if (A[AntID].PosID > 100)
{
wall[A[AntID].PosID + SurroundingSqr].Health--;
if (wall[A[AntID].PosID + SurroundingSqr].Health == 0)
{
wall[A[AntID].PosID + SurroundingSqr].IsAlive = false;
}
}
}
else
{
wall[A[AntID].PosID + SurroundingSqr].Health--;
if (wall[A[AntID].PosID + SurroundingSqr].Health == 0)
{
wall[A[AntID].PosID + SurroundingSqr].IsAlive = false;
}
}
}
If you want to see all the code you can go to this Gist

You should include all the headers you need. You are missing #include <ctime>.
You have undefined behavior because of indices running out of bounds, as can be seen when running your program compiled with -g -fsanitize=address,undefined:
ant.cpp:350:34: runtime error: index -199 out of bounds for type 'Wall [572397]'
ant.cpp:356:32: runtime error: index -1 out of bounds for type 'Wall [572397]'
=================================================================
==3989611==ERROR: AddressSanitizer: global-buffer-overflow on address 0x00000042fae4 at pc 0x000000405540 bp 0x7ffe896fa2e0 sp 0x7ffe896fa2d8
READ of size 1 at 0x00000042fae4 thread T0
#0 0x40553f in MoveLeft(int) /home/ted/proj/stackoverflow/ant.cpp:356
#1 0x407f87 in MakeAntActions() /home/ted/proj/stackoverflow/ant.cpp:213
#2 0x402687 in main /home/ted/proj/stackoverflow/ant.cpp:102
#3 0x7ff7a0671b74 in __libc_start_main (/lib64/libc.so.6+0x27b74)
#4 0x40293d in _start (/home/ted/proj/stackoverflow/ant-g+++0x40293d)
I would start nesting out the problem by following the call chain from line
102: MakeAntActions(); ➔
213: MoveLeft(i); ➔
350 and 356 (where it croaks)
if (wall[A[AntID].PosID - 199].IsAlive) // line 350 index -199
if (wall[A[AntID].PosID - 1].IsAlive) // line 356 index -1
So, A[AntID].PosID is 0 at the time the program reaches these lines which creates these negative indices - and negative indices are not allowed.
Compile with -g -fsanitize=address,undefined to get hints like this.

Related

exception after setCompleteRedYellowGreenDef in sumo client c++

I was trying to build up on the example sumo client server in c++ as in the link
http://sumo.dlr.de/wiki/TraCI/C%2B%2BTraCIAPI
I wrote a function to build a complete RedYellowGreenDef as in the function below:
std::vector<libsumo::TraCILogic> Client::buildCompletetrafficLogics(std::string tlID, int phaseIndex, int increaseVal)
{
std::vector<libsumo::TraCILogic> logics = trafficlights.getCompleteRedYellowGreenDefinition(tlID);
for (int i = 0; i < (int)logics.size(); ++i)
{
for (int j = 0; j < (int)logics[i].phases.size(); ++j)
{
if (logics[i].currentPhaseIndex = phaseIndex)
{
logics[i].phases[j].duration = logics[i].phases[j].duration + increaseVal; //increase duration by 5 seconds
logics[i].phases[j].duration2 = logics[i].phases[j].duration2+ increaseVal;
}
}
}
return logics;
}
I called this function in the main function as follows:
//increase duration of phase with index "PhaseIndex" by 5000ms
std::vector<libsumo::TraCILogic> logics = client.buildCompletetrafficLogics(*tlIt, phaseIndex,5000);
libsumo::TraCILogic logic = logics[0];
client.trafficlights.setCompleteRedYellowGreenDefinition(tlID, logic);
where tlID is a valid traffic light id, but I faced The following exception when I passed the built logic to setCompleteRedYellowGreenDefinition():
terminate called after throwing an instance of 'std::invalid_argument'
what(): Storage::writeUnsignedByte(): Invalid value, not in [0, 255]
Aborted (core dumped)
Could you help me to find the problem: why I got this exception and how to tackle it?

Exception thrown with OpenCV's SVM training, c++

I have a problem with the Support Vector Machine , coding with visual studio, c++.
Tried to replicate the example of the tutorial at the following link
https://docs.opencv.org/3.4.0/d1/d73/tutorial_introduction_to_svm.html and the situation is
(having a breakpoint at "return 0;" line)
1) In debug , x64 I run the solution, and after "return 0" statement I have an exception thrown
at memory location (not unhandled, luckily) and then the program exit correctly.
2) In release, x64, I run the solution, and after "return 0" statement I have an exception thrown
at memory location but clicking on "continue" button, this appears (of mat.inl.hpp)
inline
void Mat::release()
{
if( u && CV_XADD(&u->refcount, -1) == 1 )
deallocate();
u = NULL;
datastart = dataend = datalimit = data = 0;
for(int i = 0; i < dims; i++)
----> size.p[i] = 0; <----- // (that's the exact line)!
#ifdef _DEBUG
flags = MAGIC_VAL;
dims = rows = cols = 0;
if(step.p != step.buf)
{
fastFree(step.p);
step.p = step.buf;
size.p = &rows;
}
#endif
}
And I have the correct exit of the program only clicking on "continue" bottom several times.
This happens only in release mode.
How to fix it?

Why do I get this C++ error (terminate called after throwing an instance of 'std::bad_alloc') when I am playing the code?

I wrote the code pasted below to perform a method that starts monotrack and generates tracks.
void KalmanTracker::TrackInitiation(AssignData *track_, vector<int> *CT_order_, Camera *cam)
{
vector<int> CT_order= *CT_order_;
Functions f;
double T,X_MAX,X_MIN,Y_MAX,Y_MIN;
int numtracks = 0;
for(int curr_p=0; curr_p<track_->num_DTBP; curr_p++) { //plot in the current frame
if(track_->Data_TBP.at(curr_p).Free == false) {
for(int ant_p=0; ant_p<track_->num_DANT; ant_p++) { //plot in the previous frame
T = track_->Data_TBP.at(curr_p).Dato.t_obt - track_->Data_ANT.at(ant_p).Dato.t_obt;
X_MAX = track_->Data_ANT.at(ant_p).Dato.Est_PosXY.at(Xx) + MAX_SPEED*T + 3.0*sqrt(2.0*RX);
X_MIN = track_->Data_ANT.at(ant_p).Dato.Est_PosXY.at(Xx) - MAX_SPEED*T - 3.0*sqrt(2.0*RX);
Y_MAX = track_->Data_ANT.at(ant_p).Dato.Est_PosXY.at(Yy) + MAX_SPEED*T + 3.0*sqrt(2.0*RY);
Y_MIN = track_->Data_ANT.at(ant_p).Dato.Est_PosXY.at(Yy) - MAX_SPEED*T - 3.0*sqrt(2.0*RY);
if((track_->Data_TBP.at(curr_p).Dato.Est_PosXY.at(Xx) < X_MAX) && (track_->Data_TBP.at(curr_p).Dato.Est_PosXY.at(Xx) > X_MIN) &&
(track_->Data_TBP.at(curr_p).Dato.Est_PosXY.at(Yy) < Y_MAX) && (track_->Data_TBP.at(curr_p).Dato.Est_PosXY.at(Yy) > Y_MIN)) {
if((track_->Data_TBP.at(curr_p).Dato.Area > 10) && (track_->Data_ANT.at(ant_p).Dato.Area > 10) &&
(track_->Data_TBP.at(curr_p).Dato.InBox == false) && (track_->Data_ANT.at(ant_p).Dato.InBox == false)) {
f.InitiateMonotrack(track_,track_->Data_ANT.at(ant_p).Dato,track_->Data_TBP.at(curr_p).Dato,&CT_order,numtracks); //Start the monotrack
numtracks++;
track_->Data_TBP.at(curr_p).Free = true;
}
}
}
}
}
// Reordering and saving CT_order
int num_non_discarded = 0;
for(int i=0; i<track_->num_CT+numtracks; i++) {
if (CT_order.at(i) != -1) {
CT_order.at(num_non_discarded) = CT_order.at(i);
num_non_discarded++;
}
}
track_->num_CT = min(num_non_discarded,MAX_CT-1);
// Copy CT_order to the CT_order of the track
for(int i=0; i<track_->num_CT; i++)
track_->CT_order.at(i) = CT_order.at(i);
// Releasing previous plots
int num_ANT = 0;
for(int i=0; i<track_->num_DANT; i++)
track_->Data_ANT.at(i).Free = true;
// Copy the current plot to previous plot
for(int curr_p=0; curr_p<track_->num_DTBP; curr_p++) {
if (track_->Data_TBP.at(curr_p).Free == false) {
track_->Data_ANT.at(num_ANT).Dato.t_obt = track_->Data_TBP.at(curr_p).Dato.t_obt;
track_->Data_ANT.at(num_ANT).Dato.Est_PosXY.resize(2);
track_->Data_ANT.at(num_ANT).Dato.Est_PosXY.at(Xx) = track_->Data_TBP.at(curr_p).Dato.Est_PosXY.at(Xx);
track_->Data_ANT.at(num_ANT).Dato.Est_PosXY.at(Yy) = track_->Data_TBP.at(curr_p).Dato.Est_PosXY.at(Yy);
track_->Data_ANT.at(num_ANT).Dato.Area = track_->Data_TBP.at(curr_p).Dato.Area;
track_->Data_ANT.at(num_ANT).Dato.InBox = track_->Data_TBP.at(curr_p).Dato.InBox;
track_->Data_TBP.at(curr_p).Dato.image.copyTo(track_->Data_ANT.at(num_ANT).Dato.image);
track_->Data_ANT.at(num_ANT).Free = false;
num_ANT++;
}
}
track_->num_DANT = min(num_ANT,MAX_DTBP-1);
// Releasing current plots
for(int curr_p=0; curr_p<track_->num_DTBP; curr_p++)
track_->Data_TBP.at(curr_p).Free = true;
track_->num_DTBP = 0;
*CT_order_= CT_order;
}
When I execute the .cpp file I keep getting the error message:
terminate called after throwing an instance of 'std::bad_alloc'
terminate called recursively
what(): std::bad_alloc
I have gathered this has to do with a memory shortage or variables falling out of the main() function, but I can not figure out how to address the problem in this specific situation. If it is relevant, I am working on a Linux computer, programming in eclipse, using C++ language and opencv and boost libraries, because my project has two threads.
[EDIT] The debugger tells me:
Can't find a source file at "/build/glibc-t3gR2i/glibc-2.23/signal/../sysdeps/unix/sysv/‌​linux/raise.c" Locate the file or edit the source lookup path to include its location.
And the source not found is __GI_raise() at raise.c.
And after that error, the console tells me:
[xcb] Unknown sequence number while processing reply
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that. dronesNuevo: ../../src/xcb_io.c:635: _XReply: Assertion `!xcb_xlib_threads_sequence_lost' failed.
But other times the debugger tells me:
Thread 3 received signal SIGSEGV, Segmentation fault.
[Cambiando a Thread 0x7fffe1f69700 (LWP 17536)] 1574 ../sysdeps/x86_64/multiarch/memcpy-ssse3-back.S: No existe el archivo o el directorio.
__memmove_ssse3_back () at ../sysdeps/x86_64/multiarch/memcpy-ssse3-back.S:1574
and:
Can't find a source file at "/build/glibc-t3gR2i/glibc-2.23/string/../sysdeps/x86_64/mul‌​tiarch/memcpy-ssse3-‌​back.S"
Locate the file or edit the source lookup path to include its location.
Being the source not found __memmove_ssse3_back() at memcpy-sse3-back.S
The backtrace that I get:
*** Error in `path': double free or corruption (!prev): 0x00007fffa007ecb0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7ffff5e577e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x7fe0a)[0x7ffff5e5fe0a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7ffff5e6398c]
path[0x40d3b6]
path[0x40cc26]
path[0x40bd78]
path[0x40b3a2]
path[0x409f3f]
path[0x40813b]
path[0x412bd7]
path[0x43df93]
path[0x44826b]
path[0x447f38]
path[0x447ac2]
/usr/lib/x86_64-linux-gnu/libboost_thread.so.1.58.0(+0x115d5)[0x7ffff6a5b5d5]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x76ba)[0x7ffff59b06ba]
/lib/x86_64-linux-gnu/libc.so.6(clone+0x6d)[0x7ffff5ee682d]

strange segmentation fault during function return

I am running a program on 2 different machines. On one it works fine without issue. On the other it results in a segmentation fault. Through debugging, I have figured out where the fault occurs, but I can't figure out a logical reason for it to happen.
In one function I have the following code:
pass_particles(particle_grid, particle_properties, input_data, coll_eros_track, collision_number_part, world, grid_rank_lookup, grid_locations);
cout<<"done passing particles"<<endl;
The function pass_particles looks like:
void pass_particles(map<int,map<int,Particle> > & particle_grid, std::vector<Particle_props> & particle_properties, User_input& input_data, data_tracking & coll_eros_track, vector<int> & collision_number_part, mpi::communicator & world, std::map<int,int> & grid_rank_lookup, map<int,std::vector<double> > & grid_locations)
{
//cout<<"east-west"<<endl;
//east-west exchange (x direction)
map<int, vector<Particle> > particles_to_be_sent_east;
map<int, vector<Particle> > particles_to_be_sent_west;
vector<Particle> particles_received_east;
vector<Particle> particles_received_west;
int counter_x_sent=0;
int counter_x_received=0;
for(grid_iter=particle_grid.begin();grid_iter!=particle_grid.end();grid_iter++)
{
map<int,Particle>::iterator part_iter;
for (part_iter=grid_iter->second.begin();part_iter!=grid_iter->second.end();)
{
if (particle_properties[part_iter->second.global_part_num()].particle_in_box()[grid_iter->first])
{
//decide if a particle has left the box...need to consider whether particle was already outside the box
if ((part_iter->second.position().x()<(grid_locations[grid_iter->first][0]) && part_iter->second.position().x()>(grid_locations[grid_iter->first-input_data.z_numboxes()][0]))
|| (input_data.periodic_walls_x() && (grid_iter->first-floor(grid_iter->first/(input_data.xz_numboxes()))*input_data.xz_numboxes()<input_data.z_numboxes()) && (part_iter->second.position().x()>(grid_locations[input_data.total_boxes()-1][0]))))
{
particles_to_be_sent_west[grid_iter->first].push_back(part_iter->second);
particle_properties[particle_grid[grid_iter->first][part_iter->first].global_part_num()].particle_in_box()[grid_iter->first]=false;
counter_sent++;
counter_x_sent++;
}
else if ((part_iter->second.position().x()>(grid_locations[grid_iter->first][1]) && part_iter->second.position().x()<(grid_locations[grid_iter->first+input_data.z_numboxes()][1]))
|| (input_data.periodic_walls_x() && (grid_iter->first-floor(grid_iter->first/(input_data.xz_numboxes()))*input_data.xz_numboxes())>input_data.xz_numboxes()-input_data.z_numboxes()-1) && (part_iter->second.position().x()<(grid_locations[0][1])))
{
particles_to_be_sent_east[grid_iter->first].push_back(part_iter->second);
particle_properties[particle_grid[grid_iter->first][part_iter->first].global_part_num()].particle_in_box()[grid_iter->first]=false;
counter_sent++;
counter_x_sent++;
}
//select particles in overlap areas to send to neighboring cells
else if ((part_iter->second.position().x()>(grid_locations[grid_iter->first][0]) && part_iter->second.position().x()<(grid_locations[grid_iter->first][0]+input_data.diam_large())))
{
particles_to_be_sent_west[grid_iter->first].push_back(part_iter->second);
counter_sent++;
counter_x_sent++;
}
else if ((part_iter->second.position().x()<(grid_locations[grid_iter->first][1]) && part_iter->second.position().x()>(grid_locations[grid_iter->first][1]-input_data.diam_large())))
{
particles_to_be_sent_east[grid_iter->first].push_back(part_iter->second);
counter_sent++;
counter_x_sent++;
}
++part_iter;
}
else if (particles_received_current[grid_iter->first].find(part_iter->first)!=particles_received_current[grid_iter->first].end())
{
if ((part_iter->second.position().x()>(grid_locations[grid_iter->first][0]) && part_iter->second.position().x()<(grid_locations[grid_iter->first][0]+input_data.diam_large())))
{
particles_to_be_sent_west[grid_iter->first].push_back(part_iter->second);
counter_sent++;
counter_x_sent++;
}
else if ((part_iter->second.position().x()<(grid_locations[grid_iter->first][1]) && part_iter->second.position().x()>(grid_locations[grid_iter->first][1]-input_data.diam_large())))
{
particles_to_be_sent_east[grid_iter->first].push_back(part_iter->second);
counter_sent++;
counter_x_sent++;
}
part_iter++;
}
else
{
particle_grid[grid_iter->first].erase(part_iter++);
counter_removed++;
}
}
}
world.barrier();
mpi::request reqs_x_send[particles_to_be_sent_west.size()+particles_to_be_sent_east.size()];
vector<multimap<int,int> > box_sent_x_info;
box_sent_x_info.resize(world.size());
vector<multimap<int,int> > box_received_x_info;
box_received_x_info.resize(world.size());
int counter_x_reqs=0;
//send particles
for(grid_iter_vec=particles_to_be_sent_west.begin();grid_iter_vec!=particles_to_be_sent_west.end();grid_iter_vec++)
{
if (grid_iter_vec->second.size()!=0)
{
//send a particle. 50 will be "west" tag
if (input_data.periodic_walls_x() && (grid_iter_vec->first-floor(grid_iter_vec->first/(input_data.xz_numboxes()))*input_data.xz_numboxes()<input_data.z_numboxes()))
{
reqs_x_send[counter_x_reqs++]=world.isend(grid_rank_lookup[grid_iter_vec->first + input_data.z_numboxes()*(input_data.x_numboxes()-1)], grid_iter_vec->first + input_data.z_numboxes()*(input_data.x_numboxes()-1), particles_to_be_sent_west[grid_iter_vec->first]);
box_sent_x_info[grid_rank_lookup[grid_iter_vec->first + input_data.z_numboxes()*(input_data.x_numboxes()-1)]].insert(pair<int,int>(world.rank(), grid_iter_vec->first + input_data.z_numboxes()*(input_data.x_numboxes()-1)));
}
else if (!(grid_iter_vec->first-floor(grid_iter_vec->first/(input_data.xz_numboxes()))*input_data.xz_numboxes()<input_data.z_numboxes()))
{
reqs_x_send[counter_x_reqs++]=world.isend(grid_rank_lookup[grid_iter_vec->first - input_data.z_numboxes()], grid_iter_vec->first - input_data.z_numboxes(), particles_to_be_sent_west[grid_iter_vec->first]);
box_sent_x_info[grid_rank_lookup[grid_iter_vec->first - input_data.z_numboxes()]].insert(pair<int,int>(world.rank(),grid_iter_vec->first - input_data.z_numboxes()));
}
}
}
for(grid_iter_vec=particles_to_be_sent_east.begin();grid_iter_vec!=particles_to_be_sent_east.end();grid_iter_vec++)
{
if (grid_iter_vec->second.size()!=0)
{
//send a particle. 60 will be "east" tag
if (input_data.periodic_walls_x() && (grid_iter_vec->first-floor(grid_iter_vec->first/(input_data.xz_numboxes())*input_data.xz_numboxes())>input_data.xz_numboxes()-input_data.z_numboxes()-1))
{
reqs_x_send[counter_x_reqs++]=world.isend(grid_rank_lookup[grid_iter_vec->first - input_data.z_numboxes()*(input_data.x_numboxes()-1)], 2000000000-(grid_iter_vec->first - input_data.z_numboxes()*(input_data.x_numboxes()-1)), particles_to_be_sent_east[grid_iter_vec->first]);
box_sent_x_info[grid_rank_lookup[grid_iter_vec->first - input_data.z_numboxes()*(input_data.x_numboxes()-1)]].insert(pair<int,int>(world.rank(),2000000000-(grid_iter_vec->first - input_data.z_numboxes()*(input_data.x_numboxes()-1))));
}
else if (!(grid_iter_vec->first-floor(grid_iter_vec->first/(input_data.xz_numboxes())*input_data.xz_numboxes())>input_data.xz_numboxes()-input_data.z_numboxes()-1))
{
reqs_x_send[counter_x_reqs++]=world.isend(grid_rank_lookup[grid_iter_vec->first + input_data.z_numboxes()], 2000000000-(grid_iter_vec->first + input_data.z_numboxes()), particles_to_be_sent_east[grid_iter_vec->first]);
box_sent_x_info[grid_rank_lookup[grid_iter_vec->first + input_data.z_numboxes()]].insert(pair<int,int>(world.rank(), 2000000000-(grid_iter_vec->first + input_data.z_numboxes())));
}
}
}
counter=0;
for (int i=0;i<world.size();i++)
{
//if (world.rank()!=i)
//{
reqs[counter++]=world.isend(i,1000000000,box_sent_x_info[i]);
reqs[counter++]=world.irecv(i,1000000000,box_received_x_info[i]);
//}
}
mpi::wait_all(reqs, reqs + world.size()*2);
//receive particles
//receive west particles
for (int j=0;j<world.size();j++)
{
multimap<int,int>::iterator received_info_iter;
for (received_info_iter=box_received_x_info[j].begin();received_info_iter!=box_received_x_info[j].end();received_info_iter++)
{
//receive the message
if (received_info_iter->second<1000000000)
{
//receive the message
world.recv(received_info_iter->first,received_info_iter->second,particles_received_west);
//loop through all the received particles and add them to the particle_grid for this processor
for (unsigned int i=0;i<particles_received_west.size();i++)
{
particle_grid[received_info_iter->second].insert(pair<int,Particle>(particles_received_west[i].global_part_num(),particles_received_west[i]));
if(particles_received_west[i].position().x()>grid_locations[received_info_iter->second][0] && particles_received_west[i].position().x()<grid_locations[received_info_iter->second][1])
{
particle_properties[particles_received_west[i].global_part_num()].particle_in_box()[received_info_iter->second]=true;
}
counter_received++;
counter_x_received++;
}
}
else
{
//receive the message
world.recv(received_info_iter->first,received_info_iter->second,particles_received_east);
//loop through all the received particles and add them to the particle_grid for this processor
for (unsigned int i=0;i<particles_received_east.size();i++)
{
particle_grid[2000000000-received_info_iter->second].insert(pair<int,Particle>(particles_received_east[i].global_part_num(),particles_received_east[i]));
if(particles_received_east[i].position().x()>grid_locations[2000000000-received_info_iter->second][0] && particles_received_east[i].position().x()<grid_locations[2000000000-received_info_iter->second][1])
{
particle_properties[particles_received_east[i].global_part_num()].particle_in_box()[2000000000-received_info_iter->second]=true;
}
counter_received++;
counter_x_received++;
}
}
}
}
mpi::wait_all(reqs_y_send, reqs_y_send + particles_to_be_sent_bottom.size()+particles_to_be_sent_top.size());
mpi::wait_all(reqs_z_send, reqs_z_send + particles_to_be_sent_south.size()+particles_to_be_sent_north.size());
mpi::wait_all(reqs_x_send, reqs_x_send + particles_to_be_sent_west.size()+particles_to_be_sent_east.size());
cout<<"x sent "<<counter_x_sent<<" and received "<<counter_x_received<<" from rank "<<world.rank()<<endl;
cout<<"rank "<<world.rank()<<" sent "<<counter_sent<<" and received "<<counter_received<<" and removed "<<counter_removed<<endl;
cout<<"done passing"<<endl;
}
I only posted some of the code (so ignore the fact that some variables may appear to be undefined, as they are in a portion of the code I didn't post)
When I run the code (on the machine in which it fails), I get done passing but not done passing particles
I am lost as to what could possibly cause a segmentation fault between the end of the called function and the next line in the calling function and why it would happen on one machine and not another.
If you're crashing between the end of a function and the subsequent line in the caller, you're probably crashing in the destructor of a local variable. You need to run the program in a debugger to find out which object's destructor is crashing.
There are a couple of possibilities:
You actually are returning, but cout is buffered by the OS so you don't see "done passing particles" because the application crashes first.
You have some local class that has a destructor that is seg faulting.
Try running it in a debugger to find out where it is actually crashing.
Edit:
Since you've mentioned you're using gcc, add the -g flag and run it with gdb. Gdb will then tell you exactly where it's going wrong (probably a null dereference).
Just in case anyone comes back to this later. I updated to the newest version of boost mpi(at the time), 1.50 and this issue went away. Not much of a solution, but it worked.

Strange behavior with 'setMaxMailboxSize'

I've written a simple, though highly multi-threaded, prime numbers generator.
The algorithm goes like this:
Thread 0: generates consecutive numbers.
Threads 1 .. N: filter out numbers that are not prime.
Upon each 'new' prime discovery, a new filter thread is added.
Take I: no flow control at all.
Thread 0 'send's numbers absolutely freely.
The program finishes with signal 11 (seg. fault), rarely signal 8, even more rarely finishes successfully.
Take II: flow control with 'setMaxMailboxSize' to 1.
Most of the time, everything works well.
Take III:
Now, if it all was a result of some internal unheld overflow, it should do well with 'setMaxMailboxSize' to 2 (or even 10), am I wrong ?
Thread 0 becomes stuck after it blocks for the first time.
Could someone please direct me what do I miss ?
Note 1:
I use DMD v2.053 under Ubuntu 10.04
Note 2:
This is my code:
#!/usr/bin/dmd -run
import std.stdio;
import std.conv;
import std.concurrency;
void main(string[] args)
{
/* parse command line arguments */
if (args.length < 2) {
writeln("Usage: prime <number of primes to generate>");
return;
}
auto nPrimes = to!int(args[1]);
auto tid = spawn(&generate, thisTid);
/* gather produced primes */
for (;;) {
auto prime = receiveOnly!int();
writeln(prime);
if (--nPrimes <= 0) {
break;
}
}
tid.send("stop");
}
void generate(Tid parentTid)
{
bool terminate = false;
// filter stage 1
auto tid = spawn(&filter_stage, parentTid);
/* WHAT DO I MISS HERE ? */
setMaxMailboxSize(tid, 1, OnCrowding.block);
for (int i = 2; !terminate; i++) {
receiveTimeout(0,
(string cmd) {
writeln(cmd);
terminate = true;
}
);
tid.send(i);
}
}
void filter_stage(Tid parentTid)
{
auto prime = receiveOnly!int();
parentTid.send(prime);
// filter stage 'N'
auto tid = spawn(&filter_stage, parentTid);
filter(prime, tid);
}
void filter(int prime, Tid tid)
{
for (;;) {
receive (
(int number) {
if (number % prime != 0) {
tid.send(number);
}
}
);
}
}
Sounds like a bug in std.concurrency. Try upgrading DMD to 2.055. I'm not sure if this specific bug is fixed but there are a lot of bug fixes between 2.053 and 2.055. If it's still broken then please file a bug report at http://d.puremagic.com/issues/.