opencv/opencv

OpenCV 3.1.0 simple VideoCapture and waitKey crashes after a while on OS X 10.11.2

mahiuchun opened this issue · 65 comments

OpenCV 3.1.0 is installed through brew install opencv3 --with-contirb --with-qt5 and the following program crashes after a while:

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp>

int main(int argc, const char * argv[]) {
    cv::VideoCapture cap(0);
    cv::Mat frame;
    while (cap.read(frame)) {
        imshow("Frame", frame);
        if (cv::waitKey(1) == 'q') {
            break;
        }
    }
    return 0;
}

The stack trace is the following:

2015-12-24 09:54:22.297 basic-capture[86100:4481590] -[CaptureDelegate doFireTimer:]: unrecognized selector sent to instance 0x103600680
2015-12-24 09:54:22.313 basic-capture[86100:4481590] An uncaught exception was raised
2015-12-24 09:54:22.313 basic-capture[86100:4481590] -[CaptureDelegate doFireTimer:]: unrecognized selector sent to instance 0x103600680
2015-12-24 09:54:22.313 basic-capture[86100:4481590] (
    0   CoreFoundation                      0x00007fff95766ae2 __exceptionPreprocess + 178
    1   libobjc.A.dylib                     0x00007fff90699f7e objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff95769b9d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00007fff956a2601 ___forwarding___ + 1009
    4   CoreFoundation                      0x00007fff956a2188 _CF_forwarding_prep_0 + 120
    5   Foundation                          0x00007fff9c7d385b __NSFireTimer + 95
    6   CoreFoundation                      0x00007fff956acbc4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    7   CoreFoundation                      0x00007fff956ac853 __CFRunLoopDoTimer + 1075
    8   CoreFoundation                      0x00007fff9572ae6a __CFRunLoopDoTimers + 298
    9   CoreFoundation                      0x00007fff95667cd1 __CFRunLoopRun + 1841
    10  CoreFoundation                      0x00007fff95667338 CFRunLoopRunSpecific + 296
    11  HIToolbox                           0x00007fff8f2f3935 RunCurrentEventLoopInMode + 235
    12  HIToolbox                           0x00007fff8f2f3677 ReceiveNextEventCommon + 184
    13  HIToolbox                           0x00007fff8f2f35af _BlockUntilNextEventMatchingListInModeWithFilter + 71
    14  AppKit                              0x00007fff967d10ee _DPSNextEvent + 1067
    15  AppKit                              0x00007fff96b9d943 -[NSApplication _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 454
    16  libqcocoa.dylib                     0x000000010555ae5a _ZN21QCocoaEventDispatcher13processEventsE6QFlagsIN10QEventLoop17ProcessEventsFlagEE + 1034
    17  libopencv_highgui.3.1.dylib         0x000000010083c596 cvWaitKey + 178
    18  basic-capture                       0x0000000100001666 main + 246
    19  libdyld.dylib                       0x00007fff8a0335ad start + 1
    20  ???                                 0x0000000000000001 0x0 + 1
)
2015-12-24 09:54:22.314 basic-capture[86100:4481590] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CaptureDelegate doFireTimer:]: unrecognized selector sent to instance 0x103600680'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff95766ae2 __exceptionPreprocess + 178
    1   libobjc.A.dylib                     0x00007fff90699f7e objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff95769b9d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00007fff956a2601 ___forwarding___ + 1009
    4   CoreFoundation                      0x00007fff956a2188 _CF_forwarding_prep_0 + 120
    5   Foundation                          0x00007fff9c7d385b __NSFireTimer + 95
    6   CoreFoundation                      0x00007fff956acbc4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    7   CoreFoundation                      0x00007fff956ac853 __CFRunLoopDoTimer + 1075
    8   CoreFoundation                      0x00007fff9572ae6a __CFRunLoopDoTimers + 298
    9   CoreFoundation                      0x00007fff95667cd1 __CFRunLoopRun + 1841
    10  CoreFoundation                      0x00007fff95667338 CFRunLoopRunSpecific + 296
    11  HIToolbox                           0x00007fff8f2f3935 RunCurrentEventLoopInMode + 235
    12  HIToolbox                           0x00007fff8f2f3677 ReceiveNextEventCommon + 184
    13  HIToolbox                           0x00007fff8f2f35af _BlockUntilNextEventMatchingListInModeWithFilter + 71
    14  AppKit                              0x00007fff967d10ee _DPSNextEvent + 1067
    15  AppKit                              0x00007fff96b9d943 -[NSApplication _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 454
    16  libqcocoa.dylib                     0x000000010555ae5a _ZN21QCocoaEventDispatcher13processEventsE6QFlagsIN10QEventLoop17ProcessEventsFlagEE + 1034
    17  libopencv_highgui.3.1.dylib         0x000000010083c596 cvWaitKey + 178
    18  basic-capture                       0x0000000100001666 main + 246
    19  libdyld.dylib                       0x00007fff8a0335ad start + 1
    20  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

I can confirm issues with waitkey crashing frequently on OS X.

Please add result of cv::getBuildInformation()

Thanks!
QT backend is used here for UI interaction.
Are you able to reproduce this problem with Cocoa backend?

I'm seeing a similar problem with a similar program and what I thought was the Cocoa backend. A slightly censored file is attached with the output from cv::getBuildInformation() and the stacktraces.

One thing I noticed is when I step through in a debugger is I get breakpoints triggered at constant locations in cap_qtkit.mm. Line 335 always triggers twice, then line 299 triggers twice, then 435 triggers once, then the program works as intended.

edit: osx 10.10.5, opencv commit version 92387b1

buildinfo.txt

@kesinger Thanks for information! QT framework is not used in your build, but problem is still here.

Looks like, both QTKit capture + cv::waitKey are required to reproduce this bug.

Hi,

I am also have the same issue, but I'm not using the QT framework. My program is just plain simple c++ running on Xcode. (Update: Xcode's CoreFoundation uses QTKit Framework by default)

