Re: Segmentation fault when free



--- On Sat, 9/20/08, Nash Nipples <trashy_bumper@xxxxxxxxx> wrote:

From: Nash Nipples <trashy_bumper@xxxxxxxxx>
Subject: Re: Segmentation fault when free
To: freebsd-questions@xxxxxxxxxxx
Date: Saturday, September 20, 2008, 4:14 AM
--- On Fri, 9/19/08, Unga <unga888@xxxxxxxxx> wrote:

From: Unga <unga888@xxxxxxxxx>
Subject: Segmentation fault when free
To: freebsd-questions@xxxxxxxxxxx
Date: Friday, September 19, 2008, 9:17 AM
Hi all

I'm running FreeBSD 7 on i386. I have a C program
compiled with gcc 4.2.1 20070719.

Logically my program is:

char *a;
char *b;
char *c;

while (cond)
{
a = f1(); /* malloc() and send a string */
b = f2(); /* malloc() and send a string */

c = (char *) malloc(strlen(a) + strlen(b) + 1);
c[0] = '\0';

strcat(c, a);
strcat(c, b);

free(a);
free(b);
}

When it executes free(b), my program exits with
Segmentation fault: 11. The free(a) executes well.

The problem is with free(b). Even swap free(b) first
and
free(a) next, it still crashes at free(b).

If I comment out free() lines, further down the
program,
first few characters of one string get dropped when
executes
a completely unrelated line.

How could I bit more narrow down the problem?

Many thanks in advance.

Kind regards
Unga



im affraid i didnt implement your request correctly but the
program below did not crash my server under root in 60
seconds

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *a;
char *b;
char *c;

char *abd = "Hi, im a string 1\0";
char *bbd = "Hey, im a string 2\0";

char *f1(void){
char *ab;
ab = malloc(strlen (abd));
memcpy(ab, abd, strlen(abd));
printf("f1(): %s\n", ab);
return ab;
}

char *f2(void){
char *bb;
bb = malloc(strlen (bbd));
memcpy(bb, bbd, strlen(bbd));
printf("f1(): %s\n", bb);
return bb;
}

int
main(void)
{

while (1)
{

a = f1(); /* malloc() and send a string */
b = f2(); /* malloc() and send a string */

c = (char *) malloc(strlen(a) + strlen(b) + 1);
c[0] = '\0';

strcat(c, a);
strcat(c, b);

free(a);
free(b);
}
}


Hi thank you very much for your reply and the test case.

That is, in a trivial case like this, free() works well. Hopefully free() works well in all cases too.

But my main program is 1900 lines, f1() and f2() are in a 2200 lines second file. The f1() and f2() calls some functions from a 500 lines third file. The main program call another function, f3(), from 2nd file, pass pointers to two functions f4(), f5() of main program. The while loop iterate more than one million times. Its quite a complex situation.

There must be an error somewhere else. I noted free() causes lot of troubles. It is easy to write complex programs if you just let to leak memory. But in my case, since the program iterate millions of times, if I let to leak, I'm sure it will run out of RAM.

So the question is, if you were to encounter this issue, how would you approach it and find the culprit?

I'm using pretty basic tools to write complex programs. I use Kate to write programs, Makefiles to compile, use GCC, and use ddd in case of a trouble.

Best regards
Unga





_______________________________________________
freebsd-questions@xxxxxxxxxxx mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscribe@xxxxxxxxxxx"



Relevant Pages

  • Re: Question about gcc on OS X 10.0.4.11 Tiger
    ... running Linux and gcc 4.0.2. ... How do I switch to a different version of gcc to compile with on OS X ... fgetsis a libc function and is not really part of the compiler. ... int main(int argc, char* argv){ ...
    (comp.sys.mac.programmer.help)
  • Re: malloc warning gcc > 4.0
    ... > When I compile this code: ... > char* p; ... I know it works with gcc 3.3.6. ... implicit prototype has a return type of int. ...
    (comp.lang.c.moderated)
  • Re: malloc warning gcc > 4.0
    ... > char* p; ... >warning: incompatible implicit declaration of built-in function 'malloc' ... I know it works with gcc 3.3.6. ... which should compile without error. ...
    (comp.lang.c.moderated)
  • gets() function generates strong warning message with gcc compiler
    ... I use gcc compiler,but when I wanted to use ... function,but now it take segmentation fault error.Please help me ... char * getstring ...
    (comp.lang.c)
  • sprintf basics (was: C99 IDE for windows)
    ... you know at compile time that you need ... char arrc; ... Segmentation fault ... [arnuld@dune ztest]$ ...
    (comp.lang.c)