cv::HOGDescriptor compute()错误的参数设置导致free(): corrupted unsorted chunks错误

最近在调优DeepSORT代码时是遇到一个奇怪的错误,将tracker里特征数据改成由opencv HOG数据类型后,代码总是崩溃,使用GDB调试发现崩溃发生在一句看似根本不可能崩溃的语句上:

free(): corrupted unsorted chunks
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/aarch64-linux-gnu/libthread_db.so.1".
Core was generated by `./bright'.
Program terminated with signal SIGABRT, Aborted.
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
[Current thread is 1 (Thread 0x7f2cdba8a0 (LWP 7121))]
(gdb) bt
#0  0x0000007f8f3074f8 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1  0x0000007f8f3088d4 in __GI_abort () at abort.c:79
#2  0x0000007f8f34168c in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7f8f4026f8 "%s\n") at ../sysdeps/posix/libc_fatal.c:181
#3  0x0000007f8f347a04 in malloc_printerr (str=str@entry=0x7f8f3fe448 "free(): corrupted unsorted chunks") at malloc.c:5342
#4  0x0000007f8f349874 in _int_free (av=0x7ef8000020, p=0x7ef85c51a0, have_lock=<optimized out>) at malloc.c:4348
#5  0x0000007f34ac081c in __gnu_cxx::new_allocator<float>::deallocate(float*, unsigned long) (this=0x7f2c9da000, __p=0x7ef85c9360) at /usr/include/c++/7/ext/new_allocator.h:125
#6  0x0000007f34ac081c in std::allocator_traits<std::allocator<float> >::deallocate(std::allocator<float>&, float*, unsigned long) (__a=..., __n=<optimized out>, __p=0x7ef85c9360)
    at /usr/include/c++/7/bits/alloc_traits.h:462
#7  0x0000007f34ac081c in std::_Vector_base<float, std::allocator<float> >::_M_deallocate(float*, unsigned long) (this=0x7f2c9da000, __n=<optimized out>, __p=0x7ef85c9360)
    at /usr/include/c++/7/bits/stl_vector.h:180
#8  0x0000007f34ac081c in std::_Vector_base<float, std::allocator<float> >::~_Vector_base() (this=0x7f2c9da000, __in_chrg=<optimized out>) at /usr/include/c++/7/bits/stl_vector.h:162
#9  0x0000007f34ac081c in std::vector<float, std::allocator<float> >::~vector() (this=0x7f2c9da000, __in_chrg=<optimized out>) at /usr/include/c++/7/bits/stl_vector.h:435
#10 0x0000007f34ac081c in Deep_sort::get_hog_feature(cv::Mat) (this=this@entry=0x5598b38890, img=...) at deepsort.cpp:66
#11 0x0000007f34ac1c10 in Deep_sort::update(std::vector<DS_DetectObject, std::allocator<DS_DetectObject> >, cv::Mat, cv_image*, int) (this=0x5598b38890,
    this@entry=0x7ef801cac0, detect_objects=std::vector of length 1, capacity 1 = {...}, img=..., feature_map_ptr=feature_map_ptr@entry=0x7ef801cac0, max_que_class=<optimized out>)
    at deepsort.cpp:229
#12 0x0000007f34abc40c in ObjectDetectTrack::Run(ObjDetectTrackInput const&, ObjDetectTrackOutput&, std::vector<DS_DetectObject, std::allocator<DS_DetectObject> >&) (this=<optimized out>, input=..., output=..., odm_det_objs=std::vector of length 1, capacity 1 = {...}) at ObjectDetectTrack.cpp:145
#13 0x0000007f34a99584 in KFTrackerObjects(cv::Mat&, _NvMOTObjToTrack*, unsigned int, _NvMOTTrackedObjBatch*, unsigned long) (frame=..., det_objs_list=0x7ef8000bd0, filledNum=4166773116, pTrackedObjectsBatch=pTrackedObjectsBatch@entry=0x7f2cdb9f00, streamInd=streamInd@entry=0) at gstnvdscustomtracker.cpp:412
#14 0x0000007f34a9a134 in NvMOT_Process(NvMOTContextHandle, NvMOTProcessParams*, NvMOTTrackedObjBatch*) (contextHandle=<optimized out>, pParams=<optimized out>, pTrackedObjectsBatch=0x7f2cdb9f00) at gstnvdscustomtracker.cpp:176
#15 0x0000007f6787beac in  () at /usr/lib/aarch64-linux-gnu/gstreamer-1.0/deepstream/libnvdsgst_tracker.so
#16 0x0000007f8f50de94 in  () at /usr/lib/aarch64-linux-gnu/libstdc++.so.6
#17 0x0000007f8f5ed088 in start_thread (arg=0x7fdb4d0e2f) at pthread_create.c:463
#18 0x0000007f8f3a4ffc in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:78

而get_hot_feature()里对应的代码是:

cv::Mat resize_img;
cv::resize(img, resize_img, cv::Size(30,30),(0, 0), (0, 0), cv::INTER_LINEAR);
cv::HOGDescriptor hog = cv::HOGDescriptor(cv::Size(20, 20), cv::Size(10, 10), cv::Size(5, 5), cv::Size(5, 5), 9);
std::vector<float> descriptors;
hog.compute(resize_img, descriptors, cv::Size(20, 20), cv::Size(0, 0));
...

 发生崩溃的代码第66行就是

    std::vector<float> descriptors;

这句怎么看都没毛病的语句怎么会导致崩溃呢,开始看错误信息“free(): corrupted unsorted chunks”感觉是哪里内存释放混乱了,导致这句不能正常执行,于是把调用堆栈前面的相关代码都查了个遍也没发现什么写得不对或者多次释放内存的地方,感觉错误应该不是前面代码引起的,于是在descriptors前面加上类似这样的定义也没见崩溃在新加的同样的定义上,那谁会造成descriptors崩溃呢,只要往下看调用的地方,突然发现

    hog.compute(resize_img, descriptors, cv::Size(20, 20), cv::Size(0, 0));

这句话是写得有问题的,因为前面

    cv::resize(img, resize_img, cv::Size(30,30),(0, 0), (0, 0), cv::INTER_LINEAR);

把图片缩放成30x30尺寸了,那么hog.compute()调用时使用(20,20)的stride加0 padiing,显然这里参数设置是不正确的!于是试着把(20,20)改小成(10,10)或者(5,5),果然程序能正常运行了!

感觉真是灯下黑,不过这种情况也比较特殊,后面的hog.compute()的不正确调用,导致程序崩溃,报错位置却指向descriptor定义的地方,这个可能跟编译器的内部处理方式有关吧,一下还没想明白,先记下备忘。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Arnold-FY-Chen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值