這個循環是存在內存泄漏:NSMutableArray的環路出現內存泄漏,甚至當我明確地釋放它
int64_t i,verylongnumber;
//misc. code
for(i=0;i<verylongnumber;i++){
NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:
[NSNumber numberWithLongLong:65535],
[NSNumber numberWithLongLong:65535],
[NSNumber numberWithLongLong:65535],
[NSNumber numberWithLongLong:65535],
nil];
[myArray removeAllObjects];
[myArray release];
}
我已經竭盡所能,以保持它的內存泄露,但我不能。我認爲這與NSNumbers有關。我假設它們是自動發佈的,但這是否意味着我必須單獨釋放它們(即使用alloc)?我怎麼會這樣做呢?爲每個NSNumber創建一個單獨的變量並將其插入到數組中?這似乎很多工作。我試過[myArray removeAllObjects],但這沒有什麼區別。它在我自己的線程中擁有自己的autorelease池。我不確定這是否有所作爲。
這個固定:
我增加了一個額外的自動釋放池,內環路:
int64_t i,verylongnumber;
//misc. code
for(i=0;i<verylongnumber;i++){
NSAutoreleasePool *pool2 = [[NSAutoreleasePool alloc] init];
NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:
[NSNumber numberWithLongLong:65535],
[NSNumber numberWithLongLong:65535],
[NSNumber numberWithLongLong:65535],
[NSNumber numberWithLongLong:65535],
nil];
[myArray release];
[pool2 drain];
}
你可能想看看進入'@ autoreleasepool'聲明,這既是ARC和MRC支持下,比NSAutoreleasePool更快(在ARC轉換少了一個步驟,當你到這一點)。 –