reidmain / FDFoundationKit

An extension of Foundation Kit by 1414 Degrees.
MIT License
9 stars 5 forks source link

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[1]' #4

Closed robinsenior closed 9 years ago

robinsenior commented 9 years ago

I have experienced a crash in -[FDCache allObjects] : 75 (return _weakDictionary.allValues;)

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*\ -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[1]'

If I po _weakDictionary I see:

{ atp = "<TSLeague: 0x174192bd0>"; bund = "<TSLeague: 0x1701934c0>"; cfl = "<TSLeague: 0x170192d70>"; chlg = "<TSLeague: 0x174192d70>"; engcc = "<TSLeague: 0x174193180>"; engfa = "<TSLeague: 0x1741930b0>"; epga = "<TSLeague: 0x1701938d0>"; epl = "<TSLeague: 0x174192fe0>"; espcr = "<TSLeague: 0x170193250>"; eurc = "<TSLeague: 0x174382a40>"; eurcq = "<TSLeague: 0x174382ff0>"; fmf = "<TSLeague: 0x174193250>"; formula1 = "<TSLeague: 0x174192b00>"; fran = "<TSLeague: 0x170193660>"; gerdfbp = "<TSLeague: 0x170193590>"; itaci = "<TSLeague: 0x1701933f0>"; liga = "<TSLeague: 0x170193180>"; lpga = "<TSLeague: 0x1701939a0>"; mlb = "<TSLeague: 0x170192ca0>"; mls = "<TSLeague: 0x174192f10>"; mma = "<TSLeague: 0x170192f10>"; nascar = "<TSLeague: 0x170192fe0>"; nasnw = "<TSLeague: 0x170387840>"; nba = "<TSLeague: 0x170192b00>"; ncaab = "<TSLeague: 0x170192e40>"; ncaaf = "<TSLeague: 0x170192bd0>"; nfl = "<TSLeague: 0x170192a30>"; nhl = "<TSLeague: 0x1741927c0>"; nll = "<TSLeague: 0x170193730>"; pga = "<TSLeague: 0x174192a30>"; pga2 = "<TSLeague: 0x170193800>"; seri = "<TSLeague: 0x170193320>"; uefa = "<TSLeague: 0x174192e40>"; wcbk = "<TSLeague: 0x1701930b0>"; wjhc = "<TSLeague: 0x170192890>"; wolymhm = "<TSLeague: 0x170388bc0>"; wolymhw = "<TSLeague: 0x170388af0>"; worldcup = "<TSLeague: 0x174380820>"; wta = "<TSLeague: 0x174192ca0>"; }

reidmain commented 9 years ago

Found the crash. FDCache relies on an NSCache and an FDWeakMutableDictionary internally. If an object inside the FDWeakMutableDictionary has been released from the NSCache and is referenced nowhere else in the application the - (id)objectForKey: (id)key method inside FDWeakMutableDictionary will return nil for the key associated with that object. allObjects seems to iterate over all the keys of the FDWeakMutableDictionary and simply adds the result of objectForKey: to an array because it makes the assumption that no object in the array can be nil (which is obviously true for NSDictionary). This obviously causes an exception because you can't add nil to a NSMutableArray.

The solution is to just override allObjects inside FDWeakMutableDictionary and check if the result of objectForKey: is nil before adding it to the array.

reidmain commented 9 years ago

Fixed with the commit https://github.com/reidmain/FDFoundationKit/commit/239fc895d648af3de825d29c4122d03a458dbc4e