gpt4 book ai didi

iphone - 内存管理困惑 Objective C

转载 作者:行者123 更新时间:2023-11-28 19:10:26 25 4
gpt4 key购买 nike

问题一

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 中的代码时,它在倒数第二行显示“对象发送 - 自动释放太多次”。我不明白为什么会这样显示。

Screenshot 1

问题 2:

return [[[OAProblem alloc] initWithResponseBody:response] autorelease];

在这一行中,它显示“存储在‘self’中的对象的潜在泄漏”。

Screenshot 2

最佳答案

Why are you not using 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;
}

这就是他提示保留计数 == 0 的对象的原因。

关于iphone - 内存管理困惑 Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16163178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com