views:

70

answers:

3

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.begin());
    }

    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.

A: 

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.

Peter Milley
See edit above.
sethXM
Next question: are `Render` and `AddText` being called from separate threads? That could easily explain this. The call to `LineBuffer.erase()` in `AddText` would invalidate the iterators in `Render`.Failing that, my next guess would be that some other code is overwriting the memory in LineBuffer accidentally.
Peter Milley
Same thread. I think I've worked out the cause though, ill post it now.
sethXM
A: 
  1. If 0 isn't a valid value of the function argument screen, then I strongly suggest pass-by-reference instead of pass-by-pointer; even make it const if DrawText, where it's actually used, allows it.
  2. 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->.
  3. Where does iter come from? Yes, L.
  4. Where does L come from? Well, only you know that.
  5. Somewhere between creating L and using it in GUI_ChatBox::Render, it gets trashed.
Johann Gerell
A: 

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.

sethXM
Nope this didn't fix it. Looks like it just went away for a bit and now its back!
sethXM