简体   繁体   中英

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.

Tried to clear out every thing to make sure I'm not sending NULL length or whatever, but still no luck.

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).

strlen(label) = 7 (checked it)

Any ideas ? I'm using GCC compiler on Eclipse (Ubuntu).

Per FoggyDay's request this is the whole function:

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.

I wish I could mark all of your answers with V sign to indicate it solved the issue since you've been most helpful.

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)

Again, I thank all of you for showing me other issues I had in my code.

StackOverflow ROCKS !

1) As already mentioned above, "malloc()" MUST BE "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:

a) is "label" valid when we call 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?

SUGGESTIONS:

a) Add this code (or better, look at "label" in your debugger):

   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".

PS: If you're feeling ambitious, check out my link to the Valgrind tutorial above it.

But for "quick results", please try suggestions 1) and 2); and let us know how it goes.

if strlen(label) is indeed 7, than it's not strlen() but malloc() itself that crashes.

If malloc() crashes, that probably means malloc()'s internal housekeeping was destroyed earlier/elsewhere (by a pointer gone crazy).

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.

Scratch that.

i dont understand whatever function type that is there, but to my knowledge of malloc(); and strings, since label is an array you should send it like this

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

}

hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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