简体   繁体   English

C程序崩溃,malloc行上的“ 0xb7e88a81”没有可用的源(Eclipse)

[英]C Program crash “No source available for ”0xb7e88a81“” on malloc line (Eclipse)

I have a function that takes a string and cuts out a some parts of it. 我有一个函数,它接受一个字符串并切掉它的某些部分。

The function does its thing a couple of times until, all of a sudden, the same malloc line that worked fine, crashes with No source available for "0xb7e88a81" error. 该函数执行了几次操作,直到突然,同一行malloc行运行正常,并崩溃,并没有“ 0xb7e88a81”错误可用的源。

Tried to clear out every thing to make sure I'm not sending NULL length or whatever, but still no luck. 试图清除所有内容,以确保我没有发送NULL长度或任何长度,但仍然没有运气。

It worked at least once (debugged it) but on the second or third iteration it crashes. 它至少工作了一次(调试过),但是在第二或第三次迭代中崩溃了。

char *removeOffsetFromLabel (char *label) {
    char* labelWithoutOffset;
    int i;

    labelWithoutOffset = malloc(strlen(label));
   ........

The crash happens on the malloc line (when trying to move to the next line). 崩溃发生在malloc行上(尝试移动到下一行时)。

strlen(label) = 7 (checked it) strlen(label)= 7(选中)

Any ideas ? 有任何想法吗 ? I'm using GCC compiler on Eclipse (Ubuntu). 我在Eclipse(Ubuntu)上使用GCC编译器。

Per FoggyDay's request this is the whole function: 根据FoggyDay的要求,这是整个功能:

char *removeOffsetFromLabel (char *label) {
char* labelWithoutOffset;
int i;

labelWithoutOffset = (char*)malloc(strlen(label) + 1);
i = 0;
while (label[i] != '\0' && label[i] != OPENING_BRACKET_ASCII_CODE) {
    labelWithoutOffset[i] = label[i];
    i++;
}
labelWithoutOffset[i] = '\0';
return labelWithoutOffset;
}

I do free up "labelWithoutOffset" outside of the function before calling it again. 在再次调用它之前,我确实在函数外部释放了“ labelWithoutOffset”。

I wish I could mark all of your answers with V sign to indicate it solved the issue since you've been most helpful. 希望我能用V形标记您的所有答案,以表明它已经解决了该问题,因为您一直以来都很有帮助。

After digging in I made two changes to my code and things seem to be working fine so far: 深入研究后,我对代码进行了两项更改,到目前为止,一切似乎都可以正常进行:

  1. Removed two "free" commands that were used on an already freed up pointers (dumb mistake) 删除了在已释放的指针上使用的两个“自由”命令(愚蠢的错误)
  2. Added "pointer = NULL" after every free (just to be on the safe side) 每次空闲后添加“ pointer = NULL”(为安全起见)

Again, I thank all of you for showing me other issues I had in my code. 再次感谢大家向我展示代码中遇到的其他问题。

StackOverflow ROCKS ! StackOverflow摇滚!

1) As already mentioned above, "malloc()" MUST BE "strlen()+1": 1)如上所述,“ malloc()” 必须为 “ strlen()+ 1”:

   char *removeOffsetFromLabel (char *label) {
      char* labelWithoutOffset = (char *)malloc(strlen(label)+1);

2) Since this didn't solve the problem, we also need to look at: 2)由于这不能解决问题,因此我们还需要查看以下内容:

a) is "label" valid when we call strlen()? a)当我们调用strlen()时,“标签”有效吗?

b) do you have any code that might be overwriting "labelWithoutOffset" somewhere else - after you've allocated it in one call, and before you allocate it again in a different call? b)您是否有任何代码可能在其他地方覆盖“ labelWithoutOffset”-在一个调用中分配它之后,再在另一个调用中再次分配它之前?

SUGGESTIONS: 建议:

a) Add this code (or better, look at "label" in your debugger): a)添加以下代码(或者更好的是,在调试器中查看“标签”):

   char *removeOffsetFromLabel (char *label) {
      fprintf (STDERR, "label=%s\n", label);
      fprintf (STDERR, "strlen(label)=%d\n", strlen(label);
      char* labelWithoutOffset = (char *)malloc(strlen(label)+1);

b) Post some more code from "removeOffsetFromLabel()" - maybe we can see where the variable might be "getting stepped on". b)从“ removeOffsetFromLabel()”中发布更多代码-也许我们可以看到该变量可能在“被踩踏”的位置。

PS: If you're feeling ambitious, check out my link to the Valgrind tutorial above it. PS:如果您有雄心壮志,请查看我上面的Valgrind教程的链接。

But for "quick results", please try suggestions 1) and 2); 但是对于“快速结果”,请尝试建议1)和2); and let us know how it goes. 让我们知道如何进行。

if strlen(label) is indeed 7, than it's not strlen() but malloc() itself that crashes. 如果strlen(label)确实为7,则不是strlen()而是崩溃的malloc()本身。

If malloc() crashes, that probably means malloc()'s internal housekeeping was destroyed earlier/elsewhere (by a pointer gone crazy). 如果malloc()崩溃,则可能意味着malloc()的内部整理工作在更早/其他地方被破坏(由于指针发疯了)。

Bugs like this are hard (hardest) to find since you can't tell where they are because the crash is likely happening long after the cause. 这样的错误很难(最难发现),因为您无法分辨出它们在哪里,因为崩溃很可能在起因之后发生。

You might want to look into Valgrind usage. 您可能需要研究Valgrind的用法。

Scratch that. 从头开始。

i dont understand whatever function type that is there, but to my knowledge of malloc(); 我不明白那里的任何函数类型,但据我所知malloc(); and strings, since label is an array you should send it like this 和字符串,因为label是一个数组,您应该像这样发送它

void funcCall(int *)
main()
{
    funcCall(label)
}
funcCall(int funcLabel[])
{

}

hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM