views:

90

answers:

2

I am honestly not able to figure out when to releas these objects, Please help

+ (DrawData*) load {
    DrawData *newDrawData = [[DrawData alloc] init];

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSFileManager *fm = [NSFileManager defaultManager];

    BOOL keepLoading = YES;
    int curIndex = 0;

    while ( keepLoading ) {

        BoardData *tmpBoard = [[BoardData alloc] init];

        NSString *imageFilename = [[NSString alloc] initWithFormat:@"iCanvas_image_%d.png", curIndex];
        NSString *metadataFilename = [[NSString alloc] initWithFormat:@"iCanvas_metadata_%d.txt", curIndex];
        NSString *layersFilename = [[NSString alloc] initWithFormat:@"iCanvas_layers_%d.dat", curIndex];

        imageFilename = [documentsDirectory stringByAppendingPathComponent:imageFilename];
        metadataFilename = [documentsDirectory stringByAppendingPathComponent:metadataFilename];

        if ( [fm fileExistsAtPath:imageFilename] && [fm fileExistsAtPath:metadataFilename] ) {

            NSString *metadataFile = [[NSString alloc] initWithContentsOfFile:metadataFilename];
            NSArray *metadata = [metadataFile componentsSeparatedByCharactersInSet:
                                 [NSCharacterSet whitespaceCharacterSet]];
            tmpBoard.drawImageTypeOverlay = [[metadata objectAtIndex:0] intValue];
            tmpBoard.brushSize = [[metadata objectAtIndex:1] floatValue];
            tmpBoard.brushColorRed = [[metadata objectAtIndex:2] floatValue];
            tmpBoard.brushColorGreen = [[metadata objectAtIndex:3] floatValue];
            tmpBoard.brushColorBlue = [[metadata objectAtIndex:4] floatValue];
            tmpBoard.brushColorAlpha = [[metadata objectAtIndex:5] floatValue];
            tmpBoard.isErasing = [[metadata objectAtIndex:6] intValue];
            tmpBoard.eraseSize = [[metadata objectAtIndex:7] floatValue];
            tmpBoard.isSelected = [[metadata objectAtIndex:8] intValue];
            tmpBoard.layers = [NSMutableArray arrayWithContentsOfFile:layersFilename];
            [newDrawData addBoard:tmpBoard];
        }
        else {
            keepLoading = NO;
        }

        [tmpBoard release];

    }

    [pool release];

    return newDrawData;

}
+6  A: 

You should get in the habit of either:

1) Adding to autorelease pool

NSString *imageFilename = [[[NSString alloc] initWithFormat:@"iCanvas_image_%d.png", curIndex] autorelease];

OR

2) sending a release message (when done working with it)

[imageFilename release];

to variables you declare with this pattern:

    NSString *imageFilename = [[NSString alloc] initWithFormat:@"iCanvas_image_%d.png", curIndex];

Which is basically

[[blah alloc] init];

Read more: http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/MemoryMgmt/

Mr-sk
...or use convinience methods like [NSString stringWithFormat:...];
Vladimir
A: 
  1. I'm not sure if you want to return autoreleased newDrawData - check your ownership logic.
  2. imageFilename and metadataFilename are overwritten directly after +alloc/-initWithFormat:
  3. metadataFile is allocated but not released.

Points 2 and 3 are repeated in a while loop which may increase the number of actual leaks.

Costique