The error occurs after a couple of minutes my video capture c++ program. I even tried on a simple video capture program, and it repeats the same error.

openCV-error.txt

openCV-config.txt

Same install as OP, same stack trace, same problem. I did not see this in OpenCV 3.0. However, I also upgraded to 10.11 (El Capitan) near same time (which now I regret - SIP in OS X complicated the build process for latest versions of OpenCV+CUDA/cuDNN+Caffe w/Python2.7).

I'm using Yosemite 10.10, and I just downgraded to 3.0.0 and works pretty good. I need to run the program for longer period of time to see. But at the moment everything seems fine.

Reverting to 3.0 also solved the problem for me under OSX 10.11.

Also experiencing this problem with OpenCV 3.1.0, OSX version 10.11.2.

mron commented

Same problem with OpenCV 3.1.0, OSX version 10.11.2. Not using cv::waitKey().

mron commented

Same problem with OpenCV 3.0.0, OSX version 10.11.2. Not using cv::waitKey(). Should I be?
-[CaptureDelegate doFireTimer:]: unrecognized selector sent to instance 0x60800007e980

mron commented

Found this in: "opencv-3.1.0/modules/videoio/src/cap_qtkit.mm"
....
bool CvCaptureCAM::grabFrame(double timeOut)
// Create a dummy timer so that runUntilDate does not exit immediately:
[NSTimer scheduledTimerWithTimeInterval:100 target:nil selector:@selector(doFireTimer:) userInfo:nil repeats:YES];

This looks like it runs about 100sec then looks for nil and sends it doFireTimer:.
I can't find doFilreTimer(), and what happens when the timer target is nil?

on a site called fossies.org, the same line has "capture" as a target.
[NSTimer scheduledTimerWithTimeInterval:100 target:capture selector:@selector(doFireTimer:) userInfo:nil repeats:YES];

I can confirm consistent crash exactly 100 seconds after starting my program. I also have OS X 10.11.2 and OpenCV 3.1.0 using the VideoCapture class.

mron commented

This code doesn't make sense to me:
...
[NSTimer scheduledTimerWithTimeInterval:100 target:nil selector:@selector(doFireTimer:) userInfo:nil repeats:YES];
while (![capture updateImage] && (total += sleepTime)<=timeOut) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:sleepTime]];
}
...

The first line : " [NSTimer scheduledTimerWithTimeInterval ... " adds a timer to the current run loop, which isn't created until the while loop is executed. The "while" loop creates run loops to act essentially like sleep(). I don't think run loops act that way. [NSThread sleepForTimeInterval:] might work if there is a separate thread running. Also, if the timer is needed, it should be invalidated when no longer needed, and fixed so it references a valid object.

There is a patch for file cap_qtkit.mm between OpenCV 3.0.0 and OpenCV 3.1.0:
e8bf441#diff-2a81486ed8ebf4c35433ebaa5f2c5e75
Could someone revert this fix for cap_qtkit.mm and re-check this problem?
target:capture --> target:nil

@mron Could you try to use dummy cv::waitKey call (OpenCV 3.0.0):

void main(...)
{
    cv::waitKey(1);
    ... your code without waitKey ...
}
mron commented

I'm very new to opencv and github, so please bear with me. I did try putting the waitKey() call in. The timer still fires and crashes. Changing "nil" to "capture" will not help unless a "doFireTimer:" method is added to the capture's ( CaptureDelegate? ) class. I'm also new to objc.. sorry.

@rtielking did you fix this? I timed the code, and it crashes in exactly 100 seconds.

yes i was able to fix the crash at 100sec by commenting out the following line

[NSTimer scheduledTimerWithTimeInterval:100 target:capture selector:@selector(doFireTimer:) userInfo:nil repeats:YES];

in the /modules/videoio/src/cap_qtkit.mm file and recompiling

@rtielking I believe I installed OpenCV through Homebrew + Luarocks - how would I recompile it manually?

Download the source at https://github.com/Itseez/opencv/archive/3.1.0.zip
and extract the source code. Install XCode from Apple's App Store so you will have the developer tools you need. Find the line I mentioned above and comment it out. Then open a terminal and cd to the directory where you extracted the source code and run the following commands:

brew install cmake
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE ..
make -j4
sudo make install

Then it should work for you on the mac.

@rtielking following these instructions should theoretically allow my program to know which OpenCV version to use right? Should I remove my existing OpenCV installation first?

Might be a good idea to remove existing installation first just to be sure. I always built from source, so don't know where brew puts things when it installs.

@rtielking I compiled it.. commented out the line with the 100.. Then re-compiled it using the build instructions. However, the crash still happened at the 100 mark =/ I am also not 100% sure if the old installation is gone even though I deleted that folder.

mron commented

I was able to recompile with the line commented out. The crashes have stopped. I did have the same problem of Xcode using the old framework. I'm not sure exactly how I got Xcode to use the new library. I "make uninstall" on both 3.0 and 3.1, deleted both 3.0 and 3.1 frameworks, "make clean" on both 3.0 and 3.1. In Xcode I did Project->Clean and deleted "Derived Data" folder and removed the framework reference. Rebuilt 3.1, rebuilt the framework, add the framework. The changes were then used and new changes show up as I make them.

Having the same issue

Same issue here. Reverting to 3.0....

Could someone check this fix: #6051?

i'll check

I have verified that 3.1.0 crashes at 100 seconds on El Capitan 10.11.3 and fix #6051 fixed the crash issue. Thanks.

Works for me on OSX Yosemite.

Could someone tell me exactly how to apply this patch ? (sorry for the now question)

mron commented

I don’t understand github enough to help. I just changed my local copy.
Sorry,
Ron

I also changed the local copy, but didn't solved the problem. How did you recompile the file ? Or what steps do you took exactly to fix this ? Sorry for all the questions, but I need it for my theses. Thanks anyway !

mron commented

You have to tell Xcode to delete the cached versions of the library. I'm not sitting at the computer now, so I can't tell you how. It has to do with deleting "derived data". You can look it up. It took me a few tries to get rid of it properly. When I get home I'll look at how I did it. Xcode has so many layers, I don't remember off hand.

Ron

On Feb 26, 2016, at 3:24 AM, MikemanGit notifications@github.com wrote:

I also changed the local copy, but didn't solved the problem. How did you recompile the file ? Or what steps do you took exactly to fix this ? Sorry for all the questions, but I need it for my theses. Thanks anyway !


Reply to this email directly or view it on GitHub.

@MikemanGit hi Mike, this lovely person made a pretty great tutorial on rebuilding opencv with Make.
http://blogs.wcode.org/2014/10/howto-install-build-and-use-opencv-macosx-10-10/

also, make sure you download master at github - https://github.com/Itseez/opencv/archive/master.zip and not the binary linked to from the opencv homepage

like mron said, you will want to make sure your xcode project isn't holding on to any residual libs. inside your project after you install a fresh opencv, go to window -> projects and click delete on your derived data. afterwards you will want to 'clean' your project with product -> clean.
side note, videocapture.read() with webcams has a pretty nasty mem leak on osx 10.11 that seems to still exist in the master branch. if your camera reading needs to happen for a long time, you might want to stick to opencv2

I had a similar problem. If you have previously built OpenCV version 2 you need to deleted the old dylibs (dynamic libraries) before you "make install".
1--Using terminal, go to /usr/local/lib
2-- run ls -la
3-- you should see the older dylibs such as "highgui.2.4..." etc.
Remove all the "old" dylibs (anything with version 2...... etc)
It's actually easier to remove ALL the OpenCv and libdc1394 dylibs.

CMake will re-install them.

Managed to fix it, thanks guys !

Thank you for your help! this worked perfect

If you use Homebrew, you can also do brew install --HEAD opencv3 to get the newest release from the GitHub repository which consists of the above mentioned fix (probably uninstall the previous version first). Keep in mind though that this is installing the newest code base which is not stable and may introduce other bugs.

Same error and same stack trace here. Did the following:
brew remove opencv3 (effectively cleaned everything and Xcode immediately noticed it)
brew cleanup
brew install --HEAD opencv3
(might be unstable, but hopefully more stable than current 3.1)

thanks mateuszmikusz and LukasKalinski (and everyone) ... i've just updated my SO answer with your better advice

Hi guys. Thank you for all the advices. I reinstall the opencv yet I still have the problem.

Im running Yosemite version 10.10.5 and I also install opencv_contrib. Any suggestions? Or perhaps downgrading is the only way to go. How to downgrade it? Thank you

mron commented

I’m not familiar with github enough to help. Maybe send a more descriptive message about your install procedure and someone with more understanding can help.

On Mar 31, 2016, at 5:49 AM, Ghifarahadian notifications@github.com wrote:

Hi guys. Thank you for all the advices. I reinstall the opencv yet I still have the problem.

Im running Yosemite version 10.10.5 and I also install opencv_contrib. Any suggestions? Or perhaps downgrading is the only way to go. How to downgrade it? Thank you


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub #5874 (comment)

I am using this (https://github.com/Itseez/opencv_contrib) to install. Oh and initially i use this (https://www.youtube.com/watch?v=U49CVY8yOxw) to install openCV 3.1.0 on Mac. I am not sure whether the openCV installed this way is removed when using brew remove opencv3

hi, is this issue already solved?

Same problem, after a few captures the opencv crashes. I am using python bindings in Mac. This makes video capture unusable. I have to rely on some other frameworks to do the job.

any fixes for python bindings ? Deleting derived data is irrelevant in this case unfortunately.. cheers.

I have same problem in Sierra 10.12. v3.1.0 installed from anaconda

mron commented

Having this same problem on OSX 10.10.5. Any update on the fix?

mron commented

Having the same problem on OSX 10.11.5 . I am using the python interface for opencv2

Bumping the issue. By any chance could this be reopened so that a fix can happen to the repo?

mron commented

I use Anaconda for Python and Opencv on my Mac. How shall I recompile if that's the case?

mron commented

in anaconda prompt, type in:

conda install -c menpo opencv3

according to this website:

https://anaconda.org/menpo/opencv3

I created #8931 so hopefully it can actually be fixed. :-)

mron commented

This issue still occurs. OSX 10.12.5 with python 2.7.13 and python-opencv2

htzy commented

me too.
2017-09-20 23:20:24.579 python2.7[17999:2038663] -[CaptureDelegate doFireTimer:]: unrecognized selector sent to instance 0x7facf99ed4f0

Is someone looking at this issue?
Or at least, how can we install OpenCV 3.0? It seams brew or Pycharm can see only 3.1 and above.
Thanks.

If you're still having this problem with the latest release say so on #8931