账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    centos7上毫无规律的出现错误“Segmentation fault”?
    34
    0
    int readLogDataFromRedis(char readKey[50], struct logObj *logP)
    {
        printf("readkey=%s\n",readKey);
        int DataCount = 0;
        unsigned int j,n;
    
        struct timeval timeout = { 1, 500000 }; // 1.5 seconds
        redisContext* conn = redisConnectWithTimeout(redisServer, redisPort, timeout);
        if (conn->err) {
            printf("connection error:%s\n", conn->errstr);
            redisFree(conn);
            return 0;
        }
        redisReply* reply = (redisReply*)redisCommand(conn, readDB);
        if (NULL == reply) {
            redisFree(conn);
            return 0;
        }
        if (!(reply->type == REDIS_REPLY_STATUS && strcasecmp(reply->str, "OK") == 0)) {
            printf("Failed to execute command[%s].\n", readDB);
            freeReplyObject(reply);
            redisFree(conn);
            return 0;
        }
        freeReplyObject(reply);
    
        char cmd[50] = {0};
        sprintf(cmd, "LRANGE %s 0 -1", readKey);
        reply = (redisReply*)redisCommand(conn, cmd);
        if (reply->type != REDIS_REPLY_ARRAY) {
            printf("Failed to execute command[%s].\n",cmd);
            freeReplyObject(reply);
            redisFree(conn);
            return 0;
        }
    
        //string str;
        struct tempChar *tChar, *tCharBuf;
        tChar = (struct tempChar*)malloc(sizeof(struct tempChar)*logCount);
        memset(tChar, 0, sizeof(struct tempChar)*logCount);
        tCharBuf = tChar;
        
        for (j = 0; j < reply->elements; ++j) {
            redisReply* childReply = reply->element[j];
            if (childReply->type == REDIS_REPLY_STRING)
            {    
                char *x = childReply->str;
                
                //拼接原始数据,压缩后导出
                strcat(tCharBuf[j].str, x);
                strcat(tCharBuf[j].str, "\r");
    
                int commasIndex = 0, commaeIndex = 0;
                int col = 0;
                strcat(x, "^");
                for (n = 0; x[n] != '\0'; ++n)
                {
                    if ('^' == x[n] || '\r' == x[n])
                    {
                        commaeIndex = n;
                        if (col == 0) strcpy(logP[DataCount].ev, subStr(x, commasIndex, commaeIndex));
                        if (col == 1) strcpy(logP[DataCount].cy, subStr(x, commasIndex, commaeIndex));
                        if (col == 2) logP[DataCount].v = atof(subStr(x, commasIndex, commaeIndex));
                        if (col == 3) strcpy(logP[DataCount].tp, subStr(x, commasIndex, commaeIndex));
                        if (col == 4) strcpy(logP[DataCount].ids, subStr(x, commasIndex, commaeIndex));
                        if (col == 5) logP[DataCount].cnt = atof(subStr(x, commasIndex, commaeIndex));
                        if (col == 6) strcpy(logP[DataCount].cg, subStr(x, commasIndex, commaeIndex));
                        if (col == 7) logP[DataCount].dt_fs = atof(subStr(x, commasIndex, commaeIndex));
                        if (col == 8) strcpy(logP[DataCount].wid, subStr(x, commasIndex, commaeIndex));
                        if (col == 9) strcpy(logP[DataCount].lp, subStr(x, commasIndex, commaeIndex));
                        if (col == 10) strcpy(logP[DataCount].ref, subStr(x, commasIndex, commaeIndex));
                        if (col == 11) logP[DataCount].sw = atoi(subStr(x, commasIndex, commaeIndex));
                        if (col == 12) logP[DataCount].sh = atoi(subStr(x, commasIndex, commaeIndex));
                        if (col == 13) strcpy(logP[DataCount].aid, subStr(x, commasIndex, commaeIndex));
                        if (col == 14) strcpy(logP[DataCount].agent, subStr(x, commasIndex, commaeIndex));
                        if (col == 15) strcpy(logP[DataCount].ga, subStr(x, commasIndex, commaeIndex));
                        if (col == 16) strcpy(logP[DataCount].sid, subStr(x, commasIndex, commaeIndex));
                        if (col == 17) strcpy(logP[DataCount].lid, subStr(x, commasIndex, commaeIndex));
                        if (col == 18) strcpy(logP[DataCount].gid, subStr(x, commasIndex, commaeIndex));
                        if (col == 19) strcpy(logP[DataCount].uid, subStr(x, commasIndex, commaeIndex));
                        if (col == 20) strcpy(logP[DataCount].gsid, subStr(x, commasIndex, commaeIndex));
                        if (col == 21) strcpy(logP[DataCount].ip, subStr(x, commasIndex, commaeIndex));
                        if (col == 22) logP[DataCount].dt = atof(subStr(x, commasIndex, commaeIndex));
                        commasIndex = n + 1;
                        col++;
                    }
                }
                if (strlen(logP[DataCount].aid)>0)
                    DataCount++;
                col = 0; commasIndex = 0; commaeIndex = 0;
            }
                
        }
        freeReplyObject(reply);
    
        //转换
        char *src, *dst;
        src = (char*)malloc(singleLen * logCount);
        memset(src, 0, singleLen * logCount);
        charAppendToStr(tCharBuf, logCount, src);
        *(src - 1) = '\0';
    
        //压缩
        char fileName[100] = {0};
        sprintf(fileName, "%s/%s", filePath, readKey);
        FILE *ofile = fopen(fileName, "wb");
        qlz_state_compress *state_compress = (qlz_state_compress *)malloc(sizeof(qlz_state_compress));
        int len, len2;
        len = strlen(src);
        dst = (char*)malloc(len + 400);
    
        //写压缩结果
        len2 = qlz_compress(src, dst, len, state_compress);
        fwrite(dst, len2, 1, ofile);
        fclose(ofile);
        printf("len2=%d,compress success!",len2);
    
        
        reply = (redisReply*)redisCommand(conn, writeDB);
        if (!(reply->type == REDIS_REPLY_STATUS && strcasecmp(reply->str, "OK") == 0)) {
            printf("Failed to execute command[%s].\n", writeDB);
            freeReplyObject(reply);
            redisFree(conn);
            return 0;
        }
        freeReplyObject(reply);
        
        reply = (redisReply*)redisCommand(conn, "RPUSH toBeUpload_compress %s", readKey);
        if (!(reply->type == REDIS_REPLY_INTEGER)) {
            printf("Failed to execute command[%s].\n", "RPUSH toBeUpload_compress");
            freeReplyObject(reply);
            redisFree(conn);
            return 0;
        }
        freeReplyObject(reply);
        
        ////删除当前key的数据
        //reply = (redisReply*)redisCommand(conn, "del %s", readKey);
        //freeReplyObject(reply);
        
        if(src!=NULL){
            free(src);
            src=NULL;
        }
        if(dst!=NULL){
            free(dst);
            dst=NULL;
        }
        if(tChar!=NULL){
            free(tChar);
            tChar=NULL;
        }
        redisFree(conn);
        return DataCount;
    }

    上面这段代码之前是在windows上写的,运行起来没有任何问题,现在环境改到centos7上面,重新编译过了,运行起来各种问题,经常出现 “double free”或者“Segmentation fault”段错误,仔细检查了这段代码,没发现啥问题,而且“Segmentation fault”这个错误出现的一点规律都没有,同样的数据跑几次可能其中一两次会出现问题,并且出现这个错误问题的位置还不一样,有没有大神解解惑的?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • "Segmentation fault"通常是由于程序试图访问一个已释放的内存区域或者在程序运行过程中发生了其他错误导致的。在 CentOS 7 上出现这种错误,可能的原因包括以下几种:

      1. 未释放的内存资源:如果程序在运行过程中使用了未释放的内存资源,就可能会导致 "Segmentation fault"。例如,如果程序在读取一个未关闭的文件时出现了 "Segmentation fault",那么这个文件可能已经被其他程序打开并且读取了。

      2. 程序错误:有时候,程序的错误也可能会导致 "Segmentation fault"。例如,如果程序试图访问一个已被释放的内存区域,但是程序本身也存在错误,那么这个错误可能会导致 "Segmentation fault"。

      3. 硬件故障:如果硬件故障,如内存条损坏,也可能导致 "Segmentation fault"。

      解决这个问题的步骤通常包括:

      1. 检查程序是否有使用未释放的内存资源。这通常可以通过使用内存监视工具来完成。

      2. 检查程序是否出现了错误。这通常需要通过调试工具来完成,例如使用 debug 命令或者编译器的调试选项。

      3. 如果上述步骤都无法解决问题,那么可能需要检查硬件是否存在问题。这通常需要专业的硬件工程师进行检查。

      另外,也可以尝试以下一些解决步骤:

      1. 使用内存监视工具检查程序是否有使用未释放的内存资源。

      2. 使用调试工具(如 gdb)来检查程序是否出现了错误。

      3. 如果程序没有错误,但是仍然无法解决问题,那么可能需要升级硬件或者重新安装操作系统。

      在 CentOS 7 上出现 "Segmentation fault",需要根据具体的情况进行分析和解决。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部