gpt4 book ai didi

ios - Xcode "Analyze"消息混淆

转载 作者:行者123 更新时间:2023-12-01 18:05:17 27 4
gpt4 key购买 nike

我继承了一个不使用 ARC 的旧 Objective C 项目,并且由于第三方库的使用,可以使用 ARC。

我正在尝试解决由于访问未/释放内存而导致的一些随机崩溃。

我的首要任务之一是运行 Xcode 的“分析”工具,看看它发现了什么问题。在大多数情况下,解决这些问题非常有用,而且大多是常识。

但是,我有一些类(class)(即自定义 UITableViewCell s)引起了我的一些担忧,我不知道如何解决这些问题

Potential leak of an object

实际类(class)

#import "userListView.h"

@implementation userListView
@synthesize userCustomCelltextlabel,userCustomDetailtextlabel;
@synthesize unreadIdentifierImg;

-(void)dealloc {
[userCustomCelltextlabel release];
[userCustomDetailtextlabel release];
[unreadIdentifierImg release];
[super dealloc];
}


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {

self.unreadIdentifierImg=[[UIImageView alloc]initWithFrame:CGRectMake(165,1,12,38)];

self.userCustomCelltextlabel=[[UILabel alloc]initWithFrame:CGRectMake(12,5,150,20)];
self.userCustomCelltextlabel.font=[UIFont fontWithName:@"TrebuchetMS-Bold" size:13.0f];
self.userCustomCelltextlabel.backgroundColor=[UIColor clearColor];

self.userCustomDetailtextlabel=[[UILabel alloc]initWithFrame:CGRectMake(12,22,150,15)];
self.userCustomDetailtextlabel.backgroundColor=[UIColor clearColor];
self.userCustomDetailtextlabel.textColor=[UIColor grayColor];
self.userCustomDetailtextlabel.font=[UIFont fontWithName:@"Trebuchet MS" size:12.0];

self.userCustomDetailtextlabel.numberOfLines=1;
self.userCustomDetailtextlabel.lineBreakMode=NSLineBreakByTruncatingTail;


//self.unreadIdentifierImg.image=[UIImage imageNamed:@"MessageEntrySendButton.png"];
//self.unreadIdentifierImg.hidden=YES;
[self.contentView addSubview:self.unreadIdentifierImg];

[self.contentView addSubview:self.userCustomDetailtextlabel];
[self.contentView addSubview:self.userCustomCelltextlabel];


}
return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

@end

和标题
#import <UIKit/UIKit.h>

@interface userListView : UITableViewCell

@property (retain, nonatomic) UILabel *userCustomCelltextlabel;
@property (retain, nonatomic) UILabel *userCustomDetailtextlabel;

@property (retain, nonatomic) UIImageView *unreadIdentifierImg;

@end

扩展错误显示...
// 2. Method returns an instance of UIImageView with a +1 retain count
self.unreadIdentifierImg=[[UIImageView alloc]initWithFrame:CGRectMake(165,1,12,38)];
// Seems to point to UIImageView init

// 3. Object leaked: allocated object is not referenced later in this execution path and has a retain count of +1
self.userCustomCelltextlabel=[[UILabel alloc]initWithFrame:CGRectMake(12,5,150,20)];
// Which seems to point to the UILabel init

并重复 userCustomDetailtextlabel .我在这里遇到的问题是 allocated object is not referenced later in this execution path ,这是不正确的,因为它已添加到 contentView .

现在,显然,我们希望将对象保留为类的属性,并仅在对象被释放时处理它们,但我似乎无法确定消息的正面或反面。

我应该忽略它们吗?还有什么我应该研究的东西来尝试解决它们吗?

最佳答案

属性是用保留声明的,因此编译器将生成一个保留值的 setter 方法。

您使用 alloc/init 创建标签,这会为您提供保留计数为 +1 的标签,然后使用 self.userCustomCelltextlabel 将其分配给您的属性这会调用相应的 setter 方法,该方法正在向标签发送保留,现在它的保留计数为 +2。

在 dealloc 中,您发送 release 并且标签的保留计数为 +1 并且正在泄漏。

添加 autorelease到您的 alloc/init 或直接使用 _userCustomCelltextlabel 将其分配给 ivar .

关于ios - Xcode "Analyze"消息混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47408146/

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