0

Can somebody please help? When I try to run my program I get this message,

* Error in `./recover': double free or corruption (out): 0x00007ffe270739f0 * Aborted

I can't seem to find the issue. From what I have read it is an issue with memory leak but nothing I do has any effect on it. Any help is much appreciated.

#include <cs50.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    // check correct usage
    if (argc != 2)
    {
        fprintf(stderr, "Usage: recover file\n");
        return 1;
    }

    // address of argv[1] stored in infile
    char *infile = argv[1];

    // file pointer to open infile for reading
    FILE *card = fopen(infile, "r");

    // check file opens/exists
    if (card == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 2;
    }

    int j = 0;
    int counter = 0;

    // alocate memory for block
    int block[512];

    while(j < 1)
    {
        // read block from card and check for end of file
        fread(block, 1, 512, card);
        if (fread(block, 1, 512, card) == 512)
        {
            // check for jpg
            if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && block[3] >= 0xe0 && block[3] <= 0xef)
            {
                // store file name
                char jpegname[7];
                sprintf(jpegname, "%03i.jpg", counter);

                //open file (jpegfile is pointer to it)
                FILE* jpegfile = fopen(jpegname, "w");

                // write block from card to file
                fwrite(jpegfile, 1, 512, card);

                int i = 0;

                // for rest of jpg, keep writing blocks to image
                while(i < 1)
                {
                    fread(block, 1, 512, card);

                    // check for end of file
                    if (fread(block, 1, 512, card) == 512)
                    {
                        if (block[0] != 0xff && block[1] != 0xd8 && block[2] != 0xff && block[3] < 0xe0 && block[3] > 0xef)
                        {
                            fwrite(jpegfile, 1, 512, card);
                        }

                        // until another jpg starts
                        else
                        {
                            fclose(jpegfile);
                            counter++;

                            // stop loop when new jpeg starts
                            i++;

                            // go to start of block ready for main loop
                            fseek(card, 512, SEEK_CUR);
                        }
                    }
                    // stop loop if end of file reached
                    else
                    {
                        fclose(jpegfile);
                        i++;
                    }
                }
            }
        }
        // stop main loop if end of file reached
        else
        {
            // avoid memory leak
            free(block);
            j++;
        }
    }
}

1 Answer 1

0
        // avoid memory leak
        free(block);

The code never malloc's any memory, so why is the code trying to free anything?

There are other issues in this code, but that's for another question. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thanks. Yeah that was because I did originally malloc the block bit but then forgot to remove the free().
    – ab26221
    Jul 21, 2018 at 23:27

You must log in to answer this question.

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