LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-26-2003, 03:21 PM   #1
lackluster
Member
 
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Rep: Reputation: 30
seg fault on fclose ();


I tried everything! Can somebody help me with this?

Code:
bash-2.05b$ uname -a
CYGWIN_NT-5.0 <computername>1 1.3.20(0.73/3/2) 2003-02-08 12:10 i686 unknown unknown Cygwin
Code:
void load_jump_table() {
	FILE * jt_file;
	char * buffer = (char *) malloc(BUFSIZE * sizeof(size_t));
	<SNIP>

	if ((jt_file = fopen(JUMPFILE, "r")) == NULL) {
		fprintf(stderr, "%s could not be opened for writing\n", JUMPFILE);
		exit(1);
	}

	<SNIP>

	jump_table = (unsigned long *) malloc((word_range[RANGE_HIGH] - word_range[RANGE_LOW]) * sizeof(size_t));

	while (!(bytes_read < BUFSIZE)) {
		bytes_read = fread((void *) buffer, sizeof(char), BUFSIZE, jt_file);
		<SNIP>
	}

	if (jt_file) fclose(jt_file); /* this friggin' line */
	free((void *) buffer);
}
Code:
Program received signal SIGSEGV, Segmentation fault.
0x6103e20c in mktime ()
(gdb) bt
#0  0x6103e20c in mktime ()
#1  0x6103ec46 in free ()
#2  0x610a999c in fclose ()
#3  0x00401600 in load_jump_table () at main.c:121
#4  0x004016b9 in main (argc=1, argv=0xa043180) at main.c:132
Any thoughts? Is this a bug?
 
Old 06-27-2003, 11:22 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Assuming there's no "free jt_file" somewhere in the <SNIP>ed parts (or in functions called there), it sounds like a bug.
 
Old 06-27-2003, 11:52 AM   #3
lackluster
Member
 
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Original Poster
Rep: Reputation: 30
even if there were fclose (jt_file); lines in the <SNIP>'s (there's not), wouldn't this line not try to free it again?

if (jt_file) fclose(jt_file); /* this friggin' line */

from my understanding fclose resets the file handle to NULL after it closes it.

anyways, i just left the line commented and continued as normal ..... it was stupid program anyways ... computer-generated poetry . Here's some output:

Code:
bash-2.05b$ cpoetry.exe

consolable masochism,
        preliterate nonclassical,
chaffe methyltestosterone,
        transsexual gambado,
telethermometer cothurnus unstartling;

subtrahend respite horresco,
        slacks leptodactylus,
thews lobectomy,
        sulla corruptness sphacelation,
serrate opisthobranchia prevents;
 
Old 06-28-2003, 12:18 PM   #4
Pres
Member
 
Registered: Jun 2002
Location: Australia
Distribution: Slack 9.1
Posts: 232

Rep: Reputation: 30
Not much help I know

This is probably irrelevant but the jt_file was never opened for writing, only reading, in contradiction to your error message.

Something more helpful, perhaps if you are reading past the EOF you subsequently cause segfault in fclose.
 
Old 06-28-2003, 07:30 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
even if there were fclose (jt_file); lines in the <SNIP>'s (there's not), wouldn't this line not try to free it again?

if (jt_file) fclose(jt_file); /* this friggin' line */
The stack backtrace you posted makes makes me think that it does try to free it again:
Quote:
#1 0x6103ec46 in free ()
#2 0x610a999c in fclose ()
Quote:
from my understanding fclose resets the file handle to NULL after it closes it.
I'm not sure about that. My man page (debian sarge) doesn't mention NULL, but says:
Quote:
In either case any further access (including another call to fclose()) to the stream results in undefined behaviour.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
weird seg fault zaichik Programming 7 01-29-2005 06:34 AM
C seg fault drigz Programming 5 10-01-2004 03:35 PM
ut2004 seg fault dopefish Linux - Games 3 04-11-2004 02:44 AM
gnome_error_dialog seg fault?? Castro Programming 3 06-11-2003 01:28 PM
gnome_app_create_menus seg fault in RH 7.3 Castro Programming 0 05-30-2003 08:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration