main函数最后一行报Segmentation fault: 11,什么原因?

jueljust 2017-08-29 11:03:33

int main(int argc, char* const argv[]) {
// opt parse
struct ColorHistOptions colorHistOptions = get_option(argc , argv);
std::uint16_t* hist = new std::uint16_t[hash_length]();
switch(colorHistOptions.command){
case MAKELIB:{//lib
std::ofstream binfile_w("./hist.bin", std::ios::out|std::ios::binary);
std::ofstream idxfile_w("./hist.idx", std::ios::out|std::ios::binary);
glob_t buf;
const char * glob_pattern = strcat(colorHistOptions.lib, "/*");
glob(glob_pattern, GLOB_NOSORT, NULL, &buf);
uint i=0;
for(; i < buf.gl_pathc; i++){
try{
idxfile_w << buf.gl_pathv[i] << std::endl;
image_color_hist(buf.gl_pathv[i], size, hist);
binfile_w.write((char *) hist, hash_length*2);
}
catch(...){
printf("buf.gl_pathv[%d]= %s ERROR \n", i, (buf.gl_pathv[i]));
}
}
printf("end glob\n");
globfree(&buf);
printf("free glob\n");
binfile_w.close();
idxfile_w.close();
printf("file close\n");
break;
}
case COSDIST:{//cos
printf("cos\n");
double cos = 0.0;
double score = 0.0;
std::uint16_t* sample_hist = new std::uint16_t[hash_length]();
image_color_hist(colorHistOptions.sample, size, sample_hist);
struct stat statbuf;
if (stat("./hist.bin", &statbuf) == -1) {
/* check the value of errno */
}
int size = (int) statbuf.st_size;
std::uint16_t value = 0;
std::ifstream binfile_r("./hist.bin", std::ios::in|std::ios::binary);
char * buffer = new char[2];
for (int j=0; j<(size/hash_length/2); j++){
for (char i=0; i<hash_length; i++){
binfile_r.read(buffer, 2);
value = char2short(buffer);
hist[i] = value;
}
cos = cosDistance(sample_hist, hist);
score = std::max(score, cos);
}
binfile_r.close();
printf("%0.2f", score);
/*
if (score >= 0.9){//append lib
std::ofstream binfile_w("./hist.bin", std::ios::out|std::ios::binary|std::ios::app);
binfile_w.write((char *) sample_hist, hash_length*2);
binfile_w.close();
}
*/
break;
}
case HELP:
default:{
printf("help\n");
std::printf("cosWithLib -c buildLib -l <lib>\n");
std::printf("cosWithLib -c cosDist -s <sample>\n");
std::printf("cosWithLib -c help\n");
break;
}
}
printf("end switch\n");
return 0;
}


执行之后输出


end glob
free glob
file close
end switch
Segmentation fault: 11


环境:mac

我不能理解的是在最后的return 0上出错,但是我也没找到有什么资源没释放,不知道这个Segmentation fault怎么来的,怎样可以消除
...全文
709 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jueljust 2017-10-17
  • 打赏
  • 举报
回复
引用 12 楼 Chen_QiDian 的回复:
[quote=引用 11 楼 ccssddnn218 的回复:] [quote=引用 10 楼 Chen_QiDian 的回复:] [quote=引用 2 楼 jueljust 的回复:] 没有,这几个标准数据不是会自动释放吗?还需要显示null吗?
new出来的内存,自动释放怎么讲?[/quote] 进程结束,系统自动回收。[/quote] 这个解释很强势,无言以对。[/quote]
引用 12 楼 Chen_QiDian 的回复:
[quote=引用 11 楼 ccssddnn218 的回复:] [quote=引用 10 楼 Chen_QiDian 的回复:] [quote=引用 2 楼 jueljust 的回复:] 没有,这几个标准数据不是会自动释放吗?还需要显示null吗?
new出来的内存,自动释放怎么讲?[/quote] 进程结束,系统自动回收。[/quote] 这个解释很强势,无言以对。[/quote] new和delete配对,现在懂了
Chen_QiDian 2017-08-31
  • 打赏
  • 举报
回复
引用 11 楼 ccssddnn218 的回复:
[quote=引用 10 楼 Chen_QiDian 的回复:] [quote=引用 2 楼 jueljust 的回复:] 没有,这几个标准数据不是会自动释放吗?还需要显示null吗?
new出来的内存,自动释放怎么讲?[/quote] 进程结束,系统自动回收。[/quote] 这个解释很强势,无言以对。
Really_want 2017-08-30
  • 打赏
  • 举报
