From owner-freebsd-questions@FreeBSD.ORG Wed May 17 18:41:36 2006 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 46DB016B83A for ; Wed, 17 May 2006 18:41:36 +0000 (UTC) (envelope-from wmoran@collaborativefusion.com) Received: from mx00.pub.collaborativefusion.com (mx00.pub.collaborativefusion.com [206.210.89.199]) by mx1.FreeBSD.org (Postfix) with ESMTP id 0EC7643D49 for ; Wed, 17 May 2006 18:41:32 +0000 (GMT) (envelope-from wmoran@collaborativefusion.com) Received: from vanquish.pgh.priv.collaborativefusion.com (vanquish.pgh.priv.collaborativefusion.com [192.168.2.61]) (AUTH: PLAIN wmoran, TLS: TLSv1/SSLv3,256bits,AES256-SHA) by wingspan with esmtp; Wed, 17 May 2006 14:41:32 -0400 id 00056405.446B6E5C.0000E8A4 Date: Wed, 17 May 2006 14:41:31 -0400 From: Bill Moran To: Lorin Lund Message-Id: <20060517144131.96ee24a2.wmoran@collaborativefusion.com> In-Reply-To: <446B6D38.4070105@infowest.com> References: <3ee9ca710605171109i57ae4064x702216a1619d6c41@mail.gmail.com> <446B6D38.4070105@infowest.com> Organization: Collaborative Fusion X-Mailer: Sylpheed version 2.2.4 (GTK+ 2.8.17; i386-portbld-freebsd6.0) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: greenwood.andy@gmail.com, freebsd-questions@freebsd.org Subject: Re: C coding question X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 17 May 2006 18:41:40 -0000 On Wed, 17 May 2006 12:36:40 -0600 Lorin Lund wrote: > Andy Greenwood wrote: > > > I am helping someone work on porting some code to Freebsd, and the > > code below works on Linux, but not on FreeBSD (compiles, but gives > > Segmentation Fault: 11). I'm not sure where the problem is, and any > > pointers would be much appreciated. > > > > /* Check if we must stop */ > > if(tf_stat_file != NULL) > > { > > tf_stat = fopen(tf_stat_file, "r"); > > if (tf_stat != NULL) > > { > > /* Get state */ > > stat_state=fgetc(tf_stat); > > > > /* Torrentflux asked to shutdown the torrent */ > > if (stat_state == '0') > > { > > mustDie = 1; > > } > > } > > fclose(tf_stat); > > } > > _______________________________________________ > > I think I would move the > fclose( tf_stat) > up a line. No need to close a file that failed to open. The fclose( ) > acting on > a NULL pointer might be your error. >From man fclose: The fclose() function does not handle NULL arguments; they will result in a segmentation violation. This is intentional - it makes it easier to make sure programs written under FreeBSD are bug free. This behaviour is an implementation detail, and programs should not rely upon it. Good catch. -- Bill Moran Collaborative Fusion Inc.