简体   繁体   中英

Memory Leak using Property List / NSMutableDictionary

I'm using a Property List in my main bundle to store information about the levels and player. The pList gets copied over to the phone the first time the app is launched, then I access the information as needed from that local copy. I'm running Leaks in Instruments, and I keep coming across memory leaks that I believe are related to creating the dictionary and other data types stored in the pList.

Here's where the dictionary is created - after finding the path to the pList:

if fileManager.fileExists(atPath: path.path) {

    if let dictionaryForPlist = NSMutableDictionary(contentsOf: path) {

        return(dictionaryForPlist)
    }
    else {
        print("pList not found")
    }

let levelInstanceData = LevelData() //this class searches the main bundle for the plist and stores the pList as an NSMutableDictionary
let currentLevel = levelInstanceData.localDataFile["Level1"] as! Int //localDataFile is the NSMutableDictionary storing the information
let levelName = levelInstanceData.localDataFile["Level1Name"] as! String

I forcefully cast each piece of data to the correct data type and use it throughout the level.

Here is a screenshot of the leaked objects in Instruments. Has anyone else had this issue or have any ideas how to stop the leaks?

Providing all the leaked objects isn't overly helpful. What you will need to do is look at the call tree related to each leaked object. This will show you where in code the leak has originated from. And from there you can start to deduce what needs to be done to remedy it.

You should read this. It is dated, but it discusses the call tree.

https://www.raywenderlich.com/2696/instruments-tutorial-for-ios-how-to-debug-memory-leaks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM