Obj-C,在线分配的对象的潜在泄漏,警告?

Jul*_*les 0 xcode cocoa-touch objective-c analyzer

我已将以下变量声明为实例变量并在我的m文件中使用它,但是我收到了警告.

TransparentToolbar *tools;
Run Code Online (Sandbox Code Playgroud)

在线分配的对象的潜在泄漏......

我已经尝试过为它创建一个属性,例如..

@property (nonatomic, retain) TransparentToolbar *tools;
Run Code Online (Sandbox Code Playgroud)

并合成并释放它,但我的视图在dealloc结束时崩溃.

我究竟做错了什么 ?

在pickerSortingDataCurrent上编辑相同的警告......

h
@interface myViewController : UIViewController <UIActionSheetDelegate, 
    UIPickerViewDelegate, UIPickerViewDataSource, UITableViewDelegate, 
    UITableViewDataSource, MFMailComposeViewControllerDelegate> {

    TransparentToolbar *tools;

    NSArray *pickerSortingDataCurrent;
}
@property (nonatomic, retain) TransparentToolbar *tools;
@property (nonatomic, retain) NSArray *pickerSortingDataCurrent;

m
@synthesize pickerSortingDataCurrent;
@synthesize tools;

- (void)viewDidLoad {
    [super viewDidLoad];

    tools = [[[TransparentToolbar alloc] 
           initWithFrame:CGRectMake(0, 0, 70, 44.01)] autorelease];
    tools.barStyle = UIBarStyleBlackOpaque;

    self.pickerSortingDataCurrent = [[NSArray alloc] initWithObjects:
      @"Next Date Ascending", 
      @"Next Date Descending", nil];     // removed some items here
}

- (void)dealloc {
    [tools release];
    [pickerSortingDataCurrent release];
    [super dealloc];
}
Run Code Online (Sandbox Code Playgroud)

啊,我有autorelease ....但这并没有解决pickerSortingDataCurrent ...

编辑...

#import "TransparentToolbar.h"

@implementation TransparentToolbar

- (void)drawRect:(CGRect)rect {
    // do nothing in here
}

- (void) applyTranslucentBackground
{
    self.backgroundColor = [UIColor clearColor];
    self.opaque = NO;
    self.translucent = YES;
}

- (id) init
{
    self = [super init];
    [self applyTranslucentBackground];
    return self;
}

// Override initWithFrame.
- (id) initWithFrame:(CGRect) frame
{
    self = [super initWithFrame:frame];
    [self applyTranslucentBackground];
    return self;
}

@end
Run Code Online (Sandbox Code Playgroud)

进一步编辑 在此输入图像描述

Pau*_*l.s 5

如果您@property通常定义任何时候访问类中的ivar,则使用getter/setter,无论是点符号还是标准方法调用.

点符号

id localyMyVar = self.myVar;
self.myVar = @"A string";
Run Code Online (Sandbox Code Playgroud)

标准方法调用

id localMyVar = [self myVar];
[self setMyVar:@"A string"];
Run Code Online (Sandbox Code Playgroud)

如果你总是明确地使用这些getter和setter,那么你几乎不需要在你的代码中的任何地方调用release,dealloc或者重写setMyVar:方法.这样做可以在有限的地方进行内存管理.如果你开始释放并保留自己,那么当你刚开始时,事情可能会有点复杂.

UPDATE

@bbum为您提供答案,但我认为您也可以从编码中更加一致地受益.

例如,在违规行之前,您不使用setter直接分配给ivar.要一致并使用你花时间合成的setter/getter.我会改写

tools = [[[TransparentToolbar alloc] 
       initWithFrame:CGRectMake(0, 0, 70, 44.01)] autorelease];
tools.barStyle = UIBarStyleBlackOpaque;
Run Code Online (Sandbox Code Playgroud)

TransparentToolbar *tmpTools = [[TransparentToolbar alloc] initWithFrame:CGRectMake(0, 0, 70, 44.01)];
tmpTools.barStyle = UIBarStyleBlackOpaque;
self.tools = tmpTools;
[tmpTools release]; tmpTools = nil;
Run Code Online (Sandbox Code Playgroud)

您的init方法并不是真正遵循指南要么您应该检查self实际设置,所以它看起来应该类似于:

- (id)init
{
    self = [super init];
    if (self) {
        [self applyTranslucentBackground];
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

UPDATE

你在这里看到的内存泄漏:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.tools];
Run Code Online (Sandbox Code Playgroud)

是因为你看UINavigationItem的文档,你会看到它rightBarButtonItem被声明为retain

@property(nonatomic, retain) UIBarButtonItem *rightBarButtonItem
Run Code Online (Sandbox Code Playgroud)

因此,调用self.navigationItem.rightBarButtonItem将在您传入的对象上保留+1,然后您将分配/启动,这是另一个+1保留.该UINavigationItem会发布的时候,它是dealloc'd保留,但仍然会有你原来的挽留游逛.

修复:

UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.tools];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
[rightBarButtonItem release]; rightBarButtonItem = nil;
Run Code Online (Sandbox Code Playgroud)