【问题标题】:Memory leak vs Zombie - iPhone内存泄漏与僵尸 - iPhone
【发布时间】:2012-01-31 16:59:08
【问题描述】:

我的 iPhone 应用程序由于僵尸或内存泄漏而崩溃。我已将其缩小到 3 行代码,并且可以通过注释/取消注释代码可靠地实现这两件事之一。当在结果列表(tableView)和包含地图和一些标签的详细信息页面之间导航时会出现错误,当我第一次从地图导航回结果列表时发生内存泄漏,僵尸可能在 5/ 之后发生6 次导航到不同的结果并返回。

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

#define METERS_PER_MILE 1609.344

@interface ResDetailsPageVC : UIViewController <MKMapViewDelegate, UIAlertViewDelegate>  {

UISegmentedControl *mapTypeSwitcher;
MKMapView *mapView;      

UILabel *nameLabel;
UIButton *addressLabel;
UILabel *telephoneLabel;

NSString *address;

}

@property (nonatomic, retain) IBOutlet UISegmentedControl *mapTypeSwitcher;
@property (nonatomic, retain) IBOutlet MKMapView *mapView;

@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@property (nonatomic, retain) IBOutlet UIButton *addressLabel;
@property (nonatomic, retain) IBOutlet UILabel *telephoneLabel;


- (IBAction)segmentedControlIndexChanged;
- (IBAction)callButtonClick;
- (IBAction)addressClick;

- (void) callNumber;

@end






@synthesize mapView;
@synthesize mapTypeSwitcher;

@synthesize nameLabel, addressLabel, telephoneLabel;

- (void)dealloc {

// if these lines are commented out - memory leak
// if not - zombie?!
/*self.telephoneLabel = nil;
self.addressLabel = nil;
self.nameLabel = nil;*/
self.mapView = nil;
self.mapTypeSwitcher = nil;

[super dealloc];

}

【问题讨论】:

  • 你把retains放在那里了吗?因为 IBOutlets 是指向 xib 中对象的链接,所以它们不需要保留。通常它们是assigned 或者在 ARC 代码的情况下是 __unsafe_unretainedweak
  • 我尝试将属性更改为 (nonatomic, assign),然后在 dealloc 中不解除分配或将它们设置为 nil,但仍然得到僵尸。
  • 你一直说你有僵尸——这通常会给你更多信息,比如什么对象正在发送消息。
  • 发现了问题——有趣的是,我在maybelost.com/2011/01/a-basic-mapview-and-annotation-tutorial 找到的地图注释代码导致了崩溃。出于某种原因不喜欢 [title release].. 将代码替换为 mithin.in/2009/06/22/… 的精简版代码 - 所有内存泄漏、僵尸和其他讨厌的东西都消失了!
  • 那个实现泄露了标题和副标题。我认为你的程序除了这个类的内存管理之外还有问题。

标签: iphone xcode memory-leaks zombie-process


【解决方案1】:

某处其他一些代码正在使用相同的对象,其地址存储在这三个属性之一中,但其他代码没有正确保留该对象。

【讨论】:

  • 其他一些代码正在使用 UILabel 对象?还是我用来设置标签值的对象(这来自一个 NSDictionary,最初是从 JSON 请求中检索到的)
  • 它必须是对 UILabel 本身的某种引用。您是否从另一个类中引用 UILabel 中的值?
【解决方案2】:

我向你推荐这个:

- (void)dealloc {
[telephoneLabel release]; telephoneLabel = nil;
[addressLabel release]; addressLabel = nil;
....

[super dealloc];
}

【讨论】:

    猜你喜欢
    • 2012-09-21
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2010-11-23
    • 2011-12-10
    • 2011-05-24
    相关资源
    最近更新 更多