1

I am getting crash in [NSString stringWithFormat:]. I got it from Crashlytics. Following is the call stack and other information just above my app's last call

OS Version: 9.3.3 (13G34)
Device: iPhone 6s Plus
RAM Free: 8.7%
Disk Free: 20.9%

Crashed: com.apple.main-thread
CoreFoundation 0x1814224d0 __CFStringAppendFormatCore + 52

CoreFoundation 0x181422464 _CFStringCreateWithFormatAndArgumentsAux2 + 244
Foundation     0x181d3e320 -[NSPlaceholderString initWithFormat:locale:arguments:] + 168

Foundation     0x181d3e1f0 +[NSString stringWithFormat:] + 68


#import "ViewController.h"
#import <sys/sysctl.h>

static NSString *const kUserDefaultsPrefix = @"SDKData";
static NSString *const kVersionModel = @"VersionModel";
#define kOSVersion [[UIDevice currentDevice] systemVersion]
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self registerForMonitoring];
    VersioningModel *currentVersion = [VersioningModel currentVersion];



}

- (void)registerForMonitoring {
    [DataStorage addObserver:self forKeyPath:kVersionModel];
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {

    VersioningModel *currentVersion = [VersioningModel currentVersion];
}

-(NSString *)osVersionBuild {
    int mib[2] = {CTL_KERN, KERN_OSVERSION};
    u_int namelen = sizeof(mib) / sizeof(mib[0]);
    size_t bufferSize = 0;

    NSString *osBuildVersion = nil;

        // Get the size for the buffer
    sysctl(mib, namelen, NULL, &bufferSize, NULL, 0);

    u_char buildBuffer[bufferSize];
    int result = sysctl(mib, namelen, buildBuffer, &bufferSize, NULL, 0);

    if (result >= 0) {
        osBuildVersion = [[NSString alloc] initWithBytes:buildBuffer length:bufferSize encoding:NSUTF8StringEncoding] ;
    }

    return osBuildVersion;
}

-(NSString *)formattedOSVersionAndBuild{

    NSString *buildNumber = [self osVersionBuild] ?: nil;
    if (!buildNumber) {
        buildNumber = @"NONE";
    }
    buildNumber = [buildNumber stringByReplacingOccurrencesOfString:@"\0" withString:@""];
    return [NSString stringWithFormat:@"%@-%@", kOSVersion, buildNumber];
}

@end

// Version Model class ====================

@implementation VersioningModel {
}
+ (VersioningModel *)currentVersion {
    NSData *data = [DataStorage objectForKey:kVersionModel];
    VersioningModel *versionModel = data?[NSKeyedUnarchiver unarchiveObjectWithData:data]:nil;
    if (!versionModel){
        NSLog(@"unarchiveObjectWithData failed");
        versionModel = [[VersioningModel alloc] init];
        [versionModel save];
        NSLog(@"sucess");
    }

    return versionModel;
}

- (instancetype)init {
    self = [super init];
    if (self){
        self.platformCode = @"iOS";
        self.platformVersion = @"9.2-15F34";
        self.sdkVersion = @"2.0.1";
    }
    return self;
}

-(void)save{
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self];
    [DataStorage setObject:data forKey:kVersionModel];
}

//NSCoding
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super init];
    if (self){
        self.sdkVersion = [aDecoder decodeObjectForKey:@"sdkVersion"];
        self.platformVersion = [aDecoder decodeObjectForKey:@"platformVersion"];
        self.platformCode = [aDecoder decodeObjectForKey:@"platformCode"];
    }

    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.sdkVersion forKey:@"sdkVersion"];
    [aCoder encodeObject:self.platformVersion forKey:@"platformVersion"];
    [aCoder encodeObject:self.platformCode forKey:@"platformCode"];
}
@end
// End Version Model Class ==================


// DataStorage class ====================
@implementation DataStorage {
}

+ (NSString *)prefixAndKey:(NSString *)key {
    return [NSString stringWithFormat: @"%@%@", kUserDefaultsPrefix, key];
}

+ (id)objectForKey:(NSString *)key {
    return [[NSUserDefaults standardUserDefaults] objectForKey:[self prefixAndKey:key]];
}

+ (void)setObject:(id)object forKey:(NSString *)key {
    [[NSUserDefaults standardUserDefaults] setObject:object forKey:[self prefixAndKey:key]];
}

+ (void)addObserver:(id)object forKeyPath:(NSString *)key {

    [[NSUserDefaults standardUserDefaults] addObserver:object forKeyPath:[self prefixAndKey:key] options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:nil];
}
@end
// End DataStorage class ====================
Manish Nahar
  • 834
  • 10
  • 24
  • 1
    Can you provide more informations? E.g code snippets, parameters sent to stringWithFormat etc.. – Serkan Hekimoglu Aug 02 '16 at 11:52
  • it is hard to predict problem but might happen when string object is release , if you not have strong object then pls do it, and make sure you have correct format specifiers in stringWithFormat – Prashant Tukadiya Aug 02 '16 at 11:56
  • hi @SerkanHekimoglu I am posting sample code what exactly happening. please refer the function `currentVersion` which gets called in recursion when this crash had happened. some how `data` is not found or corrupted. I do not know why [versionModel save]; get called 2294 times and finally crashed. – Manish Nahar Aug 04 '16 at 14:47

0 Answers0