Segmentation error on startup, but success on debugging

I encountered a wired segmentation error. I am developing software in C using the Eclipse CDT. When I run my program on the terminal (Ubuntu 10, 64 bit), it simply reports "Segmentation Error". However, when I debug using gdb in Eclipse, it ends and the result is correct.

I understand that there can be many reasons for segmentation errors. And I'm sorry that I can not show my code, since I do not know where the problem may be ...

But can someone help me, is there some kind of situation that can happen in my case: segmentation error on terminals, as well as good debugging? Many thanks.


Thank you all. I spent some time learning valgrind. I just fixed the error by replacing malloc () with realloc (). The call is followed by two memcpy. This is the reason? Here is the snippet code:

bwa_seq_t *merge_seq (bwa_seq_t *s1, bwa_seq_t *s2) {
  ubyte_t *seq1, *seq2, *tmp;
  if (!s1 || !s2)
    return 0;
  seq1 = s1->seq;
  seq2 = s2->seq;
  tmp = (ubyte_t*) calloc (sizeof(ubyte_t), (s2->len + s1->len + 1));
  memcpy(tmp, seq1, sizeof(ubyte_t) * s1->len);
  memcpy(&tmp[s1->len], seq2, sizeof(ubyte_t) * s2->len);
  s1->len += s2->len;
  tmp[s1->len] = '\0';
  s1->seq = tmp;
  return s1;
}

Can someone explain why?

+5
source share
7 answers

Try the following:

  • type ulimit -c unlimitedin xterm (this allows you to create core / postmorterm files)

  • run your program (and let it crash): now the main file should be in the directory.

  • run the debugger with gdb <yourprogram> <corefile>

  • type btgdb at the command prompt to find out which line it worked on.

  • (optional) fix the error.

+9
source

, , , :

$ ulimit -c unlimited   # to create a corefile
$ yourprogram
...
crash                   # this will create file "core" in the current directory
$ gdb yourprogram core  # shows the state at the moment of the crash

:

+5

, , . ( 14 )

0

gdb .

0

, - Undefined. , Valgrind . , INVALID READ, INVALID WRITE Valgrind. , . .

0

. Linux GCC, Visual Studio 2005. , , ( ) . , , , , - .. . , , , .

printf , .

, ..: -)

0

BTW: here you shed memory. s1-> seq is basically redistributed, but you forget to free the old memory. Change:

s1->len += s2->len;
tmp[s1->len] = '\0';
s1->seq = tmp;
return s1;

at

void *del; // <<--
s1->len += s2->len;
tmp[s1->len] = '\0';
del = s1->seq; // <<--
s1->seq = tmp;
free (del); // <--
return s1;

and at least you stop the leak.

0
source

All Articles