繁体   English   中英

不了解内存分析

[英]Don't understand memory analysis

我已将XCode升级到versio 3.2.3,以在我的iPhone项目上支持iOS4。 我使用静态分析器检查了内存管理问题。

在我的例程中,我遇到以下问题:在向日历中添加事件以使其具有状态之后,我会生成用户警报。

这运行良好,但是内存分析器不喜欢我定义警报的方式。 我看不到编码问题,对吗? (我用“ <<<<”表示内存分析器提示)

- (IBAction) addToCalendar {
        ...
    UIAlertView  *tmpAlert  = [UIAlertView alloc];        <<<<Method returns an Objective-C object with a+1 retain count (owning reference)

    calData.startDate   = iVar.zeitVon;
    calData.endDate     = iEvent.zeitBis;
    calData.title       = iVar.title;
    calData.calendar    = myEventStore.defaultCalendarForNewEvents;

    if ([tmpEventStore saveEvent:tmpEvent span:EKSpanThisEvent error:&tmpSaveError]) {
        // Show a save success dialog
        [tmpAlert initWithTitle:@"Success"        <<<<Object released
                        message:@"entry saved" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    } else {
        // Show a save error dialog
        [tmpAlert initWithTitle:@"Error" 
                        message:@"entry not saved" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] ;
    }
    [tmpAlert show];                               <<<<Reference counted object is used after its released
    [tmpAlert release];
}

谢谢

您永远不要解耦allocinit init经常会改变后台的对象! 尝试

NSString* foo=[NSString alloc]; 
NSLog(@"%p %@", foo, [foo class]);
foo=[foo initWithString:@"bar"]; 
NSLog(@"%p %@", foo, [foo class]);

您会看到类似

2010-07-14 01:00:55.359 a.out[17862:903] 0x10010d080 NSPlaceholderString
2010-07-14 01:00:55.363 a.out[17862:903] 0x100001070 NSCFString

这表明+[NSString alloc]并没有真正分配任何东西。 相反,工作是initWithString本身。 我不认为UIAlertView会这样做,但您永远不会知道。

回顾一下:永远不要解耦allocinit 我认为静态分析器只是假设每个人都使用[[... alloc] init] ,因此它被您的代码弄糊涂了。 分析器应该警告您不要解耦allocinit

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM