Skip to content

Commit

Permalink
Fixes memory leak created by Apple’s interoperability (#85)
Browse files Browse the repository at this point in the history
Fixes memory leak created by Apple’s interoperability for initializing object from protocol.Type with @objc attribute
  • Loading branch information
gal-orlanczyk committed Feb 12, 2017
1 parent 2b5f93c commit 49f150c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 5 additions & 2 deletions Classes/Managers/PlayKitManager.swift
Expand Up @@ -50,8 +50,11 @@ public class PlayKitManager: NSObject {
return loader
}

public func registerPlugin(_ pluginClass: PKPlugin.Type) {
pluginRegistry[pluginClass.pluginName] = pluginClass
public func registerPlugin(_ pluginClass: Plugin.Type) {
guard let pluginType = pluginClass as? PKPlugin.Type else {
fatalError("plugin class must be of type PKPlugin")
}
pluginRegistry[pluginType.pluginName] = pluginType
}

func createPlugin(name: String, player: Player, pluginConfig: Any?, messageBus: MessageBus) -> PKPlugin? {
Expand Down
1 change: 1 addition & 0 deletions Classes/Player/AVPlayerEngine.swift
Expand Up @@ -217,6 +217,7 @@ class AVPlayerEngine: AVPlayer {
self.onEventBlock = nil
// removes app state observer
AppStateSubject.sharedInstance.remove(observer: self)
self.replaceCurrentItem(with: nil)
self.isDestroyed = true
}

Expand Down
13 changes: 12 additions & 1 deletion Classes/Plugins/PKPlugin.swift
Expand Up @@ -9,8 +9,19 @@
import UIKit
import AVFoundation

/**
Used as a workaround for Apple bug with swift interoperability.
There is an issue with initializing an object based on a protocol.Type with @objc attribute.
Therefore we use a wrapper protocol for PKPlugin with @objc and then casting to a PKPlugin without the @objc attribute.
- important:
**should not be used! use PKPlugin to add a plugin**
*/
@objc public protocol Plugin {}

/// The `PKPlugin` protocol defines all the properties and methods required to define a plugin object.
@objc public protocol PKPlugin {
public protocol PKPlugin: Plugin {
/// The plugin name.
static var pluginName: String { get }
/// The associated media entry.
Expand Down

0 comments on commit 49f150c

Please sign in to comment.