Iphone 内存管理

Iphone 内存管理,iphone,ios,objective-c,ipad,xcode4,Iphone,Ios,Objective C,Ipad,Xcode4,问题1 Place *place = [[[Place alloc] initwithCoordinate:location anotationType:CSMapAnnotationTypeStart] autorealease]; place.name = name; place.description = description; place.strUniqueIdentity = uniqueIdentity; NSLog(@"Unique identity %@",uniqueIden

问题1

Place *place = [[[Place alloc] initwithCoordinate:location anotationType:CSMapAnnotationTypeStart] autorealease];
place.name = name;
place.description = description;
place.strUniqueIdentity = uniqueIdentity;
NSLog(@"Unique identity %@",uniqueIdentity);

PlaceMark *marker = [[PlaceMark alloc] initWithPlace:place annotationType:MapAnnotationTypePin];
return [marker autorelease];
当我分析xcode 4.6.2中的代码时,它在最后一行的第二行显示“objectsent-autorelease太多次”。 我不明白为什么会这样

问题2:

return [[[OAProblem alloc] initWithResponseBody:response] autorelease];
这一行显示的是“存储在‘self’中的对象的潜在泄漏”


更好的解决方案是将应用程序转换为ARC(自动引用计数)。它可能会占用内存问题


以您的xcode项目为例,编辑>重构>转换为目标C ARC>确定

更好的解决方案是将您的应用程序转换为ARC(自动引用计数)。它可能会占用内存问题

Place *place = [[Place alloc] initwithCoordinate:location anotationType:CSMapAnnotationTypeStart];
place.name = name;
place.description = description;
place.strUniqueIdentity = uniqueIdentity;
NSLog(@"Unique identity %@",uniqueIdentity);

PlaceMark *marker = [[PlaceMark alloc] initWithPlace:place    annotationType:MapAnnotationTypePin];
[place release];//I think you are release place object, cause of memory leak
return [marker autorelease];

以您的xcode项目为例,编辑>重构>转换为目标C ARC>确定

为什么不使用ARC?的答案是完全无效。OP特别问,为什么这会发生在他自己的环境中。因此:

Place *place = [[Place alloc] initwithCoordinate:location anotationType:CSMapAnnotationTypeStart];
place.name = name;
place.description = description;
place.strUniqueIdentity = uniqueIdentity;
NSLog(@"Unique identity %@",uniqueIdentity);

PlaceMark *marker = [[PlaceMark alloc] initWithPlace:place    annotationType:MapAnnotationTypePin];
[place release];//I think you are release place object, cause of memory leak
return [marker autorelease];
你能给我们看看你的:

Place *place = [[Place alloc] initwithCoordinate:location anotationType:CSMapAnnotationTypeStart];
同时使用仪器,看看你的项目是否存在问题。静态分析器显示的并非所有内容实际上都是问题。可能会出现一些假阳性

因此,从您的回答中可以看出:

 -(id)initwithCoordinate:(CLLocationCoordinate2D)coordinate anotationType:(CSMapAnnotationType)anotationType{ 
self.latitude = coordinate.latitude; 
self.longitude = coordinate.longitude; 
self.mapAnnotationType = anotationType; 
return self;
 }
应该是这样的:

-(id)initwithCoordinate:(CLLocationCoordinate2D)coordinate anotationType:(CSMapAnnotationType)anotationType{
    if (self = [super init])
    {
        self.latitude = coordinate.latitude;
        self.longitude = coordinate.longitude;
        self.mapAnnotationType = anotationType;
    }
        return self;
}

这就是为什么他抱怨一个retain count==0的对象。

你为什么不使用Arc?的答案是完全无效的。OP特别问,为什么这会发生在他自己的环境中。因此:

你能给我们看看你的:

Place *place = [[Place alloc] initwithCoordinate:location anotationType:CSMapAnnotationTypeStart];
同时使用仪器,看看你的项目是否存在问题。静态分析器显示的并非所有内容实际上都是问题。可能会出现一些假阳性

因此,从您的回答中可以看出:

 -(id)initwithCoordinate:(CLLocationCoordinate2D)coordinate anotationType:(CSMapAnnotationType)anotationType{ 
self.latitude = coordinate.latitude; 
self.longitude = coordinate.longitude; 
self.mapAnnotationType = anotationType; 
return self;
 }
应该是这样的:

-(id)initwithCoordinate:(CLLocationCoordinate2D)coordinate anotationType:(CSMapAnnotationType)anotationType{
    if (self = [super init])
    {
        self.latitude = coordinate.latitude;
        self.longitude = coordinate.longitude;
        self.mapAnnotationType = anotationType;
    }
        return self;
}

这就是他抱怨retain count==0的对象的原因。

您会收到警告,因为
initwithCoordinate
的拼写错误。根据Cocoa命名约定,它应该是
initWithCoordinate
(大写W)。

您会收到警告,因为
initWithCoordinate
的拼写错误。根据Cocoa命名约定,它应该是
initWithCoordinate
(大写W)。

我问题第一部分的答案是初始化方法中的小“W”

-initwithCoordinate: anotationType:
根据可可命名惯例,我必须使用大写字母“W”

我将初始化方法替换为

-initWithCoordinate: anotationType:
我问题第二部分的答案是在两个故障路径中添加一个[自我释放]


Xcode在它的分析器关于OAP问题的警告中是非常没有帮助的

实际上问题出在init方法中的代码。警告(在本例中)表示“init方法返回nil,而不释放alloc分配的对象”。修复方法是在两个故障路径中添加一个[自释放]:

- (id)initWithProblem:(NSString *) aProblem
{
    NSUInteger idx = [[OAProblem validProblems] indexOfObject:aProblem];
    if (idx == NSNotFound) {
        [self release]; // <-- Add this line to fix the warning
        return nil;
    }

    return [self initWithPointer: [[OAProblem validProblems] objectAtIndex:idx]];
}
- (id)initWithProblem:(NSString *) aProblem
{
    NSUInteger idx = [[OAProblem validProblems] indexOfObject:aProblem];
    if (idx == NSNotFound) {
        [self release]; // <-- Add this line to fix the warning
        return nil;
    }

    return [self initWithPointer: [[OAProblem validProblems] objectAtIndex:idx]];
}
-(id)initWithProblem:(NSString*)一个问题
{
NSUInteger idx=[[OAProblem validProblems]索引对象:APProblem];
if(idx==NSNotFound){

[自我释放];//我问题第一部分的答案是初始化方法中的小“w”

-initwithCoordinate: anotationType:
根据可可命名惯例,我必须使用大写字母“W”

我将初始化方法替换为

-initWithCoordinate: anotationType:
我问题第二部分的答案是在两个故障路径中添加一个[自我释放]


Xcode在它的分析器关于OAP问题的警告中是非常没有帮助的

实际问题所在的代码出现在init方法中。警告(在本例中)表示“init方法返回nil而不释放alloc分配的对象”。修复方法是在两个故障路径中添加一个[自释放]:

- (id)initWithProblem:(NSString *) aProblem
{
    NSUInteger idx = [[OAProblem validProblems] indexOfObject:aProblem];
    if (idx == NSNotFound) {
        [self release]; // <-- Add this line to fix the warning
        return nil;
    }

    return [self initWithPointer: [[OAProblem validProblems] objectAtIndex:idx]];
}
- (id)initWithProblem:(NSString *) aProblem
{
    NSUInteger idx = [[OAProblem validProblems] indexOfObject:aProblem];
    if (idx == NSNotFound) {
        [self release]; // <-- Add this line to fix the warning
        return nil;
    }

    return [self initWithPointer: [[OAProblem validProblems] objectAtIndex:idx]];
}
-(id)initWithProblem:(NSString*)一个问题
{
NSUInteger idx=[[OAProblem validProblems]索引对象:APProblem];
if(idx==NSNotFound){

[self release];//Xcode在它的分析器关于OAP问题的警告中是非常无用的

实际问题所在的代码出现在init方法中。警告(在本例中)表示“init方法返回nil,而没有释放alloc分配的对象”。修复方法是在两个故障路径中添加
[自释放]

- (id)initWithProblem:(NSString *) aProblem
{
    NSUInteger idx = [[OAProblem validProblems] indexOfObject:aProblem];
    if (idx == NSNotFound) {
        [self release]; // <-- Add this line to fix the warning
        return nil;
    }

    return [self initWithPointer: [[OAProblem validProblems] objectAtIndex:idx]];
}
- (id)initWithProblem:(NSString *) aProblem
{
    NSUInteger idx = [[OAProblem validProblems] indexOfObject:aProblem];
    if (idx == NSNotFound) {
        [self release]; // <-- Add this line to fix the warning
        return nil;
    }

    return [self initWithPointer: [[OAProblem validProblems] objectAtIndex:idx]];
}
-(id)initWithProblem:(NSString*)一个问题
{
NSUInteger idx=[[OAProblem validProblems]索引对象:APProblem];
if(idx==NSNotFound){

[self release];//Xcode在它的分析器关于OAP问题的警告中是非常无用的

实际问题所在的代码出现在init方法中。警告(在本例中)表示“init方法返回nil,而没有释放alloc分配的对象”。修复方法是在两个故障路径中添加
[自释放]

- (id)initWithProblem:(NSString *) aProblem
{
    NSUInteger idx = [[OAProblem validProblems] indexOfObject:aProblem];
    if (idx == NSNotFound) {
        [self release]; // <-- Add this line to fix the warning
        return nil;
    }

    return [self initWithPointer: [[OAProblem validProblems] objectAtIndex:idx]];
}
- (id)initWithProblem:(NSString *) aProblem
{
    NSUInteger idx = [[OAProblem validProblems] indexOfObject:aProblem];
    if (idx == NSNotFound) {
        [self release]; // <-- Add this line to fix the warning
        return nil;
    }

    return [self initWithPointer: [[OAProblem validProblems] objectAtIndex:idx]];
}
-(id)initWithProblem:(NSString*)一个问题
{
NSUInteger idx=[[OAProblem validProblems]索引对象:APProblem];
if(idx==NSNotFound){

[自我释放];//你为什么不使用
ARC
?这似乎是正确的,你对类本身的内存管理做了一些修改,比如initWithCoordinate或initWithPlace。你确定在你显示的范围内没有其他内容吗?@msgambel:我总是使用手动引用计数,因为它比某些人指定的ARC更好ons。您的代码段与屏幕抓图不同。@Yash:除非您手头有具体问题,否则绝对没有理由不使用ARC(您可以在ARC项目中将特定的有问题的文件标记为MRC).ARC编译到MRC。Clang将高度复杂的算法应用到您的代码库中,以确定在何处最佳地插入
retain
/
release
,省略所有不必要的算法(从而加快代码速度).现在你的决定似乎是基于纯粹的猜测和道听途说。ARC会帮助你。使用它。(此外,ARC强制执行严格的方法命名约定,这也是一件好事。)