开发者

NSMutableArray function call leaking ..?

maybe someone can help me finding why his code is leaking ..

im calling the getNotes function, wich开发者_如何学运维 is returning a autorelease NSMutableArray

    notesArray = [[noteManager getNotes:id] retain];

notesArray is a property declared in my header file

@property (nonatomic, retain) NSMutableArray* notesArray;

this is the stripped version of the getNotes function

- (NSMutableArray*) getNotes:(NSString *)id {


    NSMutableArray* rArr = [[NSMutableArray alloc] init];

    for (NSString* sNote in noteArray) {
        myNote* note = (myNote*)[NSKeyedUnarchiver unarchiveObjectWithFile:sFile];
        [rArr addObject:note];

    }
    return [rArr autorelease];
}

the [rArr addObject:note]; is 100% leaking .. why? they are all autoreleased?

the myNote class just a class with some properties, nothing special ...


It may be that you already have notes stored in notesArray and they are not getting released before setting it again.

Try changing this

notesArray = [[noteManager getNotes:id] retain];
//to
self.notesArray = [noteManager getNotes:id];


Does notesArray get released at some point? Instead of manually retaining you can use the dot syntax:

self.notesArray = [noteManager getNotes:id];

instead of:

notesArray = [[noteManager getNotes:id] retain];


for (NSString* sNote in noteArray) {
        myNote* note = (myNote*)[NSKeyedUnarchiver unarchiveObjectWithFile:sFile];
        [rArr addObject:note];

    }

Here your notes are autoreleased and shoud not produce memory leak, but autorelease does not happen immediately. This can become a problem in a long loops. You should use your own autorelease pool to avoid such situations. Like this

for (NSString* sNote in noteArray) {
            NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
            myNote* note = (myNote*)[NSKeyedUnarchiver unarchiveObjectWithFile:sFile];
            [rArr addObject:note];
            [pool drain];    
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