0

I've "finished" my recover.c, but valgrind says about leaks of memory. It seems like i haven't closed all of my opened files. But I have a problem. If I put fclose function in my loop, to close a file, when I want to open another, I get segmentation fault. Here's my code fragment reading "card.raw" file opened as "card":

FILE* new_file = NULL;

//string for titles of files
char title [7];

//buffer for 1 block of 512 bytes (unsigned int - 4 bytes)
unsigned int buffer[128];

while(fread(&buffer, 4, 128, card) == 128)
{
        if (buffer[0] == 0xe0ffd8ff || buffer[0] == 0xe1ffd8ff)
        { 
            sprintf(title, "%03i.jpg", file_counter++);
            if (new_file == NULL)
            {
                new_file = fopen(title, "a");
                fwrite(&buffer, 4, 128, new_file);
            }else
            {
                fclose(new_file);  //causes SEGMENTATION FAULT 
                /* If I remove fclose, program creates 16 files properly but valgrind sees memory leaks because I didn't close 15 of 16 files */
                new_file = fopen(title, "a");
                fwrite(&buffer, 4, 128, new_file);
            }
        }else if (new_file != NULL)
        {
            fwrite(buffer, 4, 128, new_file);
        }

}

fclose(card);
fclose(new_file);
4
  • Yes, expanded code would help. Also, when you run your program, does it create any files? If so, how many and what filenames?
    – Cliff B
    Aug 4 '15 at 20:57
  • Include your code with i.
    – i_am_david
    Aug 4 '15 at 21:20
  • I pasted more code. @CliffB if I remove fclose function, my program creates all 16 files with good filenames. Problem is, that check50 won't accept my code, because it sees some trash output. Valgrind sees memory leaks, because I didn't close 15 of my files (opened in loop). Aug 5 '15 at 8:46
  • Does it create any files when you do close the files as in the code above? (even one)
    – L.B.
    Aug 5 '15 at 14:22
1

It's amazing how a simple mistake can hide in plain sight. I spent a fair amount of time trying to replicate your seg fault, but couldn't, but I also couldn't produce more than one jpg file. I kept looking at the code, and it all looked right, until I started looking closely at your output file name.

Rule #1 about strings is that you have to leave an extra character for that end of string marker. In this case, it's your file name variable, title.

char title [7];

It should be char title [8]; My buest guess is that you've been sliding off the end of the string by 1 character and causing a seg fault. What I don't understand is why I didn't get the seg fault, even though I was getting other problems. Could be because I was guessing at the few remaining declarations that weren't included in your code. Anyways, make the change and let us know if that cures your current issue.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

1
  • Yes now it works! Gosh, how could I be so stupid while making that "title" variable! I have to learn more on my mistakes. Thank you very much! :-) Aug 6 '15 at 12:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .