Valgrind 大小为 4 的无效读取(OpenCV)
Posted
技术标签:
【中文标题】Valgrind 大小为 4 的无效读取(OpenCV)【英文标题】:Valgrind Invalid read of size 4 (OpenCV) 【发布时间】:2017-02-20 23:17:24 【问题描述】:这是我的代码:
template <typename TValue>
std::vector<cv::Point2i> GetPixelsWithValue(const cv::Mat& image, const TValue& value)
std::vector<cv::Point2i> pixels;
cv::Size imageSize = image.size();
for(int i = 0; i < imageSize.height; ++i)
for(int j = 0; j < imageSize.width; ++j)
if(image.at<TValue>(i, j) == value)
cv::Point2i pixel(j, i);
pixels.push_back(pixel);
return pixels;
Valgrind 给出这个错误:
==10655== Conditional jump or move depends on uninitialised value(s)
==10655== at 0x4E88517: GetPixelsWithValue<int> (OpenCVExtensions.hpp:18)
==10655== by 0x4E88517: CountPixelsWithValue<int> (OpenCVExtensions.hpp:31)
...
==10655== Uninitialised value was created by a heap allocation
==10655== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10655== by 0x117138EB: cv::fastMalloc(unsigned long) (alloc.cpp:64)
==10655== by 0x1184F795: cv::StdMatAllocator::allocate(int, int const*, int, void*, unsigned long*, int, cv::UMatUsageFlags) const (matrix.cpp:192)
==10655== by 0x118507BE: cv::Mat::create(int, int const*, int) (matrix.cpp:426)
==10655== by 0x4E9BA31: create (mat.inl.hpp:684)
==10655== by 0x4E9BA31: Mat (mat.inl.hpp:351)
==10655== by 0x4E9BA31: cv::Mat
....
==10655== Invalid read of size 4
==10655== at 0x4E88510: GetPixelsWithValue<int> (OpenCVExtensions.hpp:18)
....
==10655== Address 0x23201c92 is 127,346 bytes inside a block of size 127,347 alloc'd
==10655== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10655== by 0x117138EB: cv::fastMalloc(unsigned long) (alloc.cpp:64)
==10655== by 0x1184F795: cv::StdMatAllocator::allocate(int, int const*, int, void*, unsigned long*, int, cv::UMatUsageFlags) const (matrix.cpp:192)
==10655== by 0x118507BE: cv::Mat::create(int, int const*, int) (matrix.cpp:426)
==10655== by 0x4E9BA31: create (mat.inl.hpp:684)
==10655== by 0x4E9BA31: Mat (mat.inl.hpp:351)
我发现了这些以及更多:
Why am I getting memory errors when accessing this matrix in OpenCV?
Invalid write size of 4 with matrix class (using valgrind)
但是我不明白为什么会出现错误。这可能是来自 OpenCV 的内部错误吗?
【问题讨论】:
大概你传入的图片一开始就是假的吧? 【参考方案1】:发现问题。正如@Kerrek SB 指出的那样,图像是问题所在。下面是一个更完整的例子。 uninitialized value 错误似乎是由于尝试使用未初始化的矩阵造成的。我也可能有导致 invalid read 错误的越界索引。
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace std;
using namespace cv;
std::vector<cv::Point2i> GetPixelsWithValue(const cv::Mat1b& image, const unsigned char& value)
std::vector<cv::Point2i> pixels;
cv::Size imageSize = image.size();
for(int i = 0; i < imageSize.height; ++i)
for(int j = 0; j < imageSize.width; ++j)
if(image.at<unsigned char>(i, j) == value)
cv::Point2i pixel(j, i);
pixels.push_back(pixel);
return pixels;
int main()
cv::Mat1b image(10, 12);
image.setTo(0); //enable this and all the uninitialized value errors go away
image(0,1) = 63;
image(1,3) = 63;
image(3,5) = 63;
image(4,7) = 63;
image(5,9) = 63;
// image(10,12) = 63; //causes invalid write error
std::vector<cv::Point2i> vec = GetPixelsWithValue(image, static_cast<unsigned char>(63));
std::cout << image(10,12) << std::endl; //bad access. causes invalid read error only
std::cout << vec.size() << std::endl;
return 0;
【讨论】:
以上是关于Valgrind 大小为 4 的无效读取(OpenCV)的主要内容,如果未能解决你的问题,请参考以下文章
wchar_t valgrind 问题 - 大小为 8 的读取无效
valgrind 使用 std::string 报告无效读取