to all the specialists here ...
i've much trouble with a selfwritten readline method in c++ on linux. From time to time it throws an exception, SEGMATATION FAULT. I knew that it seems the readline writes to not allowed memory, it happens when the line from the tcp stream is broken. Can someone help me to correct the readline to make it more safe but it should not be much slower i need the performance in reading lines splitted at \r\n from the stream. The other possibility is to correct the readline to not throw SEG FAULT or able to catch SEG FAULT without killing the Application (like try catch in windows).
Here is the code of the Readline :
int readLineChatPri(std::string* line, int sock)
{
static std::string buffer1;
std::string::iterator pos;
int n = 0;
try
{
char buf[MAXRECV];
while ((pos = find(buffer1.begin(), buffer1.end(), '\n')) == buffer1.end())
{
n = read(sock, buf, MAXRECV - 1);
if (n == 0)
return n;
if (n == -1)
{
*line = buffer1;
buffer1 = "";
return n; // Error Handling
}
buf [n] = 0;
buffer1 += buf;
}
*line = string(buffer1.begin(), pos); // pos + 1 with \r\n pos with \n pos -1 without \r\n
buffer1 = string(pos + 1, buffer1.end());
return n;
}
catch (exception& e)
{
logErr.WriteLogEntry("Error in <readLineChatPri 1000> : " + boost::lexical_cast<string>(e.what()));
return -1;
}
}
Hope someone can help me if you need further informations i will provide them.
Thanx Sascha