Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 7/Swift 2.1 "Message from debugger: Terminated due to memory issue"

I'm working on a card game in Swift 2.1 using Xcode 7 and my app runs fine in the simulator but crashes when I test it on my device.

Using breakpoints, I've pinpointed the crash to an NSTimer.scheduledTimerWithTimeInterval method that runs after an animation occurs (and then triggers another animation).

I thought maybe it was the size of my images, as some were quite large (>4 MB), so I compressed all the images in the animation, and in total they now take up less than 1 MB.

I've also run the Zombie and Leak tools and found nothing, so I'm a little perplexed. Here's the code where it crashes.

func animateOnDeal() {
    self.playerAnimatedCard.hidden = false
    self.dealerAnimatedCard.hidden = true
    cardOneToDeal()
}

func cardOneToDeal() {
    UIView.animateWithDuration(0.5, animations: {
        self.playerAnimatedCard.center.x -= self.view.bounds.width
        }, completion: {finished in self.flipCardOne()})
}

func flipCardOne() {
    self.playerAnimatedCard.playFlipAnimation()
    NSTimer.scheduledTimerWithTimeInterval(0.3, target: self, selector: "cardTwoToDeal", userInfo: nil, repeats: false)
}

And here's the code that actually runs the animation (in a UIImageView subclass):

func playFlipAnimation() {
    self.image = UIImage(named: "cardback2.png")
    self.animationImages = nil
    var imgArray = [UIImage]()

    for var x = 1; x <= 12; x++ {
        let img = UIImage(named: "img\(x).png")
        imgArray.append(img!)
    }

    self.animationImages = imgArray
    self.animationDuration = 0.3
    self.animationRepeatCount = 1
    self.startAnimating()

As a side note, the debugger simply states: "Message from debugger: Terminated due to memory issue."

Any help would be greatly appreciated, please let me know if you need any more information. Thanks!

EDIT:

So in order to test it out some more, I changed func playFlipAnimation to iterate and add 5 images instead of the original 12. This seems to have solved the crash, but I'm still unsure as to why having more images is crashing the application in the first place.

like image 525
rdespoiu Avatar asked Nov 28 '15 04:11

rdespoiu


2 Answers

A couple of points:

  1. As Derek Lee has already pointed out images can be very memory intensive. In my experience the runtime will also keep images cached in the background for a period after they are used. This could be pertinent to your situation as, looking at your code, you are loading in a fresh set of twelve images every time the animation is called. If you are iterating that animation a lot of times then that will quickly add up to a lot of memory, especially with image files of 1MB. In this case, while it may seem inefficient in the short run you might want to consider initializing an array of images when the class is initialized, which you can reuse.

  2. One thing you can do when iterating over a loop that you know is going to be memory intensive is place it in an autoreleasepool. See the developer reference at: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html I know that technically ARC should be all you need in the modern iOS world but that worked for me when I was building a very data-intensive procedure in one of my apps.

  3. You are unlikely ever to trigger a memory warning running in Simulator as no allowance is made for the fact that the underlying hardware has considerably more capacity available to it than the target device. In my infinite wisdom I once left something running which ended up using 50GB of memory but that did not trigger any kind of warnings on Simulator!

  4. As an aside, I would think you should be able to compress your card images down to far less than 1 MB. Of course this will depend on what kind of image you want to use (are they photos?) but if it is a simple .png, then that seems quite large to me.

Hope that helps.

like image 159
Marcus Avatar answered Sep 19 '22 17:09

Marcus


I met the same problem and found it was the problem of I checked the Enable Zombie Objects in the scheme. So you may also check it. enter image description here

like image 34
Alfy Avatar answered Sep 21 '22 17:09

Alfy