From owner-freebsd-questions@FreeBSD.ORG Sat Sep 20 03:03:09 2008 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id E508F1065671 for ; Sat, 20 Sep 2008 03:03:09 +0000 (UTC) (envelope-from unga888@yahoo.com) Received: from web57007.mail.re3.yahoo.com (web57007.mail.re3.yahoo.com [66.196.97.111]) by mx1.freebsd.org (Postfix) with SMTP id 900B98FC08 for ; Sat, 20 Sep 2008 03:03:04 +0000 (UTC) (envelope-from unga888@yahoo.com) Received: (qmail 82533 invoked by uid 60001); 20 Sep 2008 03:03:03 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:X-Mailer:Date:From:Reply-To:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type:Message-ID; b=hwkASx80Cz4Myk7BXtP4aMCU8SZ+yiZAQqkVJd3MW5i/amM3n8BVdCB/fLkTRAQ7fBxLoN8nZRpACXIH0WrCo+I8Xg7a7swIiFC+lA6GvPvMznNtHCqWR99jqzT93hesf9Ciqg4KQYlHeOuiLelpqj8TT8QLizc7xmMf7Zfp2kk=; X-YMail-OSG: 4ebiByAVM1mfVjk8f2Pg8utJ9WSh3JDba.CUf0yOhE5Sb4pJFXZ.uvEZgI8xzL3r0haYR0zdQZ_skXkHseP2JlEpdJAFoz2A.n3ff.bqHjNiuvs18v.FLLVALWUe2wgmDZCvJuauOdXIrEes0ucitLTJM60- Received: from [220.255.7.245] by web57007.mail.re3.yahoo.com via HTTP; Fri, 19 Sep 2008 20:03:03 PDT X-Mailer: YahooMailWebService/0.7.218.2 Date: Fri, 19 Sep 2008 20:03:03 -0700 (PDT) From: Unga To: trashy_bumper@yahoo.com In-Reply-To: <606875.3915.qm@web110506.mail.gq1.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Message-ID: <614097.81584.qm@web57007.mail.re3.yahoo.com> Cc: freebsd-questions@freebsd.org Subject: Re: Segmentation fault when free X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: unga888@yahoo.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 20 Sep 2008 03:03:10 -0000 --- On Sat, 9/20/08, Nash Nipples wrote: > From: Nash Nipples > Subject: Re: Segmentation fault when free > To: freebsd-questions@freebsd.org > Date: Saturday, September 20, 2008, 4:14 AM > --- On Fri, 9/19/08, Unga wrote: > > > From: Unga > > Subject: Segmentation fault when free > > To: freebsd-questions@freebsd.org > > 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 > #include > #include > > 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