evermeer / EVURLCache

a NSURLCache subclass for handling all web requests that use NSURLRequest
Other
297 stars 49 forks source link

Crash when logging is turned on #24

Closed CedricVanAsch closed 8 years ago

CedricVanAsch commented 8 years ago

When I turn on the logging, the application crashes because of the following piece of code:

    // Log a message with info if enabled
    public static func debugLog<T>(object: T, filename: String = #file, line: Int = #line, funcname: String = #function) {
        if LOGGING {
            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "MM/dd/yyyy HH:mm:ss:SSS"
            let process = NSProcessInfo.processInfo()
            let threadId = "." //NSThread.currentThread().threadDictionary
            NSLog("\(dateFormatter.stringFromDate(NSDate())) \(process.processName))[\(process.processIdentifier):\(threadId)] \((filename as NSString).lastPathComponent)(\(line)) \(funcname):\r\t\(object)\n")
        }
    }

Specifically the NSLog("\(dateFormatter.stringFromDate(NSDate())) \(process.processName))[\(process.processIdentifier):\(threadId)] \((filename as NSString).lastPathComponent)(\(line)) \(funcname):\r\t\(object)\n") line crashes the application.

Error it gives: EXC_BAD_ACCESS. When: using Webview xCode version: 7.3

evermeer commented 8 years ago

That's strange. I'm not able to replicate this. Can you? When you set a breakpoint on that NSLog line and in the output window do a po of al the values, are you able to get the values? I for instance get something like this:

(lldb) po process
<NSProcessInfo: 0x7f9941403b90>

(lldb) po threadId
"."

(lldb) po filename
"/Users/evermeer/Desktop/dev/GitHub/EVURLCache/EVURLCache/Pod/EVURLCache.swift"

(lldb) po line
104

(lldb) po funcname
"cachedResponseForRequest"

(lldb) po object
"Returning cached data from /Users/evermeer/Library/Developer/CoreSimulator/Devices/36A811DC-5B78-4CAF-86CD-B42B38F120E5/data/Containers/Bundle/Application/220119E3-2C47-4C14-8AFF-4EC082F818F1/EVURLCache.app/PreCache/evict.nl/index.html"
CedricVanAsch commented 8 years ago

I can replicate it without a problem. Did a po for all the values for you, apparently it crashes the app at the 404 cache error for me.

(lldb) po funcname
"storeCachedResponse(_:forRequest:)"

(lldb) po line
127

(lldb) po object
"CACHE Do not cache error 404 page for : Optional(https://m.promobutler.be/api/purge-publication?publicationId=1040516&url=https%3A%2F%2Fm.promobutler.be%2Fnl%2Ffolders%2Fwoodtex%3Ffid%3D65209%26embedded%3Dtrue&linksVersion=29) <NSHTTPURLResponse: 0x105323d70> { URL: https://m.promobutler.be/api/purge-publication?publicationId=1040516&url=https%3A%2F%2Fm.promobutler.be%2Fnl%2Ffolders%2Fwoodtex%3Ffid%3D65209%26embedded%3Dtrue&linksVersion=29 } { status code: 404, headers {\n    \"Content-Encoding\" = gzip;\n    \"Content-Type\" = \"text/html;charset=UTF-8\";\n    Date = \"Fri, 20 May 2016 11:49:25 GMT\";\n    Expires = \"Thu, 01 Jan 1970 00:00:00 GMT\";\n    Server = \"cloudflare-nginx\";\n    Vary = \"Accept-Encoding\";\n    \"cf-cache-status\" = HIT;\n    \"cf-ray\" = \"2a5f90b28c613c89-CDG\";\n} }"

(lldb) po process
<NSProcessInfo: 0x10150b9a0>

(lldb) po threadId
"."

(lldb) po filename
"/Users/myMac/Documents/Apps/myApp-ios/Pods/EVURLCache/EVURLCache/Pod/EVURLCache.swift"
evermeer commented 8 years ago

Instead of NSLog I now use print. Then it does not crash. I could reproduce the crash by going to 'merken', 'A.Harzee' and scrolling down. After changing to print it's all ok. I could not see the actual reason why it should crash. All variable looked OK to me. So for now this fix should be good enough.