c程序中的双重自由或腐败(out)段错误

时间:2017-12-01 14:42:59

标签: c segmentation-fault

当下面的帖子退出时,我收到错误double free or corruption (out)。我认为这与我使用malloc()并最后调用free()有关。我无法理解我在做什么有什么问题。

有人会知道为什么会这样或者能指出我正确的方向吗?

 static void *serial_logging_dut1_thread(prv_thread_params_t *threadParams) {

    ssize_t rdlen = 0;
    int res_port, res_file, res_close;
    char ibuf[1024];


    char *filename_ending = "_dut1_serial_log.txt";
    char *filename_with_extension;
    prv_instance_t *targetP = threadParams->targetP;

    /*append filename ending "_dut1_serial_log.txt" to the power data filename supplied in the experiment script*/
    filename_with_extension = malloc(strlen(targetP->output_filename)+1+20);
    strcpy(filename_with_extension, targetP->output_filename); /* copy name into the new var */
    strcat(filename_with_extension, filename_ending); /* add the extension */

    LOG("Started thread for serial logging from DUT 1\r\n");

    /*Open log file and write to it from /dev/USB1*/
    res_file = create_open_log_file(filename_with_extension);

    if (res_file) {
        LOG("Error creating the serial log file");
        serial_logging_res = -1;
        pthread_exit(&serial_logging_res);
    }

    res_port = log_dut_serial_data(joule_serial_port);

    if (res_port) {
        LOG_ARG("Error opening the serial port to DUT 1, check the cable %s\n",joule_serial_port);
        serial_logging_res = -2;
        pthread_exit(&serial_logging_res);
    }

    memset(&ibuf[0], 0, sizeof(ibuf)); /*clear the buffer*/

    /*while DUT is turned on and serial port was opened*/
    while (serial_logging_running == 1 && res_port == 0) {
     //do stuff
    }

    /*close file*/
    fclose(log_file);
    res_close = close_serial_port_joule();


    if (res_close) {
            LOG("Error closing the serial port to DUT 1");
        }

    free(filename_with_extension);

    serial_logging_res = 0;
    pthread_exit(&serial_logging_res);

}

包括create_open_log_file()。我不喜欢'认为这是原因吗?

int create_open_log_file(char dut1_serial_log_filename[]) {

char filename[256];
sprintf(filename,"/var/log/serial_log/%s", dut1_serial_log_filename);
log_file = fopen(filename,"a");

if (log_file == NULL) {
    LOG("Error creating log file\n");
    return -1;
}
LOG_ARG("Successfully opened the serial logging file %s\n", dut1_serial_log_filename);
return 0;

}

0 个答案:

没有答案