UISwitch 的静态分析器内存泄漏警告
我有以下代码,其中每个对象都是 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];
}
}
I have the following code where each object is a UISwitch IBOutlet property. I'm nor sure why I am getting a memory leak warning for each line when using xcode analyzer.
- (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"];
}
}
Update with full code:
@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];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番绞尽脑汁之后...
按照惯例,任何包含“copy”一词的 Objective C 方法都应该返回一个保留的对象。这同样适用于方法前缀“init”和“new”。
静态分析器知道此约定,并抱怨您的 copyEntirePreviousNoteButtonClicked 方法不返回保留的对象。
解决方案是不要在方法命名中包含“复制”一词,除非您确实这么想。坚持 Objective C 方法命名约定。更改方法的名称,问题就会消失。
After a lot of head scratching...
By convention, any Objective C method that contains the word 'copy' is expected to return a retained object. The same applies for the method prefixes 'init' and 'new'.
The static analyzer knows about this convention and is complaining that your copyEntirePreviousNoteButtonClicked method does not return a retained object.
The solution is not to name your methods containing the word 'copy' unless you really mean it. Stick to the Objective C method naming conventions. Change the name of your method and the problem will go away.