【问题标题】:After converting to ARC I am getting "message sent to deallocated instance"转换为 ARC 后,我收到“发送到已释放实例的消息”
【发布时间】:2015-06-13 19:31:25
【问题描述】:

我正在创建一个这样的视图实例...

- (IBAction)findPatientTapped:(id)sender { 

    RequestDialogViewController *findPatientViewController = [[RequestDialogViewController alloc] initWithNibName:@"RequestDialogViewController"    bundle:nil];

    findPatientViewController.delegate = self;
    findPatientViewController.autoCorrectOff = YES;
    findPatientViewController.defaultResponse = @"";
    findPatientViewController.keyboardType = @"ASCII";
    findPatientViewController.returnKeyType = @"Go";
    findPatientViewController.tag = findRequestTag;
    findPatientViewController.editSet = YES;
    findPatientViewController.titleText = @"Find Patient";
    findPatientViewController.messageText =@"Enter all or a portion of the patient's name...";
    findPatientViewController.messageText2 = @"Last Name...";
    findPatientViewController.messageText3 = @"First Name...";
    findPatientViewController.showResponse2 = YES;   
    findPatientViewController.showNavBarSaveButton = NO;
    findPatientViewController.infoTextFile = @"";
    findPatientViewController.view.frame = CGRectMake(280, 230, 480, 300);

    [self.view addSubview:findPatientViewController.view];
}

视图包含 2 个 UITextField 字段供用户输入。在运行转换为 ARC 工具之前,这一切都很好。转换后,视图在尝试在两个字段中的任何一个中输入值时崩溃...

-[RequestDialogViewController respondsToSelector:]:消息发送到释放的实例

这是运行转ARC工具后UIViewController的.h文件...

#import <UIKit/UIKit.h>
@protocol RequestDialogViewControllerDelegate;

@interface RequestDialogViewController : UIViewController <UITextFieldDelegate>{
    id<RequestDialogViewControllerDelegate> __weak delegate;
    IBOutlet UINavigationBar *navBar;
    IBOutlet UITextField *response;
    IBOutlet UITextField *response2;
    IBOutlet UILabel *message;
    IBOutlet UILabel *message2;
    IBOutlet UILabel *message3;
    IBOutlet UIButton *saveButton;
    NSTimer *selectAllTimer;

    NSString *defaultResponse;
    NSString *titleText, *infoTextFile;
    NSString *messageText, *messageText2, *messageText3;
    NSString *placeHolderText;
    NSString *keyboardType, *returnKeyType;
    BOOL editSet, showSaveButton,showNavBarSaveButton, showResponse2, autoCorrectOff;
    int tag;
}

@property (weak) id<RequestDialogViewControllerDelegate> delegate;
@property (nonatomic, strong)IBOutlet UITextField *response;
@property (nonatomic, strong) IBOutlet UITextField *response2;
@property (nonatomic, strong)IBOutlet UILabel *message;
@property (nonatomic, strong)IBOutlet UILabel *message2;
@property (nonatomic, strong) IBOutlet UILabel *message3;
@property (nonatomic, strong)IBOutlet UIButton *saveButton;
@property (nonatomic, strong)NSString *defaultResponse;
@property (nonatomic, strong)NSString *titleText, *infoTextFile;
@property (nonatomic, strong)NSString *messageText, *messageText2, *messageText3;
@property (nonatomic, strong)NSString *placeHolderText;
@property (nonatomic, strong)NSString *keyboardType, *returnKeyType;
@property (nonatomic, strong)IBOutlet UINavigationBar *navBar;
@property (readwrite)BOOL editSet, showSaveButton, showNavBarSaveButton, showResponse2, autoCorrectOff;
@property (readwrite)int tag;
@property (nonatomic, strong) NSTimer *selectAllTimer;

- (IBAction)saveButtonPressed:(id)sender;
- (void)selectAll;
- (IBAction)editingDidEnd:(id)sender;

@end


@protocol RequestDialogViewControllerDelegate <NSObject>
@optional
- (void)requestDialogViewDidDismiss:(RequestDialogViewController *)controller withResponse:(NSString*)reqResponse response2:(NSString*)reqResponse2;
@end

这是创建 RequestDialog 视图实例的类的 .h 文件中的相关引用...

#import "RequestDialogViewController.h"

@interface OrthoViewController : UIViewController <RequestDialogViewControllerDelegate>{
}
- (void)requestDialogViewDidDismiss:(RequestDialogViewController *)controller withResponse:(NSString*)reqResponse response2:(NSString*)reqResponse2;
@end

我需要做什么才能在 ARC 下进行这项工作?我认为这可能与协议的形成方式有关。

谢谢,

约翰

**** 感谢 Dan,我通过使 findPatientViewController 成为调用类的属性来解决了这个问题...

RequestDialogViewController *findPatientViewController;
@implementation OrthoViewController

- (IBAction)findPatientTapped:(id)sender {    
    findPatientViewController = [[RequestDialogViewController alloc] initWithNibName:@"RequestDialogViewController" bundle:nil];
    //set all the properties and add the subview
 }
@end

【问题讨论】:

    标签: ios objective-c xcode6 automatic-ref-counting


    【解决方案1】:

    您的findPatientViewController 没有保留它,因此它在您创建它的方法结束时被释放。然后,当它的视图中的某些东西试图调用它的委托方法时,你就会崩溃。

    如果findPatientTapped 是视图控制器中的一个方法,那么您应该将findPatientViewController 添加为子视图控制器。如果它在视图中,那么您至少需要将 findPatientViewController 存储在一个属性中,这样它就不会在您仍在使用它时被释放。

    您的代码在 ARC 之前并不能真正正常工作,只是发生了内存泄漏。

    【讨论】:

    • 谢谢。好的,我现在知道我需要做什么了。我在每个方法中声明了 findPatientViewController。我将定义作为类的参数向上移动,它可以工作!谢谢。
    • 意思是说“将定义作为类的属性向上移动”而不是参数。
    猜你喜欢
    • 1970-01-01
    • 2011-12-17
    • 2011-06-16
    • 2012-04-09
    相关资源
    最近更新 更多