ios-nsmutablearray未删除时可能存在内存泄漏?

ios-nsmutablearray未删除时可能存在内存泄漏?,ios,iphone,objective-c,memory-leaks,nsmutablearray,Ios,Iphone,Objective C,Memory Leaks,Nsmutablearray,我是iOS开发新手,在我的应用程序中,我看到一些奇怪的内存使用行为 我正在使用以下setupDataForPage方法从服务器获取对象: - (void)setupDataForPage:(int)page actionType:(NSString *)type success:(void (^)())callback { __weak MyTableViewController *weakSelf = self; // clearing image cache because feed c

我是iOS开发新手,在我的应用程序中,我看到一些奇怪的内存使用行为

我正在使用以下
setupDataForPage
方法从服务器获取对象:

- (void)setupDataForPage:(int)page actionType:(NSString *)type success:(void (^)())callback 
{
__weak MyTableViewController *weakSelf = self;

// clearing image cache because feed contains a lot of images
[[SDImageCache sharedImageCache] clearMemory];
[[SDImageCache sharedImageCache] clearDisk];

MyHTTPClient *API = [MyHTTPClient new];

[API feedFor:page success:^(AFHTTPRequestOperation *operation, id data) {
    NSArray *data = [data objectForKey:@"data"];

    if ([data count] > 0) {
        // remove all objects to refresh with new ones
        if ([type isEqualToString:@"pullToRefresh"]) {
            [weakSelf.models removeAllObjects];
        }

        // populate data
        NSMutableArray *result = [NSMutableArray new];
        for (NSDictionary *modelData in data) {
            MyModel *model = [[MyModel alloc] initWithDictionary:modelData];
            [result addObject:model];
        }

        [weakSelf.models addObjectsFromArray:result];
        [weakSelf.tableView reloadData];
    }

    callback();
} failure:nil];
}
在获取初始请求时,它用于viewDidLoad,也用于拉式刷新和无限滚动:

- (void)viewDidLoad {
[super viewDidLoad];

__block int page = 1;
__weak MyTableViewController *weakSelf = self;

// initial load
[self setupDataForPage:page actionType:@"initial" success:^{ page += 1; }];

// pull to refresh
[self.tableView addPullToRefreshWithActionHandler:^{
    [weakSelf setupDataForPage:1 actionType:@"pullToRefresh" success:^{
        [weakSelf.tableView.pullToRefreshView stopAnimating];
    }];
}];

// infinite scrolling
[self.tableView addInfiniteScrollingWithActionHandler:^{
    [weakSelf setupItemsForPage:page actionType:@"infiniteScroll" success:^{
        page += 1;
        [weakSelf.tableView.infiniteScrollingView stopAnimating];
    }];
}];
}
我注意到,即使在返回相同数据的pull-to-refresh操作之后(我只是删除所有模型并再次添加它们),我的应用程序的内存使用量也从几乎
19mb
增长到
24mb

我想让更有经验的人看看这段代码,以确定它是否包含一些可能的内存泄漏。。在将变量分配给模型数组后,是否应该以某种方式删除
NSMutableArray*result
变量


谢谢

首先,在此处使用
@autoreleasepool

@autoreleasepool {
    NSArray *data = [data objectForKey:@"data"];

    if ([data count] > 0) {
        // remove all objects to refresh with new ones
        if ([type isEqualToString:@"pullToRefresh"]) {
            [weakSelf.models removeAllObjects];
        }

        // populate data
        NSMutableArray *result = [NSMutableArray new];
        for (NSDictionary *modelData in data) {
            MyModel *model = [[MyModel alloc] initWithDictionary:modelData];
            [result addObject:model];
        }

        [weakSelf.models addObjectsFromArray:result];
        [weakSelf.tableView reloadData];
    }
}
@autoreleasepool
允许您立即释放该范围内分配的每个对象


这是使用它的最佳情况;)

这可能是因为在每个pullToRefresh中,您都在分配和初始化一个新数组。@Morpheus正在删除它吗?请尝试在weakSelf.models中添加对象,而不是使用
new
array。在for-in-loop中。这是您的代码,在更多的地方,@autoreleasepool{在最后打开和关闭…有什么问题吗?我是说就在
[API feedFor:page success:^(AFHTTPRequestOperation*操作,id数据)中{…
?好:)添加,看起来有点帮助,pullToRefresh内存现在增加了1Mb,但仍然在增加..Can
NSMutableArray*result=[NSMutableArray new]
是原因吗?不,这是正常的,因为您向常规数组中添加了任何新数据,并且从未删除它,所以新数据,增加内存,完全正常:)。如果我的答案有效,请不要忘了选择最佳答案。谢谢你。谢谢!顺便问一下,我真的需要
@autoreleasepool
,因为我的应用程序正在使用ARC?