Heap corruption while freeing arrays in C

I have text implemented in 2d arrays in c. There is some logic to free them in some cases. So this clearing does not work. text_from_file[i] and local_text[i] (the second one does not exist in that context) are both formatted with a high chance. But it should not happen. This is why heap corruption occurs.


int load_from_file() {

    FILE* file = fopen(file_name, "r");
    if (file == NULL) {
        printf("Failed to open filen");
        return -1;
    }

    if (text_from_file != NULL) {
        printf("File already loadedn");
        return -1;
    }
    text_from_file = calloc(text_from_file_rows, sizeof(char*));
    // added checking for correct allocation
    if (text_from_file == NULL) {
        printf("Failed to allocate memoryn");
        fclose(file);
        return -1;
    }

    for (int i = 0; i < text_from_file_rows; i++) {
        text_from_file[i] = calloc(text_from_file_chars, sizeof(char));
        // same checking here
        if (text_from_file[i] == NULL) {
            printf("Failed to allocate memoryn");
            fclose(file);
            return -1;
        }
    }

    int i = 0;
    int length = 0;
    while (fgets(text_from_file[i] + length, text_from_file_chars - length, file) != NULL) {

        length = strlen(text_from_file[i]);
        if (length == (text_from_file_chars - 1)) {
            text_from_file_chars *= 2;
            text_from_file[i] = realloc(text_from_file[i], text_from_file_chars);
            if (text_from_file[i] == NULL) {
                printf("Failed to reallocate memoryn");
                fclose(file);
                return -1;
            }
        }


        if (text_from_file[i][strlen(text_from_file[i]) - 1] == 'n') {
            text_from_file[i][strlen(text_from_file[i]) - 1] = '';
            length = 0;
            i++;
        }


        if (i >= text_from_file_rows) {
            text_from_file_rows *= 2;
            char** temp = realloc(text_from_file, text_from_file_rows * sizeof(char*));
            if (temp == NULL) {
                printf("Failed to reallocate memoryn");
                fclose(file);
                return -1;
            }
            text_from_file = temp;
            for (int j = i; j < text_from_file_rows; j++) {
                text_from_file[j] = calloc(text_from_file_chars, sizeof(char));
                if (text_from_file[j] == NULL) {
                    printf("Failed to allocate memoryn");
                    fclose(file);
                    return -1;
                }
            }
        }
        
    }


    text_from_file[i + 1] = NULL;
    fclose(file);

    return 0;
}
int concatenate_text() {
    // concatenate text from file with local text
    if (local_text == NULL) {
        text_concatenator();
        printf("Text concatenated with local successfully.n");
        return 0;
    }
    else
    {
        printf("Local text is not empty. Cannot concatenate textsn");
        if (remove_text()) {
            concatenate_text();
        }
        else
        {
            return 1;
        }
    }
}
int text_concatenator() {
    if (text_from_file == NULL) {
        return 0;
    }
    if (local_text_rows < text_from_file_rows) {
        local_text_rows = text_from_file_rows;
    }
    if (local_text_chars < text_from_file_chars)
    {
        local_text_chars = text_from_file_chars;
    }
    local_text = calloc(local_text_rows, sizeof(char*));
    for (int i = 0; i < text_from_file_rows; i++) {
        local_text[i] = calloc(local_text_chars, sizeof(char));

        if (text_from_file[i] != NULL) {
            strcpy(local_text[i], text_from_file[i]);
            local_text[i][strlen(text_from_file[i])] = '';
        }
    }
    return 0;
}

and the next given file have this exception:

int remove_text() {
    printf("Do you want to save local text to file (1) or clear text from file only (0)?: ");
    char answer;
    scanf(" %c", &answer);

    switch (answer) {
    case '1':
        save_to_file();
        printf("Local text has been saved to file.n");
        return 1;

    case '0':
        for (int i = 0; i < text_from_file_rows; i++) {
            if (text_from_file[i] != NULL) {
                free(text_from_file[i]);
                text_from_file[i] = NULL;
            }
        }
        free(text_from_file);
        text_from_file = NULL;

        printf("Local text has been cleared.n");
        return 0;

    default:
        printf("Local text and text from file saved separatelyn");
        printf("You will need to previously save local text to filen");
        return 0;
    }
}

it does occur on line

if (text_from_file[i] != NULL) {
                free(text_from_file[i]);
                text_from_file[i] = NULL;
            }

there is debug console output at this program state:

  • A breakpoint instruction (__debugbreak() statement or a similar call) was executed in Assignment1.exe.
    debug console output

I was trying to debug in a lot of ways but nothing changes.
The huge problem is that one time everything can execute correctly, but in most cases it crashes. It seems like at one time processes synchronized correctly but at another, they both modified in the wrong way. But I’m not operating with any processes in this project

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật