opencv / opencv

Open Source Computer Vision Library
https://opencv.org
Apache License 2.0
75.95k stars 55.62k forks source link

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

Closed mahiuchun closed 8 years ago

mahiuchun commented 8 years ago

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
ahundt commented 8 years ago

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

alalek commented 8 years ago

Please add result of cv::getBuildInformation()

mahiuchun commented 8 years ago

Attached buildInfo.txt

alalek commented 8 years ago

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

kesinger commented 8 years ago

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 92387b1ef8fad15196dd5f7fb4931444a68bc93a

buildinfo.txt

alalek commented 8 years ago

@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.

Tobiasgeek commented 8 years ago

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

gbumgard commented 8 years ago

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).

Tobiasgeek commented 8 years ago

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.

gbumgard commented 8 years ago

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

BuiltInParris commented 8 years ago

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

mron commented 8 years ago

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

mron commented 8 years ago

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 8 years ago

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];

rtielking commented 8 years ago

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 8 years ago

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.

alalek commented 8 years ago

There is a patch for file cap_qtkit.mm between OpenCV 3.0.0 and OpenCV 3.1.0: https://github.com/Itseez/opencv/commit/e8bf4417ef7afbebf08283eb50147c12e1bf7b03#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 8 years ago

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.

jackyliang commented 8 years ago

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

rtielking commented 8 years ago

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

jackyliang commented 8 years ago

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

rtielking commented 8 years ago

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.

jackyliang commented 8 years ago

@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?

rtielking commented 8 years ago

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.

jackyliang commented 8 years ago

@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 8 years ago

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.

cedricve commented 8 years ago

Having the same issue

shawnsnyder commented 8 years ago

Same issue here. Reverting to 3.0....

alalek commented 8 years ago

Could someone check this fix: #6051?

rtielking commented 8 years ago

i'll check

rtielking commented 8 years ago

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.

cedricve commented 8 years ago

Works for me on OSX Yosemite.

MikemanGit commented 8 years ago

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

mron commented 8 years ago

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

MikemanGit commented 8 years ago

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 8 years ago

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.

shawnsnyder commented 8 years ago

@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

FredFG commented 8 years ago

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.

MikemanGit commented 8 years ago

Managed to fix it, thanks guys !

ottogutierrez commented 8 years ago

Thank you for your help! this worked perfect

mateuszmikusz commented 8 years ago

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.

LukasKalinski commented 8 years ago

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)

foundry commented 8 years ago

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

Ghifarahadian commented 8 years ago

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 8 years ago

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 https://github.com/Itseez/opencv/issues/5874#issuecomment-203917888

Ghifarahadian commented 8 years ago

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

geraldstanje commented 7 years ago

hi, is this issue already solved?

zhonghh commented 7 years ago

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.

casparwylie commented 7 years ago

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

hndr91 commented 7 years ago

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