Greetings all,
ERROR: Program received signal 'SIGSEGV', Segmentation fault.
I am having some issues with the following code creating the above fault in Code::Blocks. It is for a chatbox I am using for a network chat program where the vector is filled with the strings of text for each line of the chat log. I don't see why its throwing a segmentation fault as I am not trying write to any memory at all with this routine.
The line creating the error is [if(iter->empty());]. If I remove that line then it will still throw the fault at the DrawText function call.
Can anyone help me out? It's been a nightmare for me at the moment trying to debug it!
FYI -> I am coding in Code::Blocks on Ubuntu. Program uses SDL, particularly the 'net' and 'thread' headers. The DrawText function is simply a wrapper for TTF_RenderTextBlended() and SDL_BlitSurface, but I know the error isn't in the DrawText function directly because I have used it with many other projects with no issues.
Everything I check points towards there being an issue with the strings in the vector, but I cannot work out what?
void GUI_ChatBox::Render(SDL_Surface *screen)
{
int line = 0;
for(vector<string>::reverse_iterator iter = L.rbegin(); iter != L.rend(); ++iter)
{
if(iter->empty())
continue;
++line;
DrawText(screen, iter->c_str(), x, (y + height) - (line * CHAR_HEIGHT));
}
}
L in the above example was short for - LineBuffer. Only one other function interacts with it and that is the function used to add text to the vector. Here it is:
void GUI_ChatBox::AddText(std::string text)
{
++index;
if(index >= maxLines)
{
index = maxLines;
LineBuffer.erase(LineBuffer.be开发者_运维问答gin());
}
LineBuffer.push_back(text);
}
That function will usually receive a char* array as the std::string parameter, but I have done this elsewhere with no issues.
Where is L
defined? And how is it being initialized? Does any other code modify it after initialization, either intentionally or accidentally? It sounds like the vector contains garbage for some reason. The problem almost certainly isn't with the code you've presented here, but rather with some other code that interacts with L
. Maybe you've got a array being overrun somewhere else that's clobbering L
, for example.
- If
0
isn't a valid value of the function argumentscreen
, then I strongly suggest pass-by-reference instead of pass-by-pointer; even make itconst
ifDrawText
, where it's actually used, allows it. - You say that "The line creating the error is [if(iter->empty());]. If I remove that line then it will still throw the fault at the DrawText function call.". What's the least common denominator on those two lines? Yes,
iter->
. - Where does
iter
come from? Yes,L
. - Where does
L
come from? Well, only you know that. - Somewhere between creating
L
and using it inGUI_ChatBox::Render
, it gets trashed.
I went all the way through my code seeing what was passed into the AddText function that could possibly break my LineBuffer vector.
I found one obscure reference to the GetText function for my Textbox which would return a (char*) with the characters in the textbox.
This function would return NULL if the textbox had no characters. I went through and changed everything to operate on strings except for in the interals of the objects. And now everything seems to be playing nice. At least I cannot replicate the error stated above after making those changes.
Thought I would put this answer up in case it helps anyone else out in the same situation.
精彩评论