iphone - 内存管理困惑 Objective C

标签 iphone ios objective-c ipad xcode4

问题一

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/

相关文章:

iphone - 如何让 YouTube 视频在 UIWebView 中开始自动播放

ios - iTunes Connect 应用程序预览未被其他大小保存和删除

ios - UIView 和 UIViewController 有什么区别?

iPhone:显示 iAd 问题

ios - UITableView 部分标题的自动布局问题

ios - ideviceinstaller -l不会在iPhone上显示应用列表

IOS - 点击 TabBarItem,检查变量以显示相应的 ViewController

iphone - UIToolbar 具有半透明而不是黑色

ios - TableView Cell with Minor Lag (Async Proper Use?) Swift

iphone - UITableViewDataSource 委托(delegate)