Search code examples
iosxcodememory-leaksuiimageinstruments

iOS - App crashing after Memory Warning - Instruments showing no leaks


I'm working on an app, that's using an AVCaptureSession, to show the "live" video stream from the device camera. I also use the AVCaptureVideoDataOutputSampleBufferDelegate methods to capture still images (UIImage) from the sampleBuffer to work with these images while the video is still being displayed on the screen (using OpenCV). Anyway, there's a lot of code, much more than I could paste here, but that's actually not the point.

The app will show up a memory warning after some seconds and some more seconds later, crash silently. I tried to use Instruments to inspect leaks and memory allocation, but there's nothing suspicious showing up, no leaks at all and memory usage is never going above 5MB (overall and live bytes).

How do I find out why the memory warning is triggered?

EDIT: I tried to add some intentional leaks to the code, which will show up in Instruments, so it seems to work in theory...


Solution

  • I just found the reason, although I don't understand why Instruments doesn't show this...

    I used [t performSelectorInBackground:sel withObject:[self imageFromSampleBuffer:sampleBuffer]];

    in

    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection

    method. After I commented that out, the app would run fine. So the problem here was, although I used a NSLock in the selector, the app would spawn more and more threads for each selector until the memory limit was reached. So the solution here is to use a lock before performing the selector, not inside the selector method.

    Thanks for all your help.