From owner-freebsd-questions@FreeBSD.ORG Sat Sep 20 07:31:42 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 3B14B106564A for ; Sat, 20 Sep 2008 07:31:42 +0000 (UTC) (envelope-from unga888@yahoo.com) Received: from web57003.mail.re3.yahoo.com (web57003.mail.re3.yahoo.com [66.196.97.107]) by mx1.freebsd.org (Postfix) with SMTP id E23F18FC0A for ; Sat, 20 Sep 2008 07:31:41 +0000 (UTC) (envelope-from unga888@yahoo.com) Received: (qmail 636 invoked by uid 60001); 20 Sep 2008 07:31:41 -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=KJQgo0D4/jPxAhukaKSVR3dnvvUVNj1HskvcjP3B6R1FFe74GPWq3ApzxRUXsrAPsBluPcUBgPfgfxTCutmY6ERmwlx2k3qQKlu7eed3DrA8kW6jQWZQaBYwA6u+32f96bZmLCEJ/b+pL3kq39M45SjdbRh6s+ov60L6A8frtd8=; X-YMail-OSG: 5xPiCmAVM1lUcTZHKZInR9PhgFAdsmLc3XQ8JRwN14SX4QEgC5rp.rBmizbOrReN5Sl3L1EWUrgXahODx4Ad..iauRn_IXtFknjfhPllBdwHZaABK0NcWPHLFTnfixVN8zSDSZxYizaxSIv_VHa3BRpYip50.pMevMitJsX0iBOXatXoBmxS Received: from [220.255.7.144] by web57003.mail.re3.yahoo.com via HTTP; Sat, 20 Sep 2008 00:31:41 PDT X-Mailer: YahooMailWebService/0.7.218.2 Date: Sat, 20 Sep 2008 00:31:41 -0700 (PDT) From: Unga To: Giorgos Keramidas In-Reply-To: <874p4bgwtr.fsf@kobe.laptop> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Message-ID: <260951.294.qm@web57003.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 07:31:42 -0000 --- On Sat, 9/20/08, Giorgos Keramidas wrote: > From: Giorgos Keramidas > Subject: Re: Segmentation fault when free > To: unga888@yahoo.com > Cc: trashy_bumper@yahoo.com, freebsd-questions@freebsd.org > Date: Saturday, September 20, 2008, 2:23 PM > On Fri, 19 Sep 2008 20:03:03 -0700 (PDT), Unga > wrote: > > 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. > > You are probably calling free() multiple times for the same > buffer. > > Try tracing the malloc and free calls, using the > information from this > message: > > http://lists.freebsd.org/pipermail/freebsd-questions/2008-July/179480.html > > > 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. > > Leaking memory is *never* a good choice. Especially if you > are writing > library code that others are supposed to use, or code that > is supposed > to run millions of times. > > While it's understandable as a 'quick hack' > when you are first writing a > program, I've seen far too many 'quick hacks' > that lived years and years > after the original 'experimental' period passed. > Hence the knee-jerk > reaction when I see leaks being used as a > 'temporary' solution :) Hi Giorgos, thank you very much for your reply. ktrace.out shows: malloc_init() 0x8103400 = malloc(1024) malloc_init() malloc_init() 0x810b0b0 = malloc(400) : so many malloc : so many free : malloc/free combinations : free(0xbfbfc9c9) 1. This clearly shows my program is trying to free a memory that has not been allocated. How it could have happened? 2. Is it correct to have many malloc_init()? Best regards Unga