views:

39

answers:

3

I have a menu scene, help scene, settings scene and game scene. When i enter into game scene, it loads some data string from a .plist file into a NSMutableArray. I am doing this reading and loading procedure as follows-

+ (void) addObjectsToArray:(NSMutableArray*) mutableArray fromFile:(NSString*) filePath
{
    NSString *bundle = [[NSBundle mainBundle] bundlePath];

    NSString *path = [bundle stringByAppendingPathComponent:filePath];

    [mutableArray addObjectsFromArray: [NSMutableArray arrayWithContentsOfFile:path]];
    [bundle release];

}

I can get into the game scene and can back to menu scene from there. But when I try to get into the game scene forth time(menu scene to game then back to menu scene and go to game scene again and do), the app just crashes.

I have found the crash point(using NSLog)-

+ (void) addObjectsToArray:(NSMutableArray*) mutableArray fromFile:(NSString*) filePath
{
    NSString *bundle = [[NSBundle mainBundle] bundlePath];
    **NSLog(@"always reach here");**
    NSString *path = [bundle stringByAppendingPathComponent:filePath];**// CRASH POINT**
    **NSLog(@"Forth time, doesnt reach here");**

    [mutableArray addObjectsFromArray: [NSMutableArray arrayWithContentsOfFile:path]];
    [bundle release];

}

But why crashing i dont understand and found no solution yet.

A: 

Any stack trace?

Anyway try to wrap it into a try-catch. I always do this when handling files whether they are from bundle or downloaded.

@try {
    NSString *bundle = [[NSBundle mainBundle] bundlePath];

    NSString *path = [bundle stringByAppendingPathComponent:filePath];

    [mutableArray addObjectsFromArray: [NSMutableArray arrayWithContentsOfFile:path]];
    [bundle release];
}
@catch (NSException * e) {
    NSLog (@"exception: %@", exception);
}

My guess is that filePath is nil. Maybe try to NSLog(@"filePath: %@", filePath); in the beginning of (void) addObjectsToArray:(NSMutableArray*) mutableArray fromFile:(NSString*) filePath

Cheers mate.

objneodude
@objneodude, no exception is thrown, but crashed.
Sadat
+2  A: 

I think you should not release bundle
"[[NSBundle mainBundle] bundlePath]" should return an autorelease object.

Then as you release it, its relain count should reached 0 at the 4rd time and crash the app

Benoît
thanks @Benoit, i got it. I have an another relative query:- if i use bundlePath( eg. [[NSBundle mainBundle] bundlePath]) from a class/method and then try it from an another class/method- would it use the un-released bundlePath which was previously used?
Sadat
+1  A: 

You must not release an object if you don't own it.
You are responsible for (auto)releasing an object only if you increased its retain-count. Either by calling retain explicitly or by calling methods with alloc, copy or new in the name.

Please read the Memory Managment Guide again


Btw, if you run the analyzer (you can find it in the Build menu) it will point you at the exact line where you did something wrong.

fluchtpunkt
thanks @fluchtpunkt, i will check it.I have an another relative query:- if i use bundlePath( eg. [[NSBundle mainBundle] bundlePath]) from a class/method and then try it from an another class/method- would it use the un-released bundlePath which was previously used? (also asked it to @Benoit)
Sadat