iPhone上的内存泄漏:(

Chr*_*ris 1 iphone cocoa-touch memory-management objective-c

我是C,Obj-C和iPhone的初学者,我正在尝试使用大量的术语.我希望你们中的一个能帮助解决我几天来一直在努力解决的问题.

我下面的代码是一个调用包含搜索字段和表的笔尖的方法.该表是通过搜索为下面的'theList'创建的数组来填充的.使用'Instruments',我得到了一个Leak:NSDictionary*theItem = [NSDictionary dictionaryWithObjectsAndKeys:clientName,@"Name",clientId,@"Id",nil]; ,但我无法弄清楚为什么:(

我知道这可能是一个难以回答的问题,但是如果有任何人可以提供帮助的话!

- (void)editClient:(id)sender {

    if (pickList == nil) {
        pickList = [[PickFromListViewController alloc] initWithNibName:@"PickList" bundle:nil];
    }

    TimeLogAppDelegate *appDelegate = (TimeLogAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSMutableArray *theList = [[NSMutableArray alloc] init];
    int i;
    for (i=0;i < [appDelegate.clients count];i++) {
        Client *thisClient = [appDelegate.clients objectAtIndex:i];
        NSString *clientName = [[NSString alloc] initWithString: thisClient.clientsName];
        NSNumber *clientId = [[NSNumber alloc] init];
        clientId = [NSNumber numberWithInt:thisClient.clientsId];
        NSDictionary *theItem = [NSDictionary dictionaryWithObjectsAndKeys:clientName,@"Name",clientId,@"Id",nil];
        [theList addObject:theItem];
        theItem = nil;
        [clientName release];
        [clientId release];
    }
    [pickList createSearchItems:theList :NSLocalizedString(@"Client",nil)];
    [theList release];

    appDelegate.returningID = [NSNumber numberWithInt: projectsClientsId];
    [self.navigationController pushViewController:pickList animated:YES];

}
Run Code Online (Sandbox Code Playgroud)

提前致谢!

ste*_*anB 9

这将返回已分配的NSNumber实例.

NSNumber *clientId = [[NSNumber alloc] init];
Run Code Online (Sandbox Code Playgroud)

这行用另一个NSNumber实例覆盖上面的clientId,numberWithInt返回自动释放的对象,因为你没有为它分配内存你不应该调用release,它会自动释放.

clientId = [NSNumber numberWithInt:thisClient.clientsId];
Run Code Online (Sandbox Code Playgroud)

您正在clientId上调用release,因此您会遇到内存问题.要修复它,请删除上面第一行在这种情况下无用,并将第二行更新为:

NSNumber * clientId = [NSNumber numberWithInt:thisClient.clientsId];
Run Code Online (Sandbox Code Playgroud)

然后删除:

[clientId release]
Run Code Online (Sandbox Code Playgroud)

因为clientId会自动释放.

编辑:Re仍然有问题...我不知道你如何操纵app委托中的客户端,否则代码应该工作正常,我创建了一个小例子,省略了我看不到的部分(app委托和客户端) ):

//命令行实用程序 - 基础工具项目:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

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

    int i = 0;
    for (i = 0; i < 10; ++i)
    {
        NSString * clientName = [NSString stringWithString:@"client"];  //no need to release
        NSNumber * clientId = [NSNumber numberWithInt:i];
        NSDictionary * theItem = [NSDictionary dictionaryWithObjectsAndKeys:
                                  clientName, @"name",
                                  clientId, @"id",
                                  nil];

        [theList addObject:theItem];
    }

    for (id item in theList) for (id key in item) NSLog(@"%@ - %@", key, [item objectForKey:key]);

    [theList release];
    [pool drain];
    return 0;
}
Run Code Online (Sandbox Code Playgroud)