Crash issue for [NSString stringWithFormat:]
-
27-05-2021 - |
Question
I submitted my application to apple for review.Apple rejected and gave me the following crash report.
Thread 0 Crashed:
1 Foundation 0x3595dd04 _NSDescriptionWithLocaleFunc + 44
2 CoreFoundation 0x36ae696e __CFStringAppendFormatCore + 7998
3 CoreFoundation 0x36a611d8 _CFStringCreateWithFormatAndArgumentsAux + 68
4 Foundation 0x3595dc2e +[NSString stringWithFormat:] + 54
5 WIPTime 0x0001572e -[NSDate(TKCategory) reportHeader] + 66
6 WIPTime 0x0006bd9e -[ReportDelegate tableView:cellForRowAtIndexPath:] + 474
7 UIKit 0x36de009c -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 540
8 UIKit 0x36ddf17a -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1070
9 UIKit 0x36dde904 -[UITableView layoutSubviews] + 200
10 UIKit 0x36d830d8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 176
11 CoreFoundation 0x36a651f4 -[NSObject performSelector:withObject:] + 36
12 QuartzCore 0x36241a9e -[CALayer layoutSublayers] + 210
13 QuartzCore 0x362416b6 CA::Layer::layout_if_needed(CA::Transaction*) + 210
14 QuartzCore 0x3624583c CA::Context::commit_transaction(CA::Transaction*) + 220
15 QuartzCore 0x36245578 CA::Transaction::commit() + 308
16 QuartzCore 0x3623d4b2 CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 50
17 CoreFoundation 0x36adab14 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 12
18 CoreFoundation 0x36ad8d50 __CFRunLoopDoObservers + 252
19 CoreFoundation 0x36ad90aa __CFRunLoopRun + 754
20 CoreFoundation 0x36a5c49e CFRunLoopRunSpecific + 294
21 CoreFoundation 0x36a5c366 CFRunLoopRunInMode + 98
22 GraphicsServices 0x30b3e432 GSEventRunModal + 130
23 UIKit 0x36dade76 UIApplicationMain + 1074
24 WIPTime 0x00003032 main + 42
25 WIPTime 0x00002ffc start + 32
the implementation for the reportHeader is following :
-(NSString *) reportHeader
{
NSString *realWorld=[self dateDescriptionRealWorld];
NSString *year=[self yearString];
NSString *weekDay=[self weekdayString];
NSString *returnString=[NSString stringWithFormat:@"%@ %@ %@",weekDay,realWorld,year];
return returnString;
}
Utility methods that are used within the method is following:
-(NSString *) dateDescriptionRealWorld
{
return [NSString stringWithFormat:@"%@ %@",[self dayString],[self monthStringWithCod]];
}
- (NSString*) monthString{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"MMMM"];
return [dateFormatter stringFromDate:self];
}
- (NSString*) yearString{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"yyyy"];
return [dateFormatter stringFromDate:self];
}
- (NSString*) dayString{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd"];
return [dateFormatter stringFromDate:self];
}
- (int) weekday{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit) fromDate:self];
int weekday = [comps weekday];
[gregorian release];
return weekday;
}
-(NSString *) weekdayString
{
NSDate *date=self;
int weekday=[date weekday];
if (weekday ==0) {
return @"Sat";
}
else if(weekday ==1)
{
return @"Sun";
}
else if(weekday ==2)
{
return @"Mon";
}
else if(weekday ==3)
{
return @"Tue";
}
else if(weekday ==4)
{
return @"Wed";
}
else if(weekday ==5)
{
return @"Thu";
}
else if(weekday ==5)
{
return @"Fri";
}
}
Solution
Where does the variable realWorld
come into the picture? I suspect it's an class field that you assign the result of calling dateDescriptionRealWorld
, but never retain. That's just a guess, though, based on incomplete information.
OTHER TIPS
A few thoughts... You should use switch/case and a default case instead of if/else. Also, you are naming your methods incorrectly. Instead of a weekday method, you should name it after what it does and returns, like, weekdayFormatted. In addition, you should probably not be alloc'ing a new dateFormatter in every method but use one NSDateFormatter in your class, and set its format in each of these methods.
I suggest removing all your methods and put the formatting inside your reportHeader method, to clean it up. You are alloc'ing quite a bit and although you use autorelease, you are relying on it too much to clean up otherwise perhaps unwise memory management.
That may not be too "practical" quick fix for you but those are the things that I see which could lead to better memory management.
Also - @Aadihira has a great question and you should show the monthStringWithCod method as well.