内存泄漏与僵尸 - iPhone

发布于 2025-01-01 06:55:08 字数 1355 浏览 2 评论 0原文

我的 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];

}

My iPhone app is either crashing due to to a zombie, or leaking memory.. I've narrowed it down to 3 lines of code and can reliably get one of the two things to happen by commenting/uncommenting the code. The bugs occur when navigation between a list of results (tableView) and a details page containing a map and a few labels, memory leak happens the first time I navigation from the map back to the list of results, the zombie occurs after maybe 5/6 times navigating to different results and back.

#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];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰魂雪魄 2025-01-08 06:55:08

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

Somewhere some other piece of code is using the same object whose address is stored in one of those three properties, but that other piece of code has not properly retained the object.

夏见 2025-01-08 06:55:08

我向你推荐这个:

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

[super dealloc];
}

I recommend this to you:

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

[super dealloc];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文