矢量中的分段错误用于将文本绘制到我的自编码 GUI 的迭代器函数
大家好,
错误: 程序收到信号“SIGSEGV”,分段错误。
我在使用以下代码时遇到一些问题,在 Code::Blocks 中创建了上述错误。它适用于我用于网络聊天程序的聊天框,其中向量填充了聊天日志每一行的文本字符串。我不明白为什么它会抛出分段错误,因为我根本没有尝试使用此例程写入任何内存。
创建错误的行是 [if(iter->empty());]。如果我删除该行,那么它仍然会在 DrawText 函数调用时抛出错误。
有人可以帮我吗?此刻尝试调试它对我来说是一场噩梦!
仅供参考 ->我正在 Ubuntu 上使用 Code::Blocks 进行编码。程序使用 SDL,特别是“net”和“thread”标头。 DrawText 函数只是 TTF_RenderTextBlend() 和 SDL_BlitSurface 的包装器,但我知道错误并不直接出现在 DrawText 函数中,因为我已经将它用于许多其他项目,没有出现任何问题。
我检查的所有内容都表明向量中的字符串存在问题,但我无法弄清楚是什么?
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 是 - LineBuffer 的缩写。只有另一个函数与它交互,那就是用于向向量添加文本的函数。就是这样:
void GUI_ChatBox::AddText(std::string text)
{
++index;
if(index >= maxLines)
{
index = maxLines;
LineBuffer.erase(LineBuffer.begin());
}
LineBuffer.push_back(text);
}
该函数通常会接收一个 char* 数组作为 std::string 参数,但我在其他地方这样做没有任何问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
L
在哪里定义的?它是如何初始化的?初始化后是否有其他代码有意或无意地修改了它?听起来该向量由于某种原因包含垃圾。几乎可以肯定,问题不在于您在此处提供的代码,而在于与L
交互的其他一些代码。例如,也许您的数组在其他地方溢出,导致L
崩溃。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 withL
. Maybe you've got a array being overrun somewhere else that's clobberingL
, for example.0
不是函数参数screen
的有效值,那么我强烈建议使用引用传递而不是指针传递;如果实际使用的 DrawText 允许的话,甚至可以将其设置为 const。iter->
。iter
从哪里来?是的,L
。L
从哪里来?嗯,只有你知道。L
和在GUI_ChatBox::Render
中使用它之间的某个地方,它会被丢弃。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.iter->
.iter
come from? Yes,L
.L
come from? Well, only you know that.L
and using it inGUI_ChatBox::Render
, it gets trashed.我仔细检查了我的代码,看看传递到 AddText 函数的内容可能会破坏我的 LineBuffer 向量。
我发现一个对我的文本框的 GetText 函数的模糊引用,该函数将返回一个 (char*) 以及文本框中的字符。
如果文本框没有字符,该函数将返回 NULL。我检查并更改了所有内容以对字符串进行操作,除了对象内部之外。现在一切似乎都进展顺利。至少在进行这些更改后我无法复制上述错误。
我想我会把这个答案提出来,以防它对处于同样情况的其他人有所帮助。
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.