【发布时间】:2011-09-30 22:15:05
【问题描述】:
我有以下代码,其中每个对象都是 UISwitch IBOutlet 属性。我不确定为什么在使用 xcode 分析器时每行都会收到内存泄漏警告。
- (IBAction)copyEntirePreviousNoteButtonClicked:(id)sender
{
self.myUISwitch1.on = TRUE;
self.myUISwitch2.on = TRUE;
}
- (IBAction)updateButtonClicked:(id)sender
{
NSMutableDictionary *copyOptions = [[[NSMutableDictionary alloc] init] autorelease];
if (self.myUISwitch1.on) {
[copyOptions setValue:@"ON" forKey:@"myUISwitch1"];
}
if (self.myUISwitch2.on) {
[copyOptions setValue:@"ON" forKey:@"myUISwitch2"];
}
}
更新完整代码:
@property (nonatomic, retain) IBOutlet UISwitch *copy_hp_cchpi;
@property (nonatomic, retain) IBOutlet UISwitch *copy_hp_history;
- (IBAction)copyEntirePreviousNoteButtonClicked:(id)sender
{
self.copy_hp_cchpi.on = YES;
self.copy_hp_history.on = TRUE;
}
- (IBAction)updateButtonClicked:(id)sender
{
NSMutableDictionary *copyOptions = [[[NSMutableDictionary alloc] init] autorelease];
if (self.copy_hp_cchpi.on) {
[copyOptions setValue:@"ON" forKey:@"copy_hp_cc_history_present_illness"];
}
if (self.copy_hp_history.on) {
[copyOptions setValue:@"ON" forKey:@"copy_hp_med_fam_social_history"];
}
int rcode = [MyAPIDataSource copyPreviewAppointmentClinicalInfo:[MyAPIDataSource getCurrentAppointmentId] copyOptions:copyOptions];
if (rcode)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to copy last appointment information. Please try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
//Send Notifications to other screens that clinical info was copied from last appointment to current one.
[[NSNotificationCenter defaultCenter] postNotificationName:@"LastAppointmentLoadedHandler" object:self];
[self dismissModalViewControllerAnimated:YES];
}
}
【问题讨论】:
-
你在哪一行得到警告,你的代码真的是这样吗?或者你是否为 StackOverflow 编辑了它 - 你的 updateButtonClicked 方法只是创建一个 NSMutableDictionary 然后把它扔掉。
-
myUISwitch1 和 myUISwitch2 是如何声明的,@properties,如果是这样的话,带有 whoa 属性。最好提供代码。
-
它们是具有保留和非原子属性的属性。它们在 dealloc 中释放。代码有点笨拙,NSMutableDictionary 之后上传到服务器。
-
@RobinSummerhill 我在上面添加了完整的代码。我在上面的几乎每一行都收到错误
Potential leak of object at line。
标签: iphone objective-c xcode memory-management uiswitch