有时使用网络URL作为参数调用函数cvCreateFileCapture
时,会发生分段错误,例如:rtsp://IPaddress:Port/Path...
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
...
CvCapture* capture = cvCreateFileCapture("rtsp://...");
...
return a.exec();
}
如果参数是磁盘上的文件,则不会发生错误。
我在调试模式下构建源代码,当分段故障发生时,会列出以下堆栈跟踪:
gst_caps_unref
CvCapture_GStreamer::open(int, char const*)
cvCreateCapture_GStreamer(int, char const*)
cvCreateFileCapture
main
更新如果我创建一个纯C++项目,那么对函数cvCreateFileCapture
的调用总是会导致分段错误。相反,如果我创建一个Qt-Gui应用程序项目,则分段错误会随机发生。
VALGRIND的输出
==3160== Memcheck, a memory error detector
==3160== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==3160== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==3160== Command: ./Calibration 15 8 5 http://root:root@192.168.1.51/axis-cgi/mjpg/video.cgi
==3160==
--3160-- WARNING: Serious error when reading debug info
--3160-- When reading debug info from /usr/lib/nvidia-331/libGL.so.331.20:
--3160-- Can't make sense of .data section mapping
--3160-- WARNING: Serious error when reading debug info
--3160-- When reading debug info from /usr/lib/nvidia-331/libnvidia-glcore.so.331.20:
--3160-- Can't make sense of .got section mapping
==3160== Invalid read of size 4
==3160== at 0x74444A2: ??? (in /usr/lib/i386-linux-gnu/libfontconfig.so.1.4.4)
==3160== by 0x7446B09: FcConfigFilename (in /usr/lib/i386-linux-gnu/libfontconfig.so.1.4.4)
==3160== by 0x745B1EB: FcConfigParseAndLoad (in /usr/lib/i386-linux-gnu/libfontconfig.so.1.4.4)
==3160== by 0x7450065: FcInitLoadConfig (in /usr/lib/i386-linux-gnu/libfontconfig.so.1.4.4)
==3160== by 0x7450188: FcInitLoadConfigAndFonts (in /usr/lib/i386-linux-gnu/libfontconfig.so.1.4.4)
==3160== by 0x745040C: FcInit (in /usr/lib/i386-linux-gnu/libfontconfig.so.1.4.4)
==3160== by 0x585B608: ??? (in /usr/lib/i386-linux-gnu/libQtGui.so.4.8.1)
==3160== by 0x57DCC0B: QApplicationPrivate::construct(_XDisplay*, unsigned long, unsigned long) (in /usr/lib/i386-linux-gnu/libQtGui.so.4.8.1)
==3160== by 0x7E014E7: ???
==3160== Address 0xaaa898c is 20 bytes inside a block of size 22 alloc'd
==3160== at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3160== by 0x7444407: ??? (in /usr/lib/i386-linux-gnu/libfontconfig.so.1.4.4)
==3160== by 0x7446B09: FcConfigFilename (in /usr/lib/i386-linux-gnu/libfontconfig.so.1.4.4)
==3160== by 0x745B1EB: FcConfigParseAndLoad (in /usr/lib/i386-linux-gnu/libfontconfig.so.1.4.4)
==3160== by 0x7450065: FcInitLoadConfig (in /usr/lib/i386-linux-gnu/libfontconfig.so.1.4.4)
==3160== by 0x7450188: FcInitLoadConfigAndFonts (in /usr/lib/i386-linux-gnu/libfontconfig.so.1.4.4)
==3160== by 0x745040C: FcInit (in /usr/lib/i386-linux-gnu/libfontconfig.so.1.4.4)
==3160== by 0x585B608: ??? (in /usr/lib/i386-linux-gnu/libQtGui.so.4.8.1)
==3160== by 0x57DCC0B: QApplicationPrivate::construct(_XDisplay*, unsigned long, unsigned long) (in /usr/lib/i386-linux-gnu/libQtGui.so.4.8.1)
==3160== by 0x7E014E7: ???
==3160==
==3160==
==3160== ---- Attach to debugger ? --- [Return/N/n/Y/y/C/c] ---- Y
==3160== starting debugger with cmd: /usr/bin/gdb -nw /proc/3163/fd/1024 3163
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /proc/3163/fd/1024...done.
Attaching to program: /proc/3163/fd/1024, process 3163
Reading symbols from /usr/lib/valgrind/vgpreload_core-x86-linux.so...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/valgrind/vgpreload_core-x86-linux.so
Reading symbols from /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so
Reading symbols from /usr/local/lib/libopencv_core.so.2.4...(no debugging symbols found)...done.
Loaded symbols for /usr/local/lib/libopencv_core.so.2.4
Reading symbols from /usr/local/lib/libopencv_imgproc.so.2.4...(no debugging symbols found)...done.
Loaded symbols for /usr/local/lib/libopencv_imgproc.so.2.4
Reading symbols from /usr/local/lib/libopencv_highgui.so.2.4...done.
Loaded symbols for /usr/local/lib/libopencv_highgui.so.2.4
Reading symbols from /usr/local/lib/libopencv_calib3d.so.2.4...(no debugging symbols found)...done.
Loaded symbols for /usr/local/lib/libopencv_calib3d.so.2.4
Reading symbols from /usr/lib/i386-linux-gnu/libQtGui.so.4...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libQtGui.so.4
Reading symbols from /lib/i386-linux-gnu/libpthread.so.0...Reading symbols from /usr/lib/debug/lib/i386-linux-gnu/libpthread-2.15.so...done.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".
done.
Loaded symbols for /lib/i386-linux-gnu/libpthread.so.0
Reading symbols from /usr/lib/i386-linux-gnu/libstdc++.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libstdc++.so.6
Reading symbols from /lib/i386-linux-gnu/libgcc_s.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib/i386-linux-gnu/libgcc_s.so.1
Reading symbols from /lib/i386-linux-gnu/libc.so.6...Reading symbols from /usr/lib/debug/lib/i386-linux-gnu/libc-2.15.so...done.
done.
Loaded symbols for /lib/i386-linux-gnu/libc.so.6
Reading symbols from /lib/i386-linux-gnu/libz.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib/i386-linux-gnu/libz.so.1
Reading symbols from /lib/i386-linux-gnu/librt.so.1...Reading symbols from /usr/lib/debug/lib/i386-linux-gnu/librt-2.15.so...done.
done.
Loaded symbols for /lib/i386-linux-gnu/librt.so.1
Reading symbols from /usr/lib/i386-linux-gnu/libGLU.so.1...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libGLU.so.1
Reading symbols from /usr/lib/nvidia-331/libGL.so.1...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/nvidia-331/libGL.so.1
Reading symbols from /lib/i386-linux-gnu/libm.so.6...Reading symbols from /usr/lib/debug/lib/i386-linux-gnu/libm-2.15.so...done.
done.
Loaded symbols for /lib/i386-linux-gnu/libm.so.6
Reading symbols from /usr/lib/i386-linux-gnu/libjpeg.so.8...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libjpeg.so.8
Reading symbols from /lib/i386-linux-gnu/libpng12.so.0...(no debugging symbols found)...done.
Loaded symbols for /lib/i386-linux-gnu/libpng12.so.0
Reading symbols from /usr/lib/i386-linux-gnu/libtiff.so.4...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libtiff.so.4
Reading symbols from /usr/lib/i386-linux-gnu/libjasper.so.1...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libjasper.so.1
Reading symbols from /usr/lib/libIlmImf.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/libIlmImf.so.6
Reading symbols from /usr/lib/libHalf.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/libHalf.so.6
Reading symbols from /usr/lib/i386-linux-gnu/libgstreamer-0.10.so.0...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libgstreamer-0.10.so.0
Reading symbols from /usr/lib/i386-linux-gnu/libgobject-2.0.so.0...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libgobject-2.0.so.0
Reading symbols from /lib/i386-linux-gnu/libglib-2.0.so.0...(no debugging symbols found)...done.
Loaded symbols for /lib/i386-linux-gnu/libglib-2.0.so.0
Reading symbols from /usr/lib/i386-linux-gnu/libgstapp-0.10.so.0...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libgstapp-0.10.so.0
Reading symbols from /usr/lib/i386-linux-gnu/libgstvideo-0.10.so.0...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libgstvideo-0.10.so.0
Reading symbols from /usr/lib/i386-linux-gnu/libdc1394.so.22...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libdc1394.so.22
Reading symbols from /usr/local/lib/libv4l1.so.0...done.
Loaded symbols for /usr/local/lib/libv4l1.so.0
Reading symbols from /usr/lib/i386-linux-gnu/libxvidcore.so.4...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libxvidcore.so.4
Reading symbols from /usr/lib/i386-linux-gnu/libQtOpenGL.so.4...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libQtOpenGL.so.4
Reading symbols from /usr/lib/i386-linux-gnu/libQtCore.so.4...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libQtCore.so.4
Reading symbols from /usr/lib/i386-linux-gnu/libQtTest.so.4...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libQtTest.so.4
Reading symbols from /usr/lib/i386-linux-gnu/libva.so.1...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libva.so.1
Reading symbols from /usr/lib/i386-linux-gnu/libvorbisenc.so.2...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libvorbisenc.so.2
Reading symbols from /usr/lib/i386-linux-gnu/libvorbis.so.0...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libvorbis.so.0
Reading symbols from /usr/lib/i386-linux-gnu/libtheoraenc.so.1...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libtheoraenc.so.1
Reading symbols from /usr/lib/i386-linux-gnu/libtheoradec.so.1...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libtheoradec.so.1
Reading symbols from /usr/lib/libopencore-amrwb.so.0...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/libopencore-amrwb.so.0
Reading symbols from /usr/lib/libopencore-amrnb.so.0...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/libopencore-amrnb.so.0
Reading symbols from /usr/lib/i386-linux-gnu/libmp3lame.so.0...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libmp3lame.so.0
Reading symbols from /usr/lib/libfaac.so.0...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/libfaac.so.0
Reading symbols from /usr/local/lib/libopencv_flann.so.2.4...(no debugging symbols found)...done.
Loaded symbols for /usr/local/lib/libopencv_flann.so.2.4
Reading symbols from /usr/local/lib/libopencv_features2d.so.2.4...(no debugging symbols found)...done.
Loaded symbols for /usr/local/lib/libopencv_features2d.so.2.4
Reading symbols from /usr/lib/i386-linux-gnu/libfontconfig.so.1...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libfontconfig.so.1
Reading symbols from /usr/lib/i386-linux-gnu/libaudio.so.2...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libaudio.so.2
Reading symbols from /usr/lib/i386-linux-gnu/libfreetype.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libfreetype.so.6
Reading symbols from /usr/lib/i386-linux-gnu/libSM.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libSM.so.6
Reading symbols from /usr/lib/i386-linux-gnu/libICE.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libICE.so.6
Reading symbols from /usr/lib/i386-linux-gnu/libXi.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libXi.so.6
Reading symbols from /usr/lib/i386-linux-gnu/libXrender.so.1...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libXrender.so.1
Reading symbols from /usr/lib/i386-linux-gnu/libXext.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libXext.so.6
Reading symbols from /usr/lib/i386-linux-gnu/libX11.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libX11.so.6
Reading symbols from /lib/ld-linux.so.2...Reading symbols from /usr/lib/debug/lib/i386-linux-gnu/ld-2.15.so...done.
done.
Loaded symbols for /lib/ld-linux.so.2
Reading symbols from /usr/lib/nvidia-331/tls/libnvidia-tls.so.331.20...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/nvidia-331/tls/libnvidia-tls.so.331.20
Reading symbols from /usr/lib/nvidia-331/libnvidia-glcore.so.331.20...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/nvidia-331/libnvidia-glcore.so.331.20
Reading symbols from /lib/i386-linux-gnu/libdl.so.2...Reading symbols from /usr/lib/debug/lib/i386-linux-gnu/libdl-2.15.so...done.
done.
Loaded symbols for /lib/i386-linux-gnu/libdl.so.2
Reading symbols from /usr/lib/libImath.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/libImath.so.6
Reading symbols from /usr/lib/libIex.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/libIex.so.6
Reading symbols from /usr/lib/libIlmThread.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/libIlmThread.so.6
Reading symbols from /usr/lib/i386-linux-gnu/libgmodule-2.0.so.0...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libgmodule-2.0.so.0
Reading symbols from /usr/lib/i386-linux-gnu/libxml2.so.2...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libxml2.so.2
Reading symbols from /usr/lib/i386-linux-gnu/libffi.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libffi.so.6
Reading symbols from /lib/i386-linux-gnu/libpcre.so.3...(no debugging symbols found)...done.
Loaded symbols for /lib/i386-linux-gnu/libpcre.so.3
Reading symbols from /usr/lib/i386-linux-gnu/libgstbase-0.10.so.0...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libgstbase-0.10.so.0
Reading symbols from /usr/lib/i386-linux-gnu/liborc-0.4.so.0...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/liborc-0.4.so.0
Reading symbols from /usr/lib/i386-linux-gnu/libraw1394.so.11...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libraw1394.so.11
Reading symbols from /lib/i386-linux-gnu/libusb-1.0.so.0...(no debugging symbols found)...done.
Loaded symbols for /lib/i386-linux-gnu/libusb-1.0.so.0
Reading symbols from /usr/local/lib/libv4l2.so.0...done.
Loaded symbols for /usr/local/lib/libv4l2.so.0
Reading symbols from /usr/lib/i386-linux-gnu/libogg.so.0...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libogg.so.0
Reading symbols from /lib/i386-linux-gnu/libexpat.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib/i386-linux-gnu/libexpat.so.1
Reading symbols from /usr/lib/i386-linux-gnu/libXt.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libXt.so.6
Reading symbols from /usr/lib/i386-linux-gnu/libXau.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libXau.so.6
Reading symbols from /lib/i386-linux-gnu/libuuid.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib/i386-linux-gnu/libuuid.so.1
Reading symbols from /usr/lib/i386-linux-gnu/libxcb.so.1...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libxcb.so.1
Reading symbols from /usr/local/lib/libv4lconvert.so.0...done.
Loaded symbols for /usr/local/lib/libv4lconvert.so.0
Reading symbols from /usr/lib/i386-linux-gnu/libXdmcp.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/i386-linux-gnu/libXdmcp.so.6
Reading symbols from /usr/lib/i386-linux-gnu/gconv/UTF-16.so...Reading symbols from /usr/lib/debug/usr/lib/i386-linux-gnu/gconv/UTF-16.so...done.
done.
Loaded symbols for /usr/lib/i386-linux-gnu/gconv/UTF-16.so
Reading symbols from /usr/lib/libicui18n.so.48...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/libicui18n.so.48
Reading symbols from /usr/lib/libicuuc.so.48...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/libicuuc.so.48
Reading symbols from /usr/lib/libicudata.so.48...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/libicudata.so.48
Failed to read a valid object file image from memory.
0x074444a2 in ?? () from /usr/lib/i386-linux-gnu/libfontconfig.so.1
(gdb)
当函数cvCreateFileCapture为有时使用网络URL作为参数调用,如果自变量是磁盘上的一个文件。
根据问题的症状,您的程序遇到了某种内存损坏。总之网络URL流执行正在执行某些操作(内存溢出/覆盖)的概率如果你使用网络URL,看起来你可以重现这个问题。但是,请不要认为如果URL是磁盘上的文件(本地),它就没有任何问题。腐败也有可能以这种方式发生,只是这种影响被推迟了。内存损坏问题很难调试,如果不对我们的程序进行广泛的分析,就没有简单的步骤来识别它们。
查看堆栈跟踪,几乎不可能找出这可能是什么根本原因,因为这些分段故障将是程序在早期执行中所做的真正糟糕的事情的影响。
识别这些内存损坏的最佳方法是使用一些动态工具以及调试器(Windows上的WinDBG/PageHeap)。从您的问题(分段错误)来看,您可能正在Linux平台上运行。因此,您可能需要将您的程序(a.out)附加到Valgrind/GDB中。
$ valgrind --tool=memcheck --db-attach=yes ./a.out
这样,当您的第一个内存错误为检测到,以便您可以执行实时调试(GDB)。这应该是理解和解决问题的最佳方式。一旦你能够找出你的第一个错误,修复它并重新运行它,看看你还有什么其他错误。应执行这些步骤,直到Valgrind没有报告错误为止。
在cvQueryFrame();
函数之前添加cvWaitKey(100);
。
如果您在Linux环境中,您可以使用valgrind来准确地找出分段故障发生的位置。只需在程序名称或执行程序的方式之前键入valgrind即可。例如,如果您使用以下命令执行程序:
你好-打印
改为发出以下命令:
valgrind你好-打印
您还可以使用不同的cout或printf语句对代码进行"胡椒"处理,并观察分段错误之前的最后一个输出。如果程序很大,这种方法可能会更麻烦/用处更小。您可以在cvCreateFileCapture出现在您的主函数中的每个位置之前和之后执行此操作,以找出它发生的位置。如果cvCreateFileCapture是opencv的一部分,那么它似乎是opencv的一个bug!
看看这个C++代码,
VideoCapture vcap;
Mat image;
const string videoStreamAddress = "rtsp://IPaddress:Port/Path?dummy=param.mjpg";//
//open the video stream and make sure it's opened
if(!vcap.open(videoStreamAddress)) {
cout << "Error opening video stream or file" << std::endl;
return -1;
}
for(;;) {
if(!vcap.read(image)) {
cout << "No frame" << std::endl;
waitKey();
}
imshow("Output Window", image);
if(waitKey(1) >= 0) break;
}
并确保您的流没有密码保护。
gst_caps_unref<-AFAIK这个函数释放gst对象使用的内存。我希望您在valgrind下运行您的应用程序,但使用以下命令:valgrind --leak-check=full ./a.out
。并附上它的输出,而不是你目前的输出(我向你保证,它会提供更多信息)。你的bug可能不会在Valgrind下重现,因为它为程序提供了类似"私人内存"的东西。在这种情况下,我建议您获得cgdb
(gdb的好接口),并在它下调试程序(检查变量值、可能的null指针、删除itd)