1

Below is my source code, every time I execute the function, the memory usage increases dramatically. Please help to point out what is the problem.

func loadfontsFromDatabase(code:String)->[String] {
    let documentsPath : AnyObject = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true)[0] as AnyObject
    let databasePath = documentsPath.appending("/bsmcoding.sqlite")
    let contactDB = FMDatabase(path: databasePath as String)
    var c:[String]=[]

    let querySQL = "SELECT FONT FROM BSMCODE WHERE BSMCODE.CODE = '\(code)' ORDER BY NO DESC"

    NSLog("query:\(querySQL)")

    let results:FMResultSet? = Constants.contactDB?.executeQuery(querySQL, withArgumentsIn: nil)

    while (results?.next())! {
        c.append((results?.string(forColumn: "FONT"))!)
    }

    results?.close()

    return c
}
zisoft
  • 22,770
  • 10
  • 62
  • 73

1 Answers1

0

There's nothing here that would account for any substantial memory loss. I would suggest using the "Debug Memory Graph" feature in Xcode 8 to identify what objects are being created and not being released, but I suspect the problem rests elsewhere in your code. Or use Instruments to track it down what's leaking and debug from there. See https://stackoverflow.com/a/30993476/1271826.

There are unrelated issues here, though:

  1. You are creating local contactDB, but you never open it and you never use it. It will be released when the routine exits, but it's completely unnecessary if you're going to use Constants.contactDB, anyway.

  2. I'd advise against using string interpolation when building your SQL. Use ? placeholder and pass the code in as a parameter. This is much safer, in case the code ever contained something that couldn't be represented in SQL statement. (This is especially true if the code was supplied by the user, in which case you'd be susceptible to SQL injection attacks or innocent input errors that could lead to crashes.)

    For example, you could do something like:

    func loadfontsFromDatabase(code: String) -> [String] {
        var c = [String]()
    
        let querySQL = "SELECT FONT FROM BSMCODE WHERE BSMCODE.CODE = ? ORDER BY NO DESC"
    
        let results = try! Constants.contactDB!.executeQuery(querySQL, values: [code])
    
        while results.next() {
            c.append((results.string(forColumn: "FONT"))!)
        }
    
        return c
    }
    

    If you don't like the forced unwrapping, you can do optional unwrapping if you want, but personally I'd rather know immediately when debugging during the development phase if there's some logic mistake (e.g. the contactDB wasn't open, the SQL is incorrect, etc.). But you can do optional binding and add the necessary guard statements if you want. But don't just do optional binding and silently return a value suggesting that everything is copacetic, leaving you with a debugging challenge of tracking down the problem if you don't get what you expected.

    But the key point is to avoid inserting values into your SQL directly. Use ? placeholders.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Thank you. I find out the problem. After I executed the query, the display change a lot and draw a lot of memory. Now I am figuring how to solve it. Thanks – user2174638 Mar 24 '17 at 05:23
  • I think below is the source of the problem. How to modify it so that it will not draw a huge memory during each execution. Please help. – user2174638 Mar 25 '17 at 06:55