2012-01-29 36 views
0

当你使用Xcode的功能,并拖动从笔尖文件到h和.m文件,Xcode中加在deallocviewDidUnload代码。它增加了我通常不会添加的额外代码。我只是好奇,如果这个额外的代码是必要的。iOS的内存管理问题

我会做[self setDisplaySlider:nil]而不是disp = nil[disp release]

这个必要吗?我不认为你必须发布disp。

@interface ViewController : UIViewController 
{ 
    IBOutlet UISegmentedControl *disp;  
} 

@property (retain, nonatomic) IBOutlet UISegmentedControl *displaySlider; 

@end 

- (void)viewDidUnload 
{ 
    [self setDisplaySlider:nil]; 
    [disp release]; 
    disp = nil; 
    [super viewDidUnload]; 
} 

- (void)dealloc { 
    [displaySlider release]; 
    [disp release]; 
    [super dealloc]; 
} 

回答

3

在我看来,你提供了一个额外的代码类。我会尽力解释。

首先,在你之前你有两个不同的IBOutlet。我认为第一个是你添加的。

IBOutlet UISegmentedControl *disp; 

第二个,相反,增加了由Xcode中,当你所做的拖放oeration。

@property (retain, nonatomic) IBOutlet UISegmentedControl *displaySlider; 

首先考虑的

术语IBOutlet仅供Xcode中的占位符。通过它,Xcode可以帮助您将实例变量连接到图形元素。

当我使用出口连接我平时喜欢提供一个@property (retain, nonatomic) IBOutlet UISegmentedControl *displaySlider;访问(如Xcode中所做的那样),因为如果你不能够在内存泄漏问题incurr。

其次考虑

已提供的代码的Xcode是正确的。当您进行拖动操作时,您将使用图形元素链接到displayController实例变量。为了平衡这方面,实例变量在dealloc方法释放如下所示:

[displayController release]; 

成瘾,Xcode中因为内存警告的情况下添加[self setDisplaySlider:nil];viewDidUnload方法可以调用。这里没有问题,因为当控制器再次加载到内存中时,插座连接被恢复。

两种方法调用之间的区别可以在Advanced Memory Management docrelease-or-set-to-nil-retained-members中读取。请注意,如果你这样做:

[displayController release]; 

您直接访问您的实例变量称为displayController,而如果你这样做:

[self setDisplaySlider:nil]; // or self.displaySlider = nil; 

您访问的访问(在这种情况下,setter方法)为实例变量。这是不一样的(为了避免混淆,请参阅我提供的代码)。

所以,这是我的代码将使用(我加了一些意见,引导你):

//.h 
@interface ViewController : UIViewController 
{ 
    UISegmentedControl *disp; // instance variable called disp 
    // (A) now this is no longer necessary, new compile mechanism will create an instance 
    // variable called _displaySlider under the hood 
} 

@property (retain, nonatomic) IBOutlet UISegmentedControl *displaySlider; 

@end 

//.m 
@synthesize displaySlider = disp; // I say to Xcode to create a setter and a getter to access the instance variable called disp as written in @property directive 
// no longer necessary for (A) 

- (void)viewDidUnload 
{ 
    [super viewDidUnload];   
    [self setDisplaySlider:nil]; // I call the setter method to release the UISegmentedControl 
} 

- (void)dealloc { 
    [disp release]; // I release the UISegmentedControl directly 
    // if you choose the (A) mechanism simply do 
    // [_displaySlider release]; 
    [super dealloc]; 
} 

希望它能帮助。

+0

我明白,它看起来像XCode生成的代码与我习惯的不同。即使没有@property的标签也会被设置为零并释放。我认为这并不是必要的 – Vikings 2012-01-29 22:13:11

0

看一看你viewDidLoad:我想你会发现这些对象被分配在那里。

您需要在viewDidUnload中使用release来平衡它,否则如果再次加载视图,对象将被再次分配而不会被释放,您将会泄漏。

+0

我没有在viewDidLoad中分配任何东西,这是XCode自动生成的代码,如果您从界面生成器拖放到类文件 – Vikings 2012-01-29 22:11:38