Memory Leak in Swift using SKVideoNode for Mac OSX App

Viewed 202

I am using SKVideoNode to play a short video clip (about 10 seconds long) that loops. The video is only a couple MB. I'm not doing anything special with it but RAM usage goes up a 2-3MB every 5 seconds. When I comment out the add child, the mem leak stops, so I know it has something to do with this class. I can't find any reason online why this simple code is causing such a mem leak and what I can do about it, since this code seems pretty standard.

Here is the entire class:

    import SpriteKit
    import AVFoundation

    class BackgroundVideo:SKSpriteNode {
        var playerItem:AVPlayerItem?
        var player:AVPlayer?
        var videoNode:SKVideoNode?

        init() {
            super.init(texture: nil, color: NSColor.clearColor(), size: CGSize(width: 1920, height: 1080))

            let urlStr = getDocumentsDirectory() + "/videos/sample.mp4"
            let url = NSURL(fileURLWithPath: urlStr)

            let asset = AVURLAsset(URL: url, options: nil)
            playerItem = AVPlayerItem(asset: asset)
            player = AVPlayer(playerItem: playerItem!)

            NSNotificationCenter.defaultCenter().addObserver(self,
                selector: "playerItemDidReachEnd:",
                name: AVPlayerItemDidPlayToEndTimeNotification,
                object: self.player?.currentItem)

            videoNode = SKVideoNode(AVPlayer: player!)
            videoNode?.position = CGPointMake(self.size.width/2, self.size.height/2)

            self.addChild(videoNode!)
            videoNode?.play()
        }

        func playerItemDidReachEnd(notification: NSNotification) {
            self.player?.seekToTime(kCMTimeZero)
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }



        func getDocumentsDirectory() -> String {
            return NSHomeDirectory() + "/Desktop/Data"
        }


    }

It also doesn't seem to be related with the loop. If I turn off looping, the mem leak still occurs. The only thing that keeps the mem leak from happening is commenting out self.addChild(videoNode!)

0 Answers
Related