LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   seg fault on fclose (); (https://www.linuxquestions.org/questions/programming-9/seg-fault-on-fclose-%3B-68267/)

lackluster 06-26-2003 03:21 PM

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?

Hko 06-27-2003 11:22 AM

Assuming there's no "free jt_file" somewhere in the <SNIP>ed parts (or in functions called there), it sounds like a bug.

lackluster 06-27-2003 11:52 AM

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;


Pres 06-28-2003 12:18 PM

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.

Hko 06-28-2003 07:30 PM

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.


All times are GMT -5. The time now is 11:09 AM.