istringstream causes my function to crash


So I am currently implementing a state reading function that reads snapshot states from a 6DOF robot (So basically I have x y z and alpha and beta coordinates, a tool position and a speed value). I read one line into the file and then pass it to a decoding function that stores every positions, distance with the last position and speeds as well as execution time in arrays. Everything works fine but at the end of the function the program crashes.

Whenever a crash occurs, I could remove the whole code of the function and then run the program again and it still crashes so I expect some memory to be corrupted or something like that. I changed from passing by value to passing by reference, it didn't change anything.

Then I fixed the issue by splitting the input string and then assigning it to the istringstream, BUT now I have an issue somewhere else in the code that makes the program crash. I write a line in a file using an ofstream and the logs give the same error that I had when it crashed because of the istringstream... I don't really understand what's happening but I suspect that buffers are being too short and that my program wants to write past the size of the buffer but I'm not really sure about that and even less sure on how to fix this issue. Any help would be very welcome !

Here is the code of the function that is executed where I manipulate some arrays (Which may be the actual cause)

void Hub::DecodeAndAct(std::string const& line)
    long long timeout = 0;
    long long ctimeout = 0;
    const char* stages[7] = {"X","Y","Z","Alpha","Beta","ToolActuator","Speedlayer"};
    int i;
    int index;
    int strIndex1;
    int strIndex2;
    int counter = 0;
    double speedLayer;
    long long speed[6];
    long long distance[6];
    double position[6];
    std::string stageString;
    std::string stringPos;
    std::string stringSpeed;
    std::stringstream stringStream;
    SA_STATUS st;
    SA_PACKET packet;

    for(i = 0 ; i <= 5; i++)
        strIndex1 = line.find(stages[i]);
        strIndex2 = line.find(stages[i+1]);
        stringStream.str(line.substr(strIndex1, (strIndex2-strIndex1)));
        stringStream >> stageString;
        stringStream >> stringPos;
        position[i] = std::stod(stringPos);
    strIndex1 = line.find(stages[6]);
    stringStream >> stringSpeed;
    stringStream >> stringSpeed;
    speedLayer = std::stod(stringSpeed);

and I use the extracted positions and speed and stages to do stuff with the robot

and the other function for writing a line in a file

void Hub::WriteToRecordingFile(std::string input)
    if(newFile == true)
        newFile = false;
        recordingFile << input;
    recordingFile <<  '\n' + input;

The code above works as long as the first code (DecodeAndAct) has NOT been executed. As soon as the DecodeAndAct function has been executed at least once, the program crashes in the function WriteToRecordingFile

Feel free to ask me any supplementary information!

I'm using the open source software Micro manager and this is the error I get as the program simply shuts down without any error message :

# A fatal error has been detected by the Java Runtime Environment:
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000007798f23c, pid=6068, tid=1392

I tried to make a minimal reproducible example with no success so far, whenever I simply do it on a new c++ project it works just fine. I'm coding on visual studio 2010 running on C++98 and with _MSC_FULL_VER I found out my compiler version is 160030319 and my debugger the one that is in visual studio 2010.

asked on Stack Overflow Jul 28, 2020 by Jon Märki • edited Jul 29, 2020 by Jon Märki

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0