我是谷歌测试框架的新手,并试图探索它。我尝试了一些简单的例子,一切都奏效了。
但是当我用 gtest 测试我的 OpneCV 程序(opencv 目前没有使用任何功能,只添加了头文件)时,它给了我段错误。举个例子
#include<iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
//for gtest API
#include "gtest/gtest.h" ///for google test framework
int main( int argc, char** argv )
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
TEST(Test, EqualityCheck) {
ASSERT_EQ(1,1)<<"Test Passed";
}
如果在没有 openCV 链接的情况下编译它,那么一切正常
g++ -o out cv_gtest.cpp -I/usr/local/include -I./inc /usr/local/lib/libgtest.a
但是如果使用 opencv 链接编译,我们会在运行时出现 seg 错误
g++ -o out cv_gtest.cpp -I/usr/local/include -I./inc /usr/local/lib/libgtest.a `pkg-config opencv --cflags --libs`
输出:
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from ImageTest
[ RUN ] Test.EqualityCheck
[ OK ] Test.EqualityCheck (0 ms)
[----------] 1 test from Test (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[ PASSED ] 1 test.
Segmentation fault (core dumped)
After debugging with valgrind (partial output)
==8284== Invalid read of size 4
==8284== at 0x34C049D4B6: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib64/libstdc++.so.6.0.13)
==8284== by 0x4394DD: void std::_Destroy<std::string>(std::string*) (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x4388A3: void std::_Destroy_aux<false>::__destroy<std::string*>(std::string*, std::string*) (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x436D92: void std::_Destroy<std::string*>(std::string*, std::string*) (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x433D98: void std::_Destroy<std::string*, std::string>(std::string*, std::string*, std::allocator<std::string>&) (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x42E879: std::vector<std::string, std::allocator<std::string> >::~vector() (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x34B943613C: __cxa_finalize (in /lib64/libc-2.12.so)
==8284== by 0x7613545: ??? (in /usr/local/lib/libopencv_ts.so.2.4.5)
==8284== by 0x767D1B0: ??? (in /usr/local/lib/libopencv_ts.so.2.4.5)
==8284== by 0x34B9435DA1: exit (in /lib64/libc-2.12.so)
==8284== by 0x34B941ECE3: (below main) (in /lib64/libc-2.12.so)
==8284== Address 0x7d99e98 is 8 bytes before a block of size 11 free'd
==8284== at 0x4A05B26: operator delete[](void*) (vg_replace_malloc.c:515)
==8284== by 0x425A0E: testing::internal::String::~String() (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x43A326: void std::_Destroy<testing::internal::String>(testing::internal::String*) (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x439CCA: void std::_Destroy_aux<false>::__destroy<testing::internal::String*>(testing::internal::String*, testing::internal::String*) (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x4392F1: void std::_Destroy<testing::internal::String*>(testing::internal::String*, testing::internal::String*) (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x438470: void std::_Destroy<testing::internal::String*, testing::internal::String>(testing::internal::String*, testing::internal::String*, std::allocator<testing::internal::String>&) (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x43AED3: std::vector<testing::internal::String, std::allocator<testing::internal::String> >::~vector() (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x34B9435DA1: exit (in /lib64/libc-2.12.so)
==8284== by 0x34B941ECE3: (below main) (in /lib64/libc-2.12.so)
==8284==
==8284== Invalid free() / delete / delete[] / realloc()
==8284== at 0x4A05FD6: operator delete(void*) (vg_replace_malloc.c:480)
==8284== by 0x34C049D4C8: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib64/libstdc++.so.6.0.13)
==8284== by 0x4394DD: void std::_Destroy<std::string>(std::string*) (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x4388A3: void std::_Destroy_aux<false>::__destroy<std::string*>(std::string*, std::string*) (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x436D92: void std::_Destroy<std::string*>(std::string*, std::string*) (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x433D98: void std::_Destroy<std::string*, std::string>(std::string*, std::string*, std::allocator<std::string>&) (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x42E879: std::vector<std::string, std::allocator<std::string> >::~vector() (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x34B943613C: __cxa_finalize (in /lib64/libc-2.12.so)
==8284== by 0x7613545: ??? (in /usr/local/lib/libopencv_ts.so.2.4.5)
==8284== by 0x767D1B0: ??? (in /usr/local/lib/libopencv_ts.so.2.4.5)
==8284== by 0x34B9435DA1: exit (in /lib64/libc-2.12.so)
==8284== by 0x34B941ECE3: (below main) (in /lib64/libc-2.12.so)
==8284== Address 0x7d99e88 is 24 bytes before a block of size 11 free'd
==8284== at 0x4A05B26: operator delete[](void*) (vg_replace_malloc.c:515)
==8284== by 0x425A0E: testing::internal::String::~String() (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x43A326: void std::_Destroy<testing::internal::String>(testing::internal::String*) (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x439CCA: void std::_Destroy_aux<false>::__destroy<testing::internal::String*>(testing::internal::String*, testing::internal::String*) (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x4392F1: void std::_Destroy<testing::internal::String*>(testing::internal::String*, testing::internal::String*) (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x438470: void std::_Destroy<testing::internal::String*, testing::internal::String>(testing::internal::String*, testing::internal::String*, std::allocator<testing::internal::String>&) (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x43AED3: std::vector<testing::internal::String, std::allocator<testing::internal::String> >::~vector() (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x34B9435DA1: exit (in /lib64/libc-2.12.so)
==8284== by 0x34B941ECE3: (below main) (in /lib64/libc-2.12.so)
==8284==
==8284==
==8284== Process terminating with default action of signal 11 (SIGSEGV)
==8284== Access not within mapped region at address 0x2
==8284== at 0x34C049D4B6: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib64/libstdc++.so.6.0.13)
==8284== by 0x4394DD: void std::_Destroy<std::string>(std::string*) (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x4388A3: void std::_Destroy_aux<false>::__destroy<std::string*>(std::string*, std::string*) (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x436D92: void std::_Destroy<std::string*>(std::string*, std::string*) (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x433D98: void std::_Destroy<std::string*, std::string>(std::string*, std::string*, std::allocator<std::string>&) (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x42E879: std::vector<std::string, std::allocator<std::string> >::~vector() (in /home/sairam/Desktop/sagar/CV_GTEST/colormap)
==8284== by 0x34B943613C: __cxa_finalize (in /lib64/libc-2.12.so)
==8284== by 0x7613545: ??? (in /usr/local/lib/libopencv_ts.so.2.4.5)
==8284== by 0x767D1B0: ??? (in /usr/local/lib/libopencv_ts.so.2.4.5)
==8284== by 0x34B9435DA1: exit (in /lib64/libc-2.12.so)
==8284== by 0x34B941ECE3: (below main) (in /lib64/libc-2.12.so)
==8284== If you believe this happened as a result of a stack
==8284== overflow in your program's main thread (unlikely but
==8284== possible), you can try to increase the size of the
==8284== main thread stack using the --main-stacksize= flag.
==8284== The main thread stack size used in this run was 10485760.
==8284==
==8284== HEAP SUMMARY:
==8284== in use at exit: 2,283,560 bytes in 183 blocks
==8284== total heap usage: 742 allocs, 560 frees, 3,695,829 bytes allocated
==8284==
==8284== LEAK SUMMARY:
==8284== definitely lost: 750,084 bytes in 4 blocks
==8284== indirectly lost: 0 bytes in 0 blocks
==8284== possibly lost: 1,503,662 bytes in 73 blocks
==8284== still reachable: 29,814 bytes in 106 blocks
==8284== suppressed: 0 bytes in 0 blocks
==8284== Rerun with --leak-check=full to see details of leaked memory
==8284==
==8284== For counts of detected and suppressed errors, rerun with: -v