回复
引用 10 楼 Chen_QiDian 的回复:
[quote=引用 2 楼 jueljust 的回复:] 没有,这几个标准数据不是会自动释放吗?还需要显示null吗?
new出来的内存,自动释放怎么讲?[/quote] 进程结束,系统自动回收。
Chen_QiDian 2017-08-30
  • 打赏
  • 举报
回复
引用 2 楼 jueljust 的回复:
没有,这几个标准数据不是会自动释放吗?还需要显示null吗?
new出来的内存,自动释放怎么讲?
上山老人 2017-08-30
  • 打赏
  • 举报
回复
std::uint16_t* hist = new std::uint16_t[hash_length]();
这个没释放
jueljust 2017-08-29
  • 打赏
  • 举报
回复
比较见鬼的是这个命令也不是每次都有问题 编译之后连续运行三次,三次结果不同

gucci@MacBookAir:colorHist $ bash build.sh 
main.cpp:38:19: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
    char * sample = (char * ) "";
                  ^
main.cpp:39:16: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
    char * lib = (char * )"";
               ^
main.cpp:40:17: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
    int command = HELP;
                ^
main.cpp:41:15: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
    bool help = true;
              ^
4 warnings generated.
gucci@MacBookAir:colorHist $ ./colorHist 
end glob
free glob
file close
end switch
gucci@MacBookAir:colorHist $ echo $?
0
gucci@MacBookAir:colorHist $ ./colorHist 
end glob
free glob
file close
end switch
Segmentation fault: 11
gucci@MacBookAir:colorHist $ ./colorHist 
colorHist(17823,0x7fff9eb853c0) malloc: *** error for object 0x7f912d6040e8: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
gucci@MacBookAir:colorHist $ 
jueljust 2017-08-29
  • 打赏
  • 举报
回复
通过逐行注释,我确定产生问题的语句是

image_color_hist(buf.gl_pathv[i], size, hist);
把这行注释了就不会出段错误,对应的函数为

void image_color_hist(char * file_path, uchar const size, std::uint16_t* hist){
    cv::Mat image = cv::imread(file_path);
    cv::resize(image, image, cv::Size(size, size), cv::INTER_CUBIC);
    //cv::imwrite(strcat(file_path, ".cpp.png"), image);
    int channels = image.channels();
    if (channels == 1){
        cv::cvtColor(image, image, cv::COLOR_GRAY2BGR);
    }
    //cv::divide(image, range, image);//not correct for unkown reason
    //image = image/range; //not correct for unkown reason
    uchar b,g,r;
    for(uchar x=0; x<size; x++){
        for(uchar y=0; y<size; y++){
            b = image.at<cv::Vec3b>(x,y)[0]/range;
            g = image.at<cv::Vec3b>(x,y)[1]/range;
            r = image.at<cv::Vec3b>(x,y)[2]/range;
            //printf("%d %d %d %d %d %d\n" , x, y, b, g, r, r*base*base+g*base+b);
            hist[r*base*base+g*base+b] += 1;
        }
    }
}
jueljust 2017-08-29
  • 打赏
  • 举报
回复
没有,这几个标准数据不是会自动释放吗?还需要显示null吗?
Chen_QiDian 2017-08-29
  • 打赏
  • 举报
回复
std::uint16_t* hist = new std::uint16_t[hash_length](); std::uint16_t* sample_hist = new std::uint16_t[hash_length](); char * buffer = new char[2]; 上面这几处是什么时候释放的呢?
paschen 版主 2017-08-29
  • 打赏
  • 举报
回复
估计越界等原因覆盖了别的东西,函数结束时会检查,所以就在return 0那里了
赵4老师 2017-08-29
  • 打赏
  • 举报
回复
如果进程意外退出不产生core文件,参考“ulimit -c core文件最大块大小”命令
jueljust 2017-08-29
  • 打赏
  • 举报
回复
@赵4老师 但是系统没有生成core文件,不知道哪里的设置不对
赵4老师 2017-08-29
  • 打赏
  • 举报
回复
进程意外退出会在当前目录下产生‘core’文件或形如‘core.数字’的文件比如‘core.1234’ 使用命令 gdb 运行程序名 core或core.数字 进入gdb然后使用bt命令 可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。 如果进程意外退出不产生core文件,参考“ulimit -c core文件最大块大小”命令

65,183

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