-1
+ (NSString *)getValueforLocale:(NSString*) i18nkey :(NSString*)locale{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSLog(@"paths are : %@",paths);
    NSString *libraryDirectory = [paths objectAtIndex:0];
    NSLog(@"libraryDirectory : %@",libraryDirectory);
    NSString *filePath = [libraryDirectory stringByAppendingPathComponent:@"I8nDB"];
    filePath = [filePath stringByAppendingPathComponent:locale];
    NSLog(@"file path is : %@",filePath);
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
    if(fileExists)
    {
        NSDictionary *dict = [[[NSDictionary alloc] initWithContentsOfFile:filePath]autorelease];
        NSDictionary *resourceBundle = [[[NSDictionary alloc] init]autorelease];
        NSString *keyValue = [[[NSString alloc]init]autorelease];
        resourceBundle = [dict valueForKey:@"hash"];
        keyValue=[resourceBundle valueForKey:i18nkey];
        NSLog(@"value for %@ is(container) : %@",i18nkey,keyValue);
        if(keyValue != nil || keyValue != NULL)
        {
            return keyValue;
        }
        else
        {
            NSLog(@"key not found in the container file");
            NSString *path = [[NSBundle mainBundle] pathForResource:@"Localizable"
                                                             ofType:@"strings"
                                                        inDirectory:nil
                                                    forLocalization:locale];
            NSLog(@"path for %@ is : %@",locale,path);
            fileExists = [[NSFileManager defaultManager] fileExistsAtPath:path];
            if(fileExists)
            {
                NSDictionary *dict = [[NSDictionary dictionaryWithContentsOfFile:path]autorelease];
                NSLog(@"value for %@ is(resources) : %@",i18nkey,[dict objectForKey:i18nkey]);
                return [dict objectForKey:i18nkey];
            }
            else
            {
                return NULL;
            }
        }
    }
    else
    {
        NSLog(@"%@ locale does not exist in container",locale);
        NSString *path = [[NSBundle mainBundle] pathForResource:@"Localizable"
                                                         ofType:@"strings"
                                                    inDirectory:nil
                                                forLocalization:locale];
        NSLog(@"path for %@ in resources is : %@",locale,path);
        fileExists = [[NSFileManager defaultManager] fileExistsAtPath:path];
        if(fileExists)
        {
            NSDictionary *dict = [[NSDictionary dictionaryWithContentsOfFile:path]autorelease];
            NSLog(@"value for %@ is : %@",i18nkey,[dict objectForKey:i18nkey]);
            return [dict objectForKey:i18nkey];
        }
        else
        {
            return NULL;
        }
    }
}

If we remove Autorelease from the above code, it is working in iOS7 if not the app is crashing

My Main concern is why it doesn't crash in iOS8&9 and only crashes in iOS7 is there in change related to autorelease over these versions

newacct
  • 119,665
  • 29
  • 163
  • 224
Madhu Avinash
  • 933
  • 2
  • 8
  • 27
  • it's normal when issue code can work in once iOS version and not in other. Because Apple change system every release note. So only code without issue can work in all iOS – larva Jul 29 '16 at 07:31

3 Answers3

0

Why don't you use ARC? Then you won't need autorelease... See http://rypress.com/tutorials/objective-c/memory-management

Your problem might be related to the settings about ARC.

Colas
  • 3,473
  • 4
  • 29
  • 68
0

in Your code you only alloc a dictionary in

NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];

so you only need to care about it, another object is not owned by You! so You don't need release or autorelease them.

Try flowing code

+ (NSString *)getValueforLocale:(NSString*) i18nkey :(NSString*)locale
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        NSLog(@"paths are : %@",paths);
        NSString *libraryDirectory = [paths objectAtIndex:0];
        NSLog(@"libraryDirectory : %@",libraryDirectory);
        NSString *filePath = [libraryDirectory stringByAppendingPathComponent:@"I8nDB"];
        filePath = [filePath stringByAppendingPathComponent:locale];
        NSLog(@"file path is : %@",filePath);
        BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
        if(fileExists)
        {
            NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
            //NSDictionary *resourceBundle = [[[NSDictionary alloc] init]autorelease];
            //NSString *keyValue = [[[NSString alloc]init]autorelease];
            NSDictionary *resourceBundle = [dict valueForKey:@"hash"];
            // relese dict here because not use after
            [dict release];
            NSString *keyValue=[resourceBundle valueForKey:i18nkey];
            NSLog(@"value for %@ is(container) : %@",i18nkey,keyValue);
            if(keyValue != nil || keyValue != NULL)
            {
                return keyValue;
            }
            else
            {
                NSLog(@"key not found in the container file");
                NSString *path = [[NSBundle mainBundle] pathForResource:@"Localizable"
                                                                 ofType:@"strings"
                                                            inDirectory:nil
                                                        forLocalization:locale];
                NSLog(@"path for %@ is : %@",locale,path);
                fileExists = [[NSFileManager defaultManager] fileExistsAtPath:path];
                if(fileExists)
                {
                    // NSDictionary *dict = [[NSDictionary dictionaryWithContentsOfFile:path]autorelease];
                    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
                    NSLog(@"value for %@ is(resources) : %@",i18nkey,[dict objectForKey:i18nkey]);
                    return [dict objectForKey:i18nkey];
                }
                else
                {
                    return NULL;
                }
            }
        }
        else
        {
            NSLog(@"%@ locale does not exist in container",locale);
            NSString *path = [[NSBundle mainBundle] pathForResource:@"Localizable"
                                                             ofType:@"strings"
                                                        inDirectory:nil
                                                    forLocalization:locale];
            NSLog(@"path for %@ in resources is : %@",locale,path);
            fileExists = [[NSFileManager defaultManager] fileExistsAtPath:path];
            if(fileExists)
            {
                // NSDictionary *dict = [[NSDictionary dictionaryWithContentsOfFile:path]autorelease];
                NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
                NSLog(@"value for %@ is : %@",i18nkey,[dict objectForKey:i18nkey]);
                return [dict objectForKey:i18nkey];
            }
            else
            {
                return NULL;
            }
        }
    }
larva
  • 4,687
  • 1
  • 24
  • 44
0

In manual reference counting, retains and releases need to be balanced.

In

NSDictionary *dict = [[[NSDictionary alloc] initWithContentsOfFile:filePath]autorelease];
NSDictionary *resourceBundle = [[[NSDictionary alloc] init]autorelease];

the retains and releases are balanced, because alloc (along with retain, new, copy, mutableCopy) returns a retained instance, and autorelease counts as a release.

However, in

NSDictionary *dict = [[NSDictionary dictionaryWithContentsOfFile:path]autorelease];

you have an overrelease because you are autorelease something that you have not retained.

iOS version has absolutely nothing to do with it.

newacct
  • 119,665
  • 29
  • 163
  • 224