C ++ Opencv AddressSanitizer:未知地址0x000000000040上的SEGV

时间:2018-05-23 21:58:25

标签: c++ opencv address-sanitizer

我无法理解这个错误。我正在尝试使用opencv尝试使用hog和svm的项目,受此示例的启发:https://docs.opencv.org/3.4.1/d5/d77/train_HOG_8cpp-example.html#_a33(代码非常相似)。错误发生在imgs [0]上此函数的第二行。我明白错误是因为我试图用cout << imgs[0];打印整个图像并且它不打印整个矩阵但是错误出现在某一点。

void computeHOG(const vector<Mat> &imgs, vector<Mat> &gradients) {
  HOGDescriptor hog;
  hog.winSize = imgs[0].size() / 8 * 8;
  vector<float> descriptors;
  Mat gray;

  for (size_t i = 0; i < imgs.size(); ++i) {
    if (imgs[i].rows >= hog.winSize.height && imgs[i].cols >= hog.winSize.width) {
      Rect r = Rect((imgs[i].rows - hog.winSize.height) / 2,
                    (imgs[i].cols - hog.winSize.width) / 2,
                    hog.winSize.width,
                    hog.winSize.height);

      cvtColor(imgs[i](r), gray, COLOR_BGR2GRAY);
      hog.compute(gray, descriptors, Size(8, 8), Size(0, 0));
      gradients.push_back(Mat(descriptors).t());
    }
  }
}

这是我的Makefile

LDIR=lib
ODIR=build

CXX=g++
CXXFLAGS= -std=c++11 -Wall -Wextra -fsanitize=address -I$(LDIR)

LIBS=-lopencv_calib3d -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_video -lopencv_videoio

_DEPS=io.hpp hog.hpp
DEPS=$(patsubst %,$(LDIR)/%,$(_DEPS))

_OBJ=main.o io.o hog.o
OBJ=$(patsubst %,$(ODIR)/%,$(_OBJ))

$(ODIR)/%.o: $(LDIR)/%.cpp
    $(CXX) -c -o $@ $< $(CXXFLAGS)

$(ODIR)/%.o: main.cpp $(DEPS)
    $(CXX) -c -o $@ $< $(CXXFLAGS)

main: $(OBJ)
    $(CXX) -o $@ $^ $(CXXFLAGS) $(LIBS)

clean:
    rm -f $(ODIR)/*.o

.PHONY: clean

它编译但是当我运行它时我得到:

ASAN:SIGSEGV
=================================================================
==670==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000040 (pc 0x00000040c917 bp 0x7ffe8906f990 sp 0x7ffe8906f980 T0)
    #0 0x40c916 in cv::MatSize::operator()() const (/[...]/opencv-project/main+0x40c916)
    #1 0x40ada0 in computeHOG(std::vector<cv::Mat, std::allocator<cv::Mat> > const&, std::vector<cv::Mat, std::allocator<cv::Mat> >&) (/[...]/opencv-project/main+0x40ada0)
    #2 0x4030df in main (/[...]/opencv-project/main+0x4030df)
    #3 0x7f511da2082f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
    #4 0x402b48 in _start (/[...]/opencv-project/main+0x402b48)

AddressSanitizer can not provide additional info. SUMMARY: AddressSanitizer: SEGV ??:0 cv::MatSize::operator()() const
==670==ABORTING

如果我从Makefile中删除-fsanitize=address,那么错误来自编译,实际上太大而无法粘贴,但是从这样开始

main.cpp:(.text+0x20): undefined reference to `__asan_option_detect_stack_use_after_return'
main.cpp:(.text+0x30): undefined reference to `__asan_stack_malloc_4'
build/main.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x970): undefined reference to `__asan_before_dynamic_init'
main.cpp:(.text+0x9d4): undefined reference to `__asan_report_load1'
main.cpp:(.text+0xa14): undefined reference to `__asan_report_store1'
main.cpp:(.text+0xa64): undefined reference to `__asan_report_load1'

我的g ++版本是g++ (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609 没有找到任何解决方案......

1 个答案:

答案 0 :(得分:1)

您的代码在调用NULL时会在某个时刻访问Mat::operator ()指针。从您发布的代码段中不清楚这一点的原因,但如果您从调试器运行代码,则应该很明显。

删除-fsanitize=address时未定义的引用表示在您更改Makefile后未重新编译某些代码。你能先做make clean吗?

作为旁注,您还应将-fsanitize=address附加到LDFLAGS(以便在运行时库中链接)。