EXC_BAD_ACCESS (heap buffer overflow) when using .at function [closed]
hi all
i am using OpenCV 3.4.0 with C++ on Mac OS X. I am trying to access a matrice on a Mat instance like the following:
cv::Mat overlay2 = cv::imread(getAssetsPath() + "overlay.png");
for (int i = 0; i < overlay2.rows; ++i) {
for (int j = 0; j < overlay2.cols; ++j) {
std::cout << i << "x" << j << std::endl;
auto vec = overlay2.at<cv::Vec4f>(i, j);
std::cout << vec << std::endl;
}
}
this is causing me a heap buffer overflow error"
READ of size 4 at 0x00010bed0800 thread T0
#0 0x10003a203 in cv::Matx<float, 4, 1>::Matx(float const*) matx.hpp:665
#1 0x10003a08b in cv::Vec<float, 4>::Vec(cv::Vec<float, 4> const&) matx.hpp:1030
#2 0x10002aa22 in cv::Vec<float, 4>::Vec(cv::Vec<float, 4> const&) matx.hpp:1030
#3 0x100028c6a in OpenCVImage::appendOverlay(OpenCVImage) Image.cpp:32
#4 0x100067434 in testOverlay() main.cpp:45
#5 0x10007762a in main main.cpp:136
#6 0x7fff6b15a114 in start (libdyld.dylib:x86_64+0x1114)
0x00010bed0800 is located 0 bytes to the right of 7077888-byte region [0x00010b810800,0x00010bed0800)
allocated by thread T0 here:
#0 0x1001da830 in wrap_posix_memalign (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x59830)
#1 0x101388d20 in cv::fastMalloc(unsigned long) (libopencv_core.3.4.dylib:x86_64+0x2d20)
#2 0x1014bbc56 in cv::StdMatAllocator::allocate(int, int const*, int, void*, unsigned long*, int, cv::UMatUsageFlags) const (libopencv_core.3.4.dylib:x86_64+0x135c56)
#3 0x10148fece in cv::Mat::create(int, int const*, int) (libopencv_core.3.4.dylib:x86_64+0x109ece)
#4 0x102135c5c in cv::imread_(cv::String const&, int, int, cv::Mat*) (libopencv_imgcodecs.3.4.dylib:x86_64+0x4c5c)
#5 0x10213593f in cv::imread(cv::String const&, int) (libopencv_imgcodecs.3.4.dylib:x86_64+0x493f)
#6 0x100028592 in OpenCVImage::appendOverlay(OpenCVImage) Image.cpp:27
#7 0x100067434 in testOverlay() main.cpp:45
#8 0x10007762a in main main.cpp:136
#9 0x7fff6b15a114 in start (libdyld.dylib:x86_64+0x1114)
SUMMARY: AddressSanitizer: heap-buffer-overflow matx.hpp:665 in cv::Matx<float, 4, 1>::Matx(float const*)
Shadow bytes around the buggy address:
0x1000217da0b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1000217da0c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1000217da0d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1000217da0e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1000217da0f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x1000217da100:[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x1000217da110: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x1000217da120: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x1000217da130: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x1000217da140: fa fa fa fa fa fa fa fa fa fa fa fa fa fa ...
it is not Vec4f but Vec4b if overlay2.channels() is equal to 4
@LBerger it actually did not make any difference. i used Vec4f because of 4 channels and to get float values. would my image somehow be corrupt?
thank you very much @LBerger. i don't know why but cv::imread was setting my png channels to 1 rather than 4. hence i was getting those errors.