iPhone内存管理,视图之间的EXC_BAD_ACCESS NSString
我在内存管理方面遇到一些问题,我认为我的问题是分配和对象释放。我想将一个 NSString (userID) 值分配给其他视图中的另一个 NSString (user_id)。
MainViewController.h
#import "OnlineCheckViewController.h"
@interface MainViewController : UIViewController
{
NSString *userID;
}
@end
MainViewController.m
#import "MainViewController.h"
- (IBAction)OnlineCheck:(id)sender
{
OnlineCheckViewController *controller = [[OnlineCheckViewController alloc] initWithNibName:@"OnlineCheckViewController" bundle:nil];
controller.delegate = self;
controller.user_id = userID;
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:controller animated:YES];
[controller release];
}
- (void)viewDidUnload
{
userID = nil;
[super viewDidUnload];
}
- (void)dealloc
{
[userID release];
[super dealloc];
}
OnlineCheckViewController.h
#import <UIKit/UIKit.h>
#import "OnlineCheckResultsViewController.h"
@protocol OnlineCheckViewControllerDelegate;
@interface OnlineCheckViewController : UIViewController
<OnlineCheckResultsViewControllerDelegate>
{
NSString *user_id;
}
@property (nonatomic, assign) id <OnlineCheckViewControllerDelegate> delegate;
@property (nonatomic, retain) NSString *user_id;
@end
OnlineCheckViewController.m
#import "OnlineCheckViewController.h"
@synthesize user_id;
@synthesize delegate=_delegate;
- (void)dealloc
{
[user_id release];
[super dealloc];
}
- (void)viewDidUnload
{
[self setUser_id:nil];
[super viewDidUnload];
}
谢谢!
I have some problems with memory management, I think my problem is the assignment, and object releases. I want to assign a NSString (userID) value to another NSString in other view (user_id).
MainViewController.h
#import "OnlineCheckViewController.h"
@interface MainViewController : UIViewController
{
NSString *userID;
}
@end
MainViewController.m
#import "MainViewController.h"
- (IBAction)OnlineCheck:(id)sender
{
OnlineCheckViewController *controller = [[OnlineCheckViewController alloc] initWithNibName:@"OnlineCheckViewController" bundle:nil];
controller.delegate = self;
controller.user_id = userID;
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:controller animated:YES];
[controller release];
}
- (void)viewDidUnload
{
userID = nil;
[super viewDidUnload];
}
- (void)dealloc
{
[userID release];
[super dealloc];
}
OnlineCheckViewController.h
#import <UIKit/UIKit.h>
#import "OnlineCheckResultsViewController.h"
@protocol OnlineCheckViewControllerDelegate;
@interface OnlineCheckViewController : UIViewController
<OnlineCheckResultsViewControllerDelegate>
{
NSString *user_id;
}
@property (nonatomic, assign) id <OnlineCheckViewControllerDelegate> delegate;
@property (nonatomic, retain) NSString *user_id;
@end
OnlineCheckViewController.m
#import "OnlineCheckViewController.h"
@synthesize user_id;
@synthesize delegate=_delegate;
- (void)dealloc
{
[user_id release];
[super dealloc];
}
- (void)viewDidUnload
{
[self setUser_id:nil];
[super viewDidUnload];
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我敢打赌,这就是你的问题。请注意,当您创建属性“user_id”时,您指定“retain”。保留可确保处理器在您指示之前不会释放字符串。
NSString 会自动设置为自动释放。如果你打算将值保留在非属性 NSString (userID) 中,那么你需要调用 [userID keep];设置它的值后。
我猜测您从未保留过 userID,并且当您尝试将 user_id 设置为它时,它会调用错误或更可能,它会抛出错误,因为您在 dealloc 中释放了它。
I'm going to bet that this is your problem. Note that when you create the property "user_id", you specify "retain". Retain makes sure that the processor doesn't release the string until you tell it to.
An NSString is automatically set to auto release. If you plan to keep the value in the non property NSString (userID) then you need to call [userID retain]; after you set the value of it.
I'm guessing that you never retained userID and when you try to set user_id to it, it calls the error or more likely, it throws the error because you release it in dealloc.
感谢您的所有回答。
我已经解决了这个问题。
在 MainViewController.h 中
,我添加了 userID IBOutlet
在 MainViewController.m
中 在 OnlineCheckViewController.h 中
更改为分配
在 OnlineCheckViewController.m 中强>
Thanks to all your answers.
I have resolved this issue.
In MainViewController.h
I have added userID IBOutlet
In MainViewController.m
In OnlineCheckViewController.h
Changed to assign
In OnlineCheckViewController.m
尝试这个代码
我想这可以解决你的问题......
Try this code
i think this may solve your problem...